`JsonBuilder` provides an object for generating a JSON tree.
The root of the JSON tree can be either a [[email protected]] or a [[email protected]]. Thus the first call must necessarily be either [
[email protected]_object] or [[email protected]_array].
For convenience to language bindings, most `JsonBuilder` method return the instance, making it easy to chain function calls.
Using `JsonBuilder`
```c g_autoptr(JsonBuilder) 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(JsonNode) root = json_builder_get_root (builder);
g_autoptr(JsonGenerator) gen = json_generator_new (); json_generator_set_root (gen, root); g_autofree char *str = json_generator_to_data
(gen, NULL);
// str now contains the following JSON data // { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] } ```
Example: Builder & Generators:
static int main (string[] args) {
// Build a object:
Json.Builder builder = new Json.Builder ();
builder.begin_object ();
builder.set_member_name ("url");
builder.add_string_value ("http://www.gnome.org/img/flash/two-thirty.png");
builder.set_member_name ("size");
builder.begin_array ();
builder.add_int_value (652);
builder.add_int_value (242);
builder.end_array ();
builder.end_object ();
// Generate a string:
// { "url" : "http://www.gnome.org/img/flash/two-thirty.png", "size" : [ 652, 242 ] }
Json.Generator generator = new Json.Generator ();
Json.Node root = builder.get_root ();
generator.set_root (root);
string str = generator.to_data (null);
print (str);
print ("\n");
return 0;
}
valac --pkg json-glib-1.0 builder-generator.vala
- public unowned Builder? add_boolean_value (bool value)
Adds a boolean value to the currently open object member or array.
- public unowned Builder? add_double_value (double value)
Adds a floating point value to the currently open object member or
array.
- public unowned Builder? add_int_value (int64 value)
Adds an integer value to the currently open object member or array.
- public unowned Builder? add_null_value ()
Adds a null value to the currently open object member or array.
- public unowned Builder? add_string_value (string value)
Adds a boolean value to the currently open object member or array.
- public unowned Builder? add_value (owned Node node)
Adds a value to the currently open object member or array.
- public unowned Builder? begin_array ()
Opens an array inside the given builder.
- public unowned Builder? begin_object ()
Opens an object inside the given builder.
- public unowned Builder? end_array ()
Closes the array inside the given builder that was opened by the most
recent call to [method@Json.
- public unowned Builder? end_object ()
Closes the object inside the given builder that was opened by the most
recent call to [method@Json.
- public Node? get_root ()
Returns the root of the currently constructed tree.
- public void reset ()
Resets the state of the builder back to its initial state.
- public unowned Builder? set_member_name (string member_name)
Sets the name of the member in an object.