HowTo: GObject Serialization to XML using GXml

While GXml has C API, it is written in Vala, then this posts will show you how to use GXml’s Vala API to get GObject to XML and back easy.

Lets start with a simple class and GXml’s implementation of GXml.Serializable, called GXml.SerializableObjectModel:

using GXml;

public class Record : GXml.SerializableObjectModel {
  public string name { get; set; }

  // GXml.SerializableObjectModel overrides
  public override string node_name () { return "Record"; }
  public override string to_string () { return @"Record: $name"; }
}

The above example, defines a class called Record witch should be written in XML as:

<Record name="Daniel"/>

This just happen if Record.name is set, if no you’ll get:

<Record/>

In order to get this XML to be written you’ll need:

var r = new Record ();
r.name = "Daniel";
var d = new GDocument ();
r.serialize (d);
stdout.printf (@"$d");

The above code will print an XML representation of your object, with the given values, as shown above.

If you have an XML file or string, you can use following to read data from:

var r = new Record ();
var d = new GDocument.from_string ("<Record name=\"Jhon\"/>");
r.deserialize (d);
stdout.printf (@"$r");

The above code will print your object string representation using Record.to_string().

GXml.SerializableObjectModel will inspect all your public properties, trying to print out string representation of each. Most basic types are sopported, while you can override default GXml.SerializableObjectModel.serialize_property() in order to catch up when an special property like GLib.List is corrently serialize to create your custom serialization; if you do so, remember to override GXml.SerializableObjectModel.deserialize_property() to get back string to your property. Remember, GXml.SerializableObjectModel is just an implementation of GXml.Serializable interface and you can both, override default methods or implement your own.

Next time we can review how a set of XML tags could be de/serialized from XML using builtin GXml serializable collections.

XML GObject Serialization

GXml is a library, written in Vala, providing a good support to read and write XML files. Behind it you’ll find libxml2, wrapped by GObject classes.

Initially written by Richard Schwarting, to provide DOM Level 3 support on libxml2, now it has a set of interfaces, making easy to write different backends. While today just libxm2 is used, GXml have three GObject classes, one in with the original DOM support, one optimized to gain better use of libmxl2 and one in pure GObject. All of them allows you to access XML objects, like tags and properties, navigate over XML tree and modify if you need it. Each implementation of general purpose interfaces, like GXml.Node, have its own advantanges: DOM support, low memory foot print or better performance with larger foot print. Is a matter of you on what implementation you need.

My motivation on GXml, was initially pushed to have a low C library to support reading Substation Configuration Language files, IEC 61850-6 standard XML based files to energy related devices configuration. This files are large and complex. While there are a free open source project to handle them by LibreSCL, in order to have it working, GXml should gain GObject to XML serialization/deserialization. This has lead to commercial products, running both on Linux and Windows.

I’ll write down about how GXml serialization can be used for GObject Serialization, wait second posts.

GDA becomming better introspectable

GNOME Data Access (GDA), is a C library for Database access, it provides a library to write clients and a set GTK+ widgets to help write GUI applications; it also provides a control center, a SQL browser and CLI a la PostgreSQL’s psql command.

It is really useful, but could be better if most of its advanced futures are available for GObject Introspection (GI). While GDA has support for GI for a while, there are some structures in C and some API witch has been marked as no-introspectable, automatically or hard coded.

Making a C library introspectable is a little easy, but requires a lot of work, by hiding structs members and provide getters and setters methods for its members (sealing); make sure you are handling correctly struct’s memory management, freeing on request, but most importantly, is to convert this struct as a GBoxed type, making easy for bindings to create, read/write members (using its setter/getter), free, copy  and pack it to a GValue.

The hard work on making a struct introspectable, is to substituted all direct access to its members to use its new API. This is a hard work as you can see when GdaBinary was sealed to make it introspectable.

You can follow GDA Instrospectable bug and its bug dependencies, in order to know how has been solved each case; may that will help you to create better C introspectable API or help us in the process.