JSON GVariant Integration

## JSON GVariant Integration JSON GVariant Integration — Serialize and deserialize GVariant types |

## Functions

JsonNode * | json_gvariant_serialize ()
gchar * | json_gvariant_serialize_data ()
GVariant * | json_gvariant_deserialize ()
GVariant * | json_gvariant_deserialize_data ()

## 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 using the following rules:

#### Strings

JSON strings map to GVariant `(s)`.

#### Integers

JSON integers map to GVariant int64 `(x)`.

#### Booleans

JSON booleans map to GVariant boolean `(b)`.

#### Numbers

JSON numbers map to GVariant double `(d)`.

#### Arrays

JSON arrays map to GVariant arrays of variants `(av)`.

#### Objects

JSON objects map to GVariant dictionaries of string to variants `(a{sv})`.

#### Null values

JSON null values map to GVariant maybe variants `(mv)`.

## Functions

### json_gvariant_serialize ()

    JsonNode *
    json_gvariant_serialize (GVariant *variant);

Converts `variant` to a JSON tree.

#### Parameters

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.

#### Parameters

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.

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

#### Parameters

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, floating GVariant compliant with `signature` , or `NULL` on error.

[transfer none]

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.

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

#### Parameters

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, floating GVariant compliant with `signature` , or `NULL` on error.

[transfer none]

Since: 0.14

## Types and Values