GObject integration

GObject integration — Serialize and deserialize GObjects

Synopsis

                    JsonSerializableIface;
JsonNode*           json_serializable_serialize_property
                                                        (JsonSerializable *serializable,
                                                         const gchar *property_name,
                                                         const GValue *value,
                                                         GParamSpec *pspec);
gboolean            json_serializable_deserialize_property
                                                        (JsonSerializable *serializable,
                                                         const gchar *property_name,
                                                         GValue *value,
                                                         GParamSpec *pspec,
                                                         JsonNode *property_node);

gchar*              json_serialize_gobject              (GObject *gobject,
                                                         gsize *length);
GObject*            json_construct_gobject              (GType gtype,
                                                         const gchar *data,
                                                         gsize length,
                                                         GError **error);

Description

JSON-GLib provides API for serializing and deserializing GObjects to and from JSON data streams.

Simple GObject classes can be (de)serialized into JSON objects, if the properties have compatible types with the native JSON types (integers, booleans, strings, string vectors). If the class to be (de)serialized has complex data types for properties (like boxed types or other objects) then the class should implement the provided JsonSerializable interface and its virtual functions.

Details

JsonSerializableIface

typedef struct {
  JsonNode *(* serialize_property)   (JsonSerializable *serializable,
                                      const gchar      *property_name,
                                      const GValue     *value,
                                      GParamSpec       *pspec);
  gboolean  (* deserialize_property) (JsonSerializable *serializable,
                                      const gchar      *property_name,
                                      GValue           *value,
                                      GParamSpec       *pspec,
                                      JsonNode         *property_node);
} JsonSerializableIface;

Interface that allows serializing and deserializing GObjects with properties storing complex data types. The json_serialize_gobject() function will check if the passed GObject implements this interface, so it can also be used to override the default property serialization sequence.

serialize_property ()

virtual function for serializing a GObject property into a JsonNode

deserialize_property ()

virtual function for deserializing a JsonNode into a GObject property

json_serializable_serialize_property ()

JsonNode*           json_serializable_serialize_property
                                                        (JsonSerializable *serializable,
                                                         const gchar *property_name,
                                                         const GValue *value,
                                                         GParamSpec *pspec);

Asks a JsonSerializable implementation to serialize a GObject property into a JsonNode object.

serializable :

a JsonSerializable object

property_name :

the name of the property

value :

the value of the property

pspec :

a GParamSpec

Returns :

a JsonNode containing the serialize property

json_serializable_deserialize_property ()

gboolean            json_serializable_deserialize_property
                                                        (JsonSerializable *serializable,
                                                         const gchar *property_name,
                                                         GValue *value,
                                                         GParamSpec *pspec,
                                                         JsonNode *property_node);

Asks a JsonSerializable implementation to deserialize the property contained inside property_node into value.

serializable :

a JsonSerializable

property_name :

the name of the property

value :

a pointer to an uninitialized GValue

pspec :

a GParamSpec

property_node :

a JsonNode containing the serialized property

Returns :

TRUE if the property was successfully deserialized.

json_serialize_gobject ()

gchar*              json_serialize_gobject              (GObject *gobject,
                                                         gsize *length);

Serializes a GObject into a JSON data stream. If gobject implements the JsonSerializableIface interface, it will be asked to serizalize all its properties; otherwise, the default implementation will be use to translate the compatible types into JSON native types.

gobject :

a GObject

length :

return value for the length of the buffer, or NULL

Returns :

a JSON data stream representing the passed GObject

json_construct_gobject ()

GObject*            json_construct_gobject              (GType gtype,
                                                         const gchar *data,
                                                         gsize length,
                                                         GError **error);

Deserializes a JSON data stream and creates the corresponding GObject class. If gtype implements the JsonSerializableIface interface, it will be asked to deserialize all the JSON members into the respective properties; otherwise, the default implementation will be used to translate the compatible JSON native types.

Note: the JSON data stream must be an object declaration.

gtype :

the GType of object to construct

data :

a JSON data stream

length :

length of the data stream, or -1 if it is NUL-terminated

error :

return location for a GError, or NULL

Returns :

a GObject or NULL

Since 0.4