Json.Generator

g GObject.Object GObject.Object Json.Generator Json.Generator GObject.Object->Json.Generator

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

new ()

get_indent ()

get_indent_char ()

get_pretty ()

get_root ()

set_indent (indent_level)

set_indent_char (indent_char)

set_pretty (is_pretty)

set_root (node)

to_data ()

to_file (filename)

to_gstring (string)

to_stream (stream, cancellable)

Virtual Methods

Inherited:

GObject.Object (7)

Properties

Name

Type

Flags

Short Description

indent

int

r/w

Number of indentation spaces

indent-char

int

r/w

Character that should be used when indenting

pretty

bool

r/w

Pretty-print the output

root

Json.Node

r/w

Root of the JSON data tree

Signals

Inherited:

GObject.Object (1)

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent_instance

GObject.Object

r

Class Details

class Json.Generator(**kwargs)
Bases:

GObject.Object

Abstract:

No

Structure:

Json.GeneratorClass

JsonGenerator provides an object for generating a JSON data stream from a tree of [struct`Json`.Node] instances, and put it into a buffer or a file.

classmethod new()
Returns:

the newly created generator instance

Return type:

Json.Generator

Creates a new JsonGenerator.

You can use this object to generate a JSON data stream starting from a data object model composed by [struct`Json`.Node]s.

get_indent()
Returns:

the number of repetitions per indentation level

Return type:

int

Retrieves the value set using [method`Json`.Generator.set_indent].

New in version 0.14.

get_indent_char()
Returns:

the character to be used when indenting

Return type:

str

Retrieves the value set using [method`Json`.Generator.set_indent_char].

New in version 0.14.

get_pretty()
Returns:

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

Return type:

bool

Retrieves the value set using [method`Json`.Generator.set_pretty].

New in version 0.14.

get_root()
Returns:

the root node

Return type:

Json.Node or None

Retrieves a pointer to the root node set using [method`Json`.Generator.set_root].

New in version 0.14.

set_indent(indent_level)
Parameters:

indent_level (int) – the number of repetitions of the indentation character that should be applied when pretty printing

Sets the number of repetitions for each indentation level.

New in version 0.14.

set_indent_char(indent_char)
Parameters:

indent_char (str) – a Unicode character to be used when indenting

Sets the character to be used when indenting.

New in version 0.14.

set_pretty(is_pretty)
Parameters:

is_pretty (bool) – whether the generated string should be pretty printed

Sets whether the generated JSON should be pretty printed.

Pretty printing will use indentation character specified in the [property`Json`.Generator:indent-char] property and the spacing specified in the [property`Json`.Generator:indent] property.

New in version 0.14.

set_root(node)
Parameters:

node (Json.Node) – the root node

Sets the root of the JSON data stream to be serialized by the given generator.

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

to_data()
Returns:

a newly allocated string holding a JSON data stream

length:

return location for the length of the returned buffer

Return type:

(str, length: int)

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

to_file(filename)
Parameters:

filename (str) – the path to the target file

Raises:

GLib.Error

Returns:

True if saving was successful.

Return type:

bool

Creates a JSON data stream and puts it inside filename, overwriting the file’s current contents.

This operation is atomic, in the sense that the data is written to a temporary file which is then renamed to the given filename.

to_gstring(string)
Parameters:

string (GLib.String) – a string buffer

Returns:

the passed string, updated with the generated JSON data

Return type:

GLib.String

Generates a JSON data stream and appends it to the string buffer.

New in version 1.4.

to_stream(stream, cancellable)
Parameters:
Raises:

GLib.Error

Returns:

whether the write operation was successful

Return type:

bool

Outputs JSON data and writes it (synchronously) to the given stream.

New in version 0.12.

Property Details

Json.Generator.props.indent
Name:

indent

Type:

int

Default Value:

2

Flags:

READABLE, WRITABLE

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

Json.Generator.props.indent_char
Name:

indent-char

Type:

int

Default Value:

32

Flags:

READABLE, WRITABLE

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

New in version 0.6.

Json.Generator.props.pretty
Name:

pretty

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE

Whether the output should be “pretty-printed”, with indentation and newlines.

The indentation level can be controlled by using the [property`Json`.Generator:indent] property.

Json.Generator.props.root
Name:

root

Type:

Json.Node

Default Value:

None

Flags:

READABLE, WRITABLE

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

New in version 0.4.