Functions

boxed_can_deserialize (gboxed_type, node_type)

boxed_can_serialize (gboxed_type)

boxed_deserialize (gboxed_type, node)

boxed_serialize (gboxed_type, boxed)

construct_gobject (gtype, data, length)

from_string (str)

gobject_deserialize (gtype, node)

gobject_from_data (gtype, data, length)

gobject_serialize (gobject)

gobject_to_data (gobject)

gvariant_deserialize (json_node, signature)

gvariant_deserialize_data (json, length, signature)

gvariant_serialize (variant)

gvariant_serialize_data (variant)

parser_error_quark ()

path_error_quark ()

reader_error_quark ()

serialize_gobject (gobject)

string_compare (a, b)

string_equal (a, b)

string_hash (key)

to_string (node, pretty)

Details

Json.boxed_can_deserialize(gboxed_type, node_type)
Parameters:
Returns:

TRUE if the type can be deserialized, and FALSE otherwise

Return type:

bool

Checks whether it is possible to deserialize a GBoxed of type gboxed_type from a [struct`Json`.Node] of type node_type.

New in version 0.10.

Json.boxed_can_serialize(gboxed_type)
Parameters:

gboxed_type (GObject.GType) – a boxed type

Returns:

TRUE if the type can be serialized, and FALSE otherwise

node_type:

the node type to which the boxed type can be serialized into

Return type:

(bool, node_type: Json.NodeType)

Checks whether it is possible to serialize a GBoxed of type gboxed_type into a [struct`Json`.Node].

The type of the node is placed inside node_type if the function returns TRUE, and it’s undefined otherwise.

New in version 0.10.

Json.boxed_deserialize(gboxed_type, node)
Parameters:
Returns:

the newly allocated boxed data

Return type:

object or None

Deserializes the given [struct`Json`.Node] into a GBoxed of the given type.

New in version 0.10.

Json.boxed_serialize(gboxed_type, boxed)
Parameters:
Returns:

a node with the serialized boxed type

Return type:

Json.Node or None

Serializes a pointer to a GBoxed of the given type into a [struct`Json`.Node].

If the serialization is not possible, this function will return NULL.

New in version 0.10.

Json.construct_gobject(gtype, data, length)
Parameters:
  • gtype (GObject.GType) – the type of the object to construct

  • data (str) – a JSON data stream

  • length (int) – length of the data stream (unused)

Raises:

GLib.Error

Returns:

a new object instance of the given type

Return type:

GObject.Object or None

Deserializes a JSON data stream and creates an instance of the given type.

If the given type implements the [iface`Json`.Serializable] interface, it will be asked to deserialize all the JSON members into their 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.

For historical reasons, the length argument is unused. The given data must be a NUL-terminated string.

New in version 0.4.

Deprecated since version 0.10: Use [func`Json`.gobject_from_data] instead

Json.from_string(str)
Parameters:

str (str) – a valid UTF-8 string containing JSON data

Raises:

GLib.Error

Returns:

the root node of the JSON tree

Return type:

Json.Node or None

Parses the given string and returns the corresponding JSON tree.

If the string is empty, this function will return NULL.

In case of parsing error, this function returns NULL and sets the error appropriately.

New in version 1.2.

Json.gobject_deserialize(gtype, node)
Parameters:
  • gtype (GObject.GType) – the type of the object to create

  • node (Json.Node) – a node of type JSON_NODE_OBJECT describing the object instance for the given type

Returns:

The newly created instance

Return type:

GObject.Object

Creates a new GObject instance of the given type, and constructs it using the members of the object in the given node.

New in version 0.10.

Json.gobject_from_data(gtype, data, length)
Parameters:
  • gtype (GObject.GType) – the type of the object to construct

  • data (str) – a JSON data stream

  • length (int) – length of the data stream, or -1 if it is NUL-terminated

Raises:

GLib.Error

Returns:

a new object instance of the given type

Return type:

GObject.Object or None

Deserializes a JSON data stream and creates an instance of the given type.

If the type implements the [iface`Json`.Serializable] interface, it will be asked to deserialize all the JSON members into their 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

New in version 0.10.

Json.gobject_serialize(gobject)
Parameters:

gobject (GObject.Object) – the object to serialize

Returns:

the newly created JSON tree

Return type:

Json.Node

Creates a JSON tree representing the passed object instance.

Each member of the returned JSON object will map to a property of the object type.

The returned JSON tree will be returned as a JsonNode with a type of JSON_NODE_OBJECT.

