JSON-GLib Reference Manual: Boxed Types Serialization

## Boxed Types Serialization Boxed Types Serialization — Serialize and deserialize GBoxed types |

## Functions

JsonNode * | (*JsonBoxedSerializeFunc) ()
gpointer | (*JsonBoxedDeserializeFunc) ()
void | json_boxed_register_serialize_func ()
void | json_boxed_register_deserialize_func ()
gboolean | json_boxed_can_serialize ()
gboolean | json_boxed_can_deserialize ()
JsonNode * | json_boxed_serialize ()
gpointer | json_boxed_deserialize ()

## Description

GLib's GBoxed type is a generic wrapper for arbitrary C structures.

JSON-GLib allows serialization and deserialization of a GBoxed type by registering functions mapping a JsonNodeType to a specific GType.

When registering a GBoxed type you should also register the corresponding transformation functions, e.g.:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    GType
    my_struct_get_type (void)
    {
      static GType boxed_type = 0;

      if (boxed_type == 0)
        {
          boxed_type =
            g_boxed_type_register_static (g_intern_static_string ("MyStruct"),
                                          (GBoxedCopyFunc) my_struct_copy,
                                          (GBoxedFreeFunc) my_struct_free);

          json_boxed_register_serialize_func (boxed_type, JSON_NODE_OBJECT,
                                              my_struct_serialize);
          json_boxed_register_deserialize_func (boxed_type, JSON_NODE_OBJECT,
                                                my_struct_deserialize);
        }

      return boxed_type;
    }

The serialization function will be invoked by `json_boxed_serialize()`: it will be passed a pointer to the C structure and it must return a JsonNode. The deserialization function will be invoked by `json_boxed_deserialize()`: it will be passed a JsonNode for the declared type and it must return a newly allocated C structure.

It is possible to check whether a GBoxed type can be deserialized from a specific JsonNodeType, and whether a GBoxed can be serialized and to which specific JsonNodeType.

## Functions

### JsonBoxedSerializeFunc ()

    JsonNode *
    (*JsonBoxedSerializeFunc) (gconstpointer boxed);

Serializes the passed GBoxed and stores it inside a JsonNode

#### Parameters

boxed | a GBoxed |

#### Returns

the newly created JsonNode

Since 0.10

### JsonBoxedDeserializeFunc ()

    gpointer
    (*JsonBoxedDeserializeFunc) (JsonNode *node);

Deserializes the contents of the passed JsonNode into a GBoxed

#### Parameters

node | a JsonNode |

#### Returns

the newly created boxed type

Since 0.10

### json_boxed_register_serialize_func ()

    void
    json_boxed_register_serialize_func (GType gboxed_type,
                                        JsonNodeType node_type,
                                        JsonBoxedSerializeFunc serialize_func);

Registers a serialization function for a GBoxed of type `gboxed_type` to a JsonNode of type `node_type`

#### Parameters

gboxed_type | a boxed type |
node_type | a node type |
serialize_func | serialization function for `boxed_type` into a JsonNode of type `node_type` |

Since 0.10

### json_boxed_register_deserialize_func ()

    void
    json_boxed_register_deserialize_func (GType gboxed_type,
                                          JsonNodeType node_type,
                                          JsonBoxedDeserializeFunc deserialize_func);

Registers a deserialization function for a GBoxed of type `gboxed_type` from a JsonNode of type `node_type`

#### Parameters

gboxed_type | a boxed type |
node_type | a node type |
deserialize_func | deserialization function for `boxed_type` from a JsonNode of type `node_type` |

Since 0.10

### json_boxed_can_serialize ()

    gboolean
    json_boxed_can_serialize (GType gboxed_type,
                              JsonNodeType *node_type);

Checks whether it is possible to serialize a GBoxed of type `gboxed_type` into a JsonNode. The type of the JsonNode is placed inside `node_type` if the function returns `TRUE` and it's undefined otherwise.

#### Parameters

gboxed_type | a boxed type |
node_type | the JsonNode type to which the boxed type can be serialized into. | [out]

#### Returns

`TRUE` if the type can be serialized, and `FALSE` otherwise.

Since 0.10

### json_boxed_can_deserialize ()

    gboolean
    json_boxed_can_deserialize (GType gboxed_type,
                                JsonNodeType node_type);

Checks whether it is possible to deserialize a GBoxed of type `gboxed_type` from a JsonNode of type `node_type`

#### Parameters

gboxed_type | a boxed type |
node_type | a JsonNode type |

#### Returns

`TRUE` if the type can be deserialized, `FALSE` otherwise

Since 0.10

### json_boxed_serialize ()

    JsonNode *
    json_boxed_serialize (GType gboxed_type,
                          gconstpointer boxed);

Serializes `boxed` , a pointer to a GBoxed of type `gboxed_type` , into a JsonNode

#### Parameters

gboxed_type | a boxed type |
boxed | a pointer to a GBoxed of type `gboxed_type` |

#### Returns

a JsonNode with the serialization of the boxed type, or `NULL` if serialization either failed or was not possible.

[transfer full]

Since 0.10

### json_boxed_deserialize ()

    gpointer
    json_boxed_deserialize (GType gboxed_type,
                            JsonNode *node);

Deserializes `node` into a GBoxed of `gboxed_type`

#### Parameters

gboxed_type | a boxed type |
node | a JsonNode |

#### Returns

the newly allocated GBoxed. Use `g_boxed_free()` to release the resources allocated by this function.

[transfer full]

Since 0.10

## Types and Values