The Google Summer of Code 2012 now comes to a close, so to celebrate, GXml 0.3.0 has been tagged with GObject Serialization as its big feature.
The last few days have had been trying to find a clever way to reliably identify some types of collections, but that's not finished, so they're not being automatically serialized in this release.
I've also bravely fought against autotools to try to figure out how to minimise the number of places where I have to update a version number. Too much of autotools remain a mystery to me, sadly, but I think I got it? It works for me: hopefully someone will file a bug if something doesn't work for them.
Summary
- Serialization class
- that can automatically handle many types
- that can deserialise to types known on the local system and distinguish multiple references to the same object. I have some future plans to remove redundant XML that gets generated, too.
- Serializable interface
- allows classes to override default serialization and serialize non-public properties too. Yay!
- follows json-glib so that a common serialization interface can be defined in the future
- Documentation
- successfully got Valadoc to generate GtkDoc (and fixed a lot of documentation in GXml from before)
- Testing
- created test suites for serialization features
- API breakage
- namespaces were changed, in part to reduce the amount of code C programmers have to write.
Vala Example
Automatic serialization
GXml.DomNode xml = GXml.Serialization.serialize_object (banana);
...
banana = GXml.Serialization.deserialize_object (xml);
Implementing the interface: overriding how a property is serialized
public class Banana : GLib.Object, GXml.Serializable {
public int weight { get; set; }
public GXml.DomNode? serialize_property (string prop_name, GLib.ParamSpec spec,
GXml.Document doc) {
switch (prop_name) {
case "weight":
return doc.create_text_node ("%d".printf (weight * 1000));
default:
return null;
}
}
public bool deserialize_property (string prop_name, GLib.ParamSpec spec,
GXml.DomNode prop_node) {
GLib.Value outvalue = GLib.Value (typeof (int));
switch (prop_name) {
case "weight":
int64.try_parse (((GXml.Element)prop_node).content, out outvalue);
this.weight = (int)outvalue.get_int64 () / 1000;
return true;
default:
return false;
}
}
}
Implementing the interface: override view of properties
using GXml;
public class Plantain : GLib.Object, GXml.Serializable {
private int weight { get; set; }
private ParamSpec[] _props = null;
public unowned ParamSpec[] list_properties () {
if (this._props == null) {
this._props = new ParamSpec[1];
this._props[0] = new ParamSpecInt ("weight", "weight", "weight",
0, 200, 0, ParamFlags.READABLE);
}
return this._props;
}
private ParamSpec prop;
public unowned ParamSpec? find_property (string prop_name) {
ParamSpec[] props = this.list_properties ();
foreach (ParamSpec prop in props) {
if (prop.name == prop_name) {
this.prop = prop;
return this.prop;
}
}
return null;
}
public new void get_property (ParamSpec spec, ref Value str_value) {
Value value = Value (typeof (int));
if (spec.name == "weight")
value.set_int (this.weight);
else {
((GLib.Object)this).get_property (spec.name, ref str_value);
return;
}
value.transform (ref str_value);
return;
}
public new void set_property (ParamSpec spec, Value value) {
if (spec.name == "weight")
this.weight = value.get_int ();
else {
((GLib.Object)this).set_property (spec.name, value);
}
}
}
...
Challenges
These are the greatest challenges I've dealt with
- my masters: there's a lot of pressure to hurry up and finish; ideally, a GSOC should be the sole or primary focus of my summer, but I don't have much opportunity to take a summer off right now. Going to GUADEC was amazing, but consequently made my masters a bigger part of these past two weeks.
- autotools: a lot of feedback centred on people wanting autotools instead of WAF, so now you have and now I cry myself to sleep at night. I just need to make it a bigger part of my life.
- valadoc: I was having issues getting valadoc to correctly generate GtkDoc last year and this year until Philip Withnall directed me to libfolks.
Future
- want to support automatic serialization of more complex types; I've been working on collections but there are problems with it that prevent me from committing it.
- two projects approached me at GUADEC suggesting GXml for saving data to disk, so I am going to write patches for them. :D
- examples in other languages
Download tar ball: http://ftp.gnome.org/pub/GNOME/sources/gxml/0.3/
Keine Kommentare:
Kommentar veröffentlichen