JSON GVariant Integration

JSON GVariant Integration — Serialize and deserialize GVariant types

Synopsis

JsonNode *          json_gvariant_serialize             (GVariant *variant);
gchar *             json_gvariant_serialize_data        (GVariant *variant,
                                                         gsize *length);
GVariant *          json_gvariant_deserialize           (JsonNode *json_node,
                                                         const gchar *signature,
                                                         GError **error);
GVariant *          json_gvariant_deserialize_data      (const gchar *json,
                                                         gssize length,
                                                         const gchar *signature,
                                                         GError **error);

Description

Use json_gvariant_serialize() and json_gvariant_serialize_data() to convert from any GVariant value to a JsonNode tree or its string representation.

Use json_gvariant_deserialize() and json_gvariant_deserialize_data() to obtain the GVariant value from a JsonNode tree or directly from a JSON string. Since many GVariant data types cannot be directly represented as JSON, a GVariant type string (signature) should be provided to these methods in order to obtain a correct, type-contrained result. If no signature is provided, conversion can still be done, but the resulting GVariant value will be "guessed" from the JSON data types, according to the following table:

Table 2. Default JSON to GVariant conversion (without signature constrains)

JSON GVariant
string string (s)
int64 int64 (x)
boolean boolean (b)
double double (d)
array array of variants (av)
object dictionary of string-variant (a{sv})
null maybe variant (mv)


Details

json_gvariant_serialize ()

JsonNode *          json_gvariant_serialize             (GVariant *variant);

Converts variant to a JSON tree.

variant :

A GVariant to convert

Returns :

A JsonNode representing the root of the JSON data structure obtained from variant. [transfer full]

Since 0.14


json_gvariant_serialize_data ()

gchar *             json_gvariant_serialize_data        (GVariant *variant,
                                                         gsize *length);

Converts variant to its JSON encoded string representation. This method is actually a helper function. It uses json_gvariant_serialize() to obtain the JSON tree, and then JsonGenerator to stringify it.

variant :

A GVariant to convert

length :

Return location for the length of the returned string, or NULL. [out][allow-none]

Returns :

The JSON encoded string corresponding to variant. [transfer full]

Since 0.14


json_gvariant_deserialize ()

GVariant *          json_gvariant_deserialize           (JsonNode *json_node,
                                                         const gchar *signature,
                                                         GError **error);

Converts a JSON data structure to a GVariant value using signature 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 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 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.

json_node :

A JsonNode to convert

signature :

A valid GVariant type string, or NULL. [allow-none]

error :

A pointer to a GError

Returns :

A newly created GVariant compliant with signature, or NULL on error. [transfer full]

Since 0.14


json_gvariant_deserialize_data ()

GVariant *          json_gvariant_deserialize_data      (const gchar *json,
                                                         gssize length,
                                                         const gchar *signature,
                                                         GError **error);

Converts a JSON string to a GVariant value. This method works exactly like json_gvariant_deserialize(), but takes a JSON encoded string instead. The string is first converted to a JsonNode using JsonParser, and then json_gvariant_deserialize() is called.

json :

A JSON data string

length :

The length of json, or -1 if NULL-terminated

signature :

A valid GVariant type string, or NULL. [allow-none]

error :

A pointer to a GError

Returns :

A newly created GVariant compliant with signature, or NULL on error. [transfer full]

Since 0.14