Json.Builder

g GObject.Object GObject.Object Json.Builder Json.Builder GObject.Object->Json.Builder

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

new ()

class

new_immutable ()

add_boolean_value (value)

add_double_value (value)

add_int_value (value)

add_null_value ()

add_string_value (value)

add_value (node)

begin_array ()

begin_object ()

end_array ()

end_object ()

get_root ()

reset ()

set_member_name (member_name)

Virtual Methods

Inherited:

GObject.Object (7)

Properties

Name

Type

Flags

Short Description

immutable

bool

r/w/co

Whether the builder output is immutable.

Signals

Inherited:

GObject.Object (1)

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent_instance

GObject.Object

r

Class Details

class Json.Builder(**kwargs)
Bases:

GObject.Object

Abstract:

No

Structure:

Json.BuilderClass

JsonBuilder provides an object for generating a JSON tree.

The root of the JSON tree can be either a [struct`Json`.Object] or a [struct`Json`.Array]. Thus the first call must necessarily be either [method`Json`.Builder.begin_object] or [method`Json`.Builder.begin_array].

For convenience to language bindings, most JsonBuilder method return the instance, making it easy to chain function calls.

Using Json.Builder

```c g_autoptr(Json.Builder) builder = Json.Builder.new ();

Json.Builder.begin_object (builder);

Json.Builder.set_member_name (builder, “url”); Json.Builder.add_string_value (builder, “http://www.gnome.org/img/flash/two-thirty.png”);

Json.Builder.set_member_name (builder, “size”); Json.Builder.begin_array (builder); Json.Builder.add_int_value (builder, 652); Json.Builder.add_int_value (builder, 242); Json.Builder.end_array (builder);

Json.Builder.end_object (builder);

g_autoptr(Json.Node) root = Json.Builder.get_root (builder);

g_autoptr(Json.Generator) gen = Json.Generator.new (); Json.Generator.set_root (gen, root); g_autofree str *str = Json.Generator.to_data (gen, None);

// str now contains the following JSON data // { “url” : “http://www.gnome.org/img/flash/two-thirty.png”, “size” : [ 652, 242 ] } ```

classmethod new()
Returns:

the newly created builder instance

Return type:

Json.Builder

Creates a new JsonBuilder.

You can use this object to generate a JSON tree and obtain the root node.

classmethod new_immutable()
Returns:

the newly create builder instance

Return type:

Json.Builder

Creates a new, immutable JsonBuilder instance.

It is equivalent to setting the [property`Json`.Builder:immutable] property set to TRUE at construction time.

New in version 1.2.

add_boolean_value(value)
Parameters:

value (bool) – the value of the member or element

Returns:

the builder instance

Return type:

Json.Builder or None

Adds a boolean value to the currently open object member or array.

If called after [method`Json`.Builder.set_member_name], sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: [method`Json`.Builder.add_value]

add_double_value(value)
Parameters:

value (float) – the value of the member or element

Returns:

the builder instance

Return type:

Json.Builder or None

Adds a floating point value to the currently open object member or array.

If called after [method`Json`.Builder.set_member_name], sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: [method`Json`.Builder.add_value]

add_int_value(value)
Parameters:

value (int) – the value of the member or element

Returns:

the builder instance

Return type:

Json.Builder or None

Adds an integer value to the currently open object member or array.

If called after [method`Json`.Builder.set_member_name], sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: [method`Json`.Builder.add_value]

add_null_value()
Returns:

the builder instance

Return type:

Json.Builder or None

Adds a null value to the currently open object member or array.

If called after [method`Json`.Builder.set_member_name], sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: [method`Json`.Builder.add_value]

add_string_value(value)
Parameters:

value (str) – the value of the member or element

Returns:

the builder instance

Return type:

Json.Builder or None

Adds a boolean value to the currently open object member or array.

If called after [method`Json`.Builder.set_member_name], sets the given value as the value of the current member in the open object; otherwise, the value is appended to the elements of the open array.

See also: [method`Json`.Builder.add_value]

add_value(node)
Parameters:

node (Json.Node) – the value of the member or element

Returns:

the builder instance

Return type:

Json.Builder or None

Adds a value to the currently open object member or array.

If called after [method`Json`.Builder.set_member_name], sets the given node as the value of the current member in the open object; otherwise, the node is appended to the elements of the open array.

The builder will take ownership of the node.

begin_array()
Returns:

the builder instance

Return type:

Json.Builder or None

Opens an array inside the given builder.

You can add a new element to the array by using [method`Json`.Builder.add_value].

Once you added all elements to the array, you must call [method`Json`.Builder.end_array] to close the array.

begin_object()
Returns:

the builder instance

Return type:

Json.Builder or None

Opens an object inside the given builder.

You can add a new member to the object by using [method`Json`.Builder.set_member_name], followed by [method`Json`.Builder.add_value].

Once you added all members to the object, you must call [method`Json`.Builder.end_object] to close the object.

If the builder is in an inconsistent state, this function will return NULL.

end_array()
Returns:

the builder instance

Return type:

Json.Builder or None

Closes the array inside the given builder that was opened by the most recent call to [method`Json`.Builder.begin_array].

This function cannot be called after [method`Json`.Builder.set_member_name].

end_object()
Returns:

the builder instance

Return type:

Json.Builder or None

Closes the object inside the given builder that was opened by the most recent call to [method`Json`.Builder.begin_object].

This function cannot be called after [method`Json`.Builder.set_member_name].

get_root()
Returns:

the root node

Return type:

Json.Node or None

Returns the root of the currently constructed tree.

if the build is incomplete (ie: if there are any opened objects, or any open object members and array elements) then this function will return NULL.

reset()

Resets the state of the builder back to its initial state.

set_member_name(member_name)
Parameters:

member_name (str) – the name of the member

Returns:

the builder instance

Return type:

Json.Builder or None

Sets the name of the member in an object.

This function must be followed by of these functions:

  • [method`Json`.Builder.add_value], to add a scalar value to the member

  • [method`Json`.Builder.begin_object], to add an object to the member

  • [method`Json`.Builder.begin_array], to add an array to the member

This function can only be called within an open object.

Property Details

Json.Builder.props.immutable
Name:

immutable

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

Whether the tree should be immutable when created.

Making the output immutable on creation avoids the expense of traversing it to make it immutable later.

New in version 1.2.