Skip to content
Snippets Groups Projects
Commit 887454f1 authored by Daniel Espinosa's avatar Daniel Espinosa
Browse files

GOM: Using property nick to find serializables

If you define a property with a prefix "::" it
will be serialized as a XML element attribute.

Property's nick are used as attribute's name
take care to use descriptive names using, may be,
camel case names.
parent 33aa9243
Branches
Tags
No related merge requests found
......@@ -39,6 +39,16 @@ public interface GXml.GomObject : GLib.Object,
* attribute use property's nick name as declared in {@link GLib.ParamSpec}
*/
public virtual bool use_nick_name () { return true; }
public virtual HashTable<string,string> get_properties_map () {
var l = new HashTable<string,string> (str_hash, str_equal);
foreach (ParamSpec spec in this.get_class ().list_properties ()) {
if ("::" in spec.get_nick ()) {
GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
l.insert (spec.name, spec.get_nick ());
}
}
return l;
}
/**
* Search for properties in objects, it should be
* an {@link GLib.Object}'s property. If found a
......
......@@ -364,6 +364,17 @@ public class GXml.XParser : Object, GXml.Parser {
if (size > 1500)
tw.flush ();
}
// GomObject serialisation
var opm = (node as GomObject).get_properties_map ();
foreach (string pk in opm.get_keys ()) {
string v = (node as GomObject).get_attribute (pk);
if (v == null) continue;
string pn = opm.lookup (pk);
size += tw.write_attribute (pn.replace ("::",""), v);
size += tw.end_attribute ();
if (size > 1500)
tw.flush ();
}
}
// Non Elements
#if DEBUG
......
......@@ -24,8 +24,9 @@ using GXml;
class GomSerializationTest : GXmlTest {
public class Book : GomElement {
[Description (nick="::Name")]
public string name { get; set; }
public Book () {
construct {
_local_name = "Book";
}
public string to_string () { return (_document as GomDocument).to_string (); }
......@@ -40,6 +41,10 @@ class GomSerializationTest : GXmlTest {
assert (b.get_attribute ("name") == "My Book");
s = b.to_string ();
GLib.message ("DOC:"+s);
foreach (ParamSpec spec in b.get_class ().list_properties ()) {
if ("::" in spec.get_nick ())
GLib.message ("Name: "+spec.name+ " Nick: "+spec.get_nick ());
}
});
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment