JSON-GLib Reference Manual: GObject Serialization

## GObject Serialization GObject Serialization — Serialize and deserialize GObjects |

## Functions

JsonNode * | json_gobject_serialize ()
GObject * | json_gobject_deserialize ()
gchar * | json_gobject_to_data ()
GObject * | json_gobject_from_data ()
GObject * | json_construct_gobject ()
gchar * | json_serialize_gobject ()

## Description

JSON-GLib provides API for serializing and deserializing GObject instances 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.

## Functions

### json_gobject_serialize ()

    JsonNode *
    json_gobject_serialize (GObject *gobject);

Creates a JsonNode representing the passed GObject instance. Each member of the returned JSON object will map to a property of the GObject

#### Parameters

gobject | a GObject |

#### Returns

the newly created JsonNode of type `JSON_NODE_OBJECT`. Use `json_node_free()` to free the resources allocated by this function.

[transfer full]

Since 0.10

### json_gobject_deserialize ()

    GObject *
    json_gobject_deserialize (GType gtype,
                              JsonNode *node);

Creates a new GObject of type `gtype` , and constructs it using the members of the passed JsonObject

#### Parameters

gtype | the type of the GObject to create |
node | a JsonNode of type `JSON_NODE_OBJECT` describing the instance of type `gtype` |

#### Returns

The newly created GObject instance. Use `g_object_unref()` to free the resources allocated by this function.

[transfer full]

Since 0.10

### json_gobject_to_data ()

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

Serializes a GObject into a JSON data stream, iterating recursively over each property.

If `gobject` implements the JsonSerializableIface interface, it will be asked to serialize all its properties; otherwise, the default implementation will be use to translate the compatible types into JSON native types.

#### Parameters

gobject | a GObject |
length | return value for the length of the buffer, or `NULL`. | [out]

#### Returns

a JSON data stream representing the passed GObject

Since 0.10

### json_gobject_from_data ()

    GObject *
    json_gobject_from_data (GType gtype,
                            const gchar *data,
                            gssize 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.

#### Parameters

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`.

[transfer full]

Since 0.10

### json_construct_gobject ()

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

`json_construct_gobject` has been deprecated since version 0.10 and should not be used in newly-written code.

Use `json_gobject_from_data()` instead

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.

#### Parameters

gtype | the GType of object to construct |
data | a JSON data stream |
length | length of the data stream |
error | return location for a GError, or `NULL` |

#### Returns

a GObject or `NULL`.

[transfer full]

Since 0.4

### json_serialize_gobject ()

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

`json_serialize_gobject` has been deprecated since version 0.10 and should not be used in newly-written code.

Use `json_gobject_to_data()` instead

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.

#### Parameters

gobject | a GObject |
length | return value for the length of the buffer, or `NULL`. | [out]

#### Returns

a JSON data stream representing the passed GObject

## Types and Values