Builder


Object Hierarchy:

Json.Builder Json.Builder Json.Builder GLib.Object GLib.Object GLib.Object->Json.Builder

Description:

[ CCode ( type_id = "json_builder_get_type ()" ) ]
public class Builder : Object

`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

Namespace: Json
Package: json-glib-1.0

Content:

Properties:

Creation methods:

Methods:

Inherited Members:

All known members inherited from class GLib.Object



2022 vala-language.org