New in version 0.10.

Json.gobject_to_data(gobject)
Parameters:

gobject (GObject.Object) – the object to serialize

Returns:

a JSON data stream representing the given object

length:

return value for the length of the buffer

Return type:

(str, length: int)

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

If the given object implements the [iface`Json`.Serializable] 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.

New in version 0.10.

Json.gvariant_deserialize(json_node, signature)
Parameters:
  • json_node (Json.Node) – the node to convert

  • signature (str or None) – a valid GVariant type string

Raises:

GLib.Error

Returns:

A newly created GVariant

Return type:

GLib.Variant or None

Converts a JSON data structure to a GVariant.

If signature is not NULL, it will be used to resolve ambiguous data types.

If no error occurs, the resulting GVariant is guaranteed to conform to signature.

If signature is not NULL but does not represent a valid GVariant type string, NULL is returned and the error is set to G_IO_ERROR_INVALID_ARGUMENT.

If a signature is provided but the JSON structure cannot be mapped to it, NULL is returned and the error is set to G_IO_ERROR_INVALID_DATA.

If signature is NULL, the conversion is done based strictly on the types in the JSON nodes.

The returned variant has a floating reference that will need to be sunk by the caller code.

New in version 0.14.

Json.gvariant_deserialize_data(json, length, signature)
Parameters:
  • json (str) – A JSON data string

  • length (int) – The length of json, or -1 if NUL-terminated

  • signature (str or None) – A valid GVariant type string

Raises:

GLib.Error

Returns:

A newly created ``GVariant``D compliant

Return type:

GLib.Variant or None

Converts a JSON string to a GVariant value.

This function works exactly like [func`Json`.gvariant_deserialize], but takes a JSON encoded string instead.

The string is first converted to a [struct`Json`.Node] using [class`Json`.Parser], and then json_gvariant_deserialize is called on the node.

The returned variant has a floating reference that will need to be sunk by the caller code.

New in version 0.14.

Json.gvariant_serialize(variant)
Parameters:

variant (GLib.Variant) – A GVariant to convert

Returns:

the root of the JSON data structure obtained from variant

Return type:

Json.Node

Converts variant to a JSON tree.

New in version 0.14.

Json.gvariant_serialize_data(variant)
Parameters:

variant (GLib.Variant) – A GLib.Variant to convert

Returns:

The JSON encoded string corresponding to the given variant

length:

the length of the returned string

Return type:

(str, length: int)

Converts variant to its JSON encoded string representation.

This is a convenience function around [func`Json`.gvariant_serialize], to obtain the JSON tree, and then [class`Json`.Generator] to stringify it.

New in version 0.14.

Json.parser_error_quark()
Return type:

int

Json.path_error_quark()
Return type:

int

Json.reader_error_quark()
Return type:

int

Json.serialize_gobject(gobject)
Parameters:

gobject (GObject.Object) – the object to serialize

Returns:

a JSON data stream representing the given object

length:

return value for the length of the buffer

Return type:

(str, length: int)

Serializes a GObject instance into a JSON data stream.

If the object implements the [iface`Json`.Serializable] 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.

Deprecated since version 0.10: Use [func`Json`.gobject_to_data] instead

Json.string_compare(a, b)
Parameters:
  • a (str) – a JSON string

  • b (str) – another JSON string

Returns:

an integer less than zero if a < b, equal to zero if a == b, and greater than zero if a > b

Return type:

int

Check whether a and b are equal UTF-8 JSON strings and return an ordering over them in strcmp() style.

New in version 1.2.

Json.string_equal(a, b)
Parameters:
  • a (str) – a JSON string

  • b (str) – another JSON string

Returns:

TRUE if a and b are equal; FALSE otherwise

Return type:

bool

Check whether a and b are equal UTF-8 JSON strings.

New in version 1.2.

Json.string_hash(key)
Parameters:

key (str) – a JSON string to hash

Returns:

hash value for key

Return type:

int

Calculate a hash value for the given key (a UTF-8 JSON string).

Note: Member names are compared byte-wise, without applying any Unicode decomposition or normalisation. This is not explicitly mentioned in the JSON standard (ECMA-404), but is assumed.

New in version 1.2.

Json.to_string(node, pretty)
Parameters:
  • node (Json.Node) – a JSON tree

  • pretty (bool) – whether the output should be prettyfied for printing

Returns:

the string representation of the node

Return type:

str

Generates a stringified JSON representation of the contents of the given node.

New in version 1.2.