Json.Builder¶
- Subclasses:
None
Methods¶
- Inherited:
- Structs:
class |
|
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
- Inherited:
Properties¶
Name |
Type |
Flags |
Short Description |
---|---|---|---|
r/w/co |
Whether the builder output is immutable. |
Signals¶
- Inherited:
Fields¶
- Inherited:
Name |
Type |
Access |
Description |
---|---|---|---|
parent_instance |
r |
Class Details¶
- class Json.Builder(**kwargs)¶
- Bases:
- Abstract:
No
- Structure:
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_autofreestr
*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:
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:
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
orNone
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
orNone
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
orNone
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
orNone
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
orNone
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
orNone
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
orNone
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
orNone
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
orNone
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
orNone
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 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
orNone
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.