JSON-GLib Reference Manual: JsonGenerator

## JsonGenerator JsonGenerator — Generates JSON data streams |

## Functions

JsonGenerator * | json_generator_new ()
void | json_generator_set_root ()
JsonNode * | json_generator_get_root ()
void | json_generator_set_pretty ()
gboolean | json_generator_get_pretty ()
void | json_generator_set_indent ()
guint | json_generator_get_indent ()
void | json_generator_set_indent_char ()
gunichar | json_generator_get_indent_char ()
gboolean | json_generator_to_file ()
gchar * | json_generator_to_data ()
gboolean | json_generator_to_stream ()

## Properties

guint | indent | Read / Write
guint | indent-char | Read / Write
gboolean | pretty | Read / Write
JsonNode * | root | Read / Write

## Types and Values

struct | JsonGenerator
struct | JsonGeneratorClass

## Object Hierarchy

        GObject
        ╰── JsonGenerator

## Description

JsonGenerator provides an object for generating a JSON data stream and put it into a buffer or a file.

## Functions

### json_generator_new ()

    JsonGenerator *
    json_generator_new (void);

Creates a new JsonGenerator. You can use this object to generate a JSON data stream starting from a data object model composed by JsonNodes.

#### Returns

the newly created JsonGenerator instance

### json_generator_set_root ()

    void
    json_generator_set_root (JsonGenerator *generator,
                             JsonNode *node);

Sets `node` as the root of the JSON data stream to be serialized by the JsonGenerator.

The passed `node` is copied by the generator object, so it can be safely freed after calling this function.

#### Parameters

generator | a JsonGenerator |
node | a JsonNode |

### json_generator_get_root ()

    JsonNode *
    json_generator_get_root (JsonGenerator *generator);

Retrieves a pointer to the root JsonNode set using `json_generator_set_root()`.

#### Parameters

generator | a JsonGenerator |

#### Returns

a JsonNode, or `NULL`. The returned node is owned by the JsonGenerator and it should not be freed.

[transfer none]

Since 0.14

### json_generator_set_pretty ()

    void
    json_generator_set_pretty (JsonGenerator *generator,
                               gboolean is_pretty);

Sets whether the generated JSON should be pretty printed, using the indentation character specified in the “indent-char” property and the spacing specified in “indent” property.

#### Parameters

generator | a JsonGenerator |
is_pretty | whether the generated string should be pretty printed |

Since 0.14

### json_generator_get_pretty ()

    gboolean
    json_generator_get_pretty (JsonGenerator *generator);

Retrieves the value set using `json_generator_set_pretty()`.

#### Parameters

generator | a JsonGenerator |

#### Returns

`TRUE` if the generated JSON should be pretty-printed, and `FALSE` otherwise

Since 0.14

### json_generator_set_indent ()

    void
    json_generator_set_indent (JsonGenerator *generator,
                               guint indent_level);

Sets the number of repetitions for each indentation level.

#### Parameters

generator | a JsonGenerator |
indent_level | the number of repetitions of the indentation character that should be applied when pretty printing |

Since 0.14

### json_generator_get_indent ()

    guint
    json_generator_get_indent (JsonGenerator *generator);

Retrieves the value set using `json_generator_set_indent()`.

#### Parameters

generator | a JsonGenerator |

#### Returns

the number of repetitions per indentation level

Since 0.14

### json_generator_set_indent_char ()

    void
    json_generator_set_indent_char (JsonGenerator *generator,
                                    gunichar indent_char);

Sets the character to be used when indenting

#### Parameters

generator | a JsonGenerator |
indent_char | a Unicode character to be used when indenting |

Since 0.14

### json_generator_get_indent_char ()

    gunichar
    json_generator_get_indent_char (JsonGenerator *generator);

Retrieves the value set using `json_generator_set_indent_char()`.

#### Parameters

generator | a JsonGenerator |

#### Returns

the character to be used when indenting

Since 0.14

### json_generator_to_file ()

    gboolean
    json_generator_to_file (JsonGenerator *generator,
                            const gchar *filename,
                            GError **error);

Creates a JSON data stream and puts it inside `filename` , overwriting the current file contents. This operation is atomic.

#### Parameters

generator | a JsonGenerator |
filename | path to the target file |
error | return location for a GError, or `NULL` |

#### Returns

`TRUE` if saving was successful.

### json_generator_to_data ()

    gchar *
    json_generator_to_data (JsonGenerator *generator,
                            gsize *length);

Generates a JSON data stream from `generator` and returns it as a buffer.

#### Parameters

generator | a JsonGenerator |
length | return location for the length of the returned buffer, or `NULL`. | [out]

#### Returns

a newly allocated buffer holding a JSON data stream. Use `g_free()` to free the allocated resources.

### json_generator_to_stream ()

    gboolean
    json_generator_to_stream (JsonGenerator *generator,
                              GOutputStream *stream,
                              GCancellable *cancellable,
                              GError **error);

Outputs JSON data and streams it (synchronously) to `stream` .

#### Parameters

generator | a JsonGenerator |
stream | a GOutputStream |
cancellable | a GCancellable, or `NULL`. | [allow-none]
error | return location for a GError, or `NULL` |

#### Returns

`TRUE` if the write operation was successful, and `FALSE` on failure. In case of error, the GError will be filled accordingly

Since 0.12

## Types and Values

### struct JsonGenerator

    struct JsonGenerator;

JSON data streams generator. The contents of the JsonGenerator structure are private and should only be accessed via the provided API.

### struct JsonGeneratorClass

    struct JsonGeneratorClass {
    };

JsonGenerator class

## Property Details

### The `“indent”` property

      “indent”                   guint

Number of spaces to be used to indent when pretty printing.

Flags: Read / Write

Default value: 2

### The `“indent-char”` property

      “indent-char”              guint

The character that should be used when indenting in pretty print.

Flags: Read / Write

Default value: ' '

Since 0.6

### The `“pretty”` property

      “pretty”                   gboolean

Whether the output should be "pretty-printed", with indentation and newlines. The indentation level can be controlled by using the JsonGenerator:indent property

Flags: Read / Write

Default value: FALSE

### The `“root”` property

      “root”                     JsonNode *

The root JsonNode to be used when constructing a JSON data stream.

Flags: Read / Write

Since 0.4