Callbacks

ArrayForeach (array, index_, element_node, *user_data)

BoxedDeserializeFunc (node)

BoxedSerializeFunc (boxed)

ObjectForeach (object, member_name, member_node, *user_data)

Details

Json.ArrayForeach(array, index_, element_node, *user_data)
Parameters:
  • array (Json.Array) – the iterated JSON array

  • index (int) – the index of the element

  • element_node (Json.Node) – the value of the element at the given index_

  • user_data (object or None) – data passed to the function

The function to be passed to [method`Json`.Array.foreach_element].

You should not add or remove elements to and from array within this function.

It is safe to change the value of element_node.

New in version 0.8.

Json.BoxedDeserializeFunc(node)
Parameters:

node (Json.Node) – a node tree representing a boxed data

Returns:

the newly created boxed structure

Return type:

object or None

Deserializes the contents of the passed JsonNode into a GBoxed, for instance:

```c static object my_point_deserialize (Json.Node *node) { double x = 0.0, y = 0.0;

if (JSON_NODE_HOLDS_ARRAY (node)) { Json.Array *array = Json.Node.get_array (node);

if (Json.Array.get_length (array) == 2) { x = Json.Array.get_double_element (array, 0); y = Json.Array.get_double_element (array, 1); } } else if (JSON_NODE_HOLDS_OBJECT (node)) { Json.Object *obj = Json.Node.get_object (node);

x = Json.Object.get_double_member_with_default (obj, “x”, 0.0); y = Json.Object.get_double_member_with_default (obj, “y”, 0.0); }

// my_point_new() is defined elsewhere return my_point_new (x, y); } ```

New in version 0.10.

Json.BoxedSerializeFunc(boxed)
Parameters:

boxed (object or None) – a boxed data structure

Returns:

the newly created JSON node tree representing the boxed data

Return type:

Json.Node

Serializes the passed GBoxed and stores it inside a JsonNode, for instance:

```c static Json.Node * my_point_serialize (gconstpointer boxed) { const MyPoint *point = boxed;

g_autoptr(Json.Builder) builder = Json.Builder.new ();

Json.Builder.begin_object (builder); Json.Builder.set_member_name (builder, “x”); Json.Builder.add_double_value (builder, point->x); Json.Builder.set_member_name (builder, “y”); Json.Builder.add_double_value (builder, point->y); Json.Builder.end_object (builder);

return Json.Builder.get_root (builder); } ```

New in version 0.10.

Json.ObjectForeach(object, member_name, member_node, *user_data)
Parameters:
  • object (Json.Object) – the iterated JSON object

  • member_name (str) – the name of the member

  • member_node (Json.Node) – the value of the member

  • user_data (object or None) – data passed to the function

The function to be passed to [method`Json`.Object.foreach_member].

You should not add or remove members to and from object within this function.

It is safe to change the value of member_node.

New in version 0.8.