json.builder

Module for [Builder] class

Types 3

json.builder.Builder provides an object for generating a JSON tree.

The root of the JSON tree can be either a json.object.ObjectWrap or a json.array.Array. Thus the first call must necessarily be either json.builder.Builder.beginObject or json.builder.Builder.beginArray.

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

Using json.builder.Builder

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 ] }

Methods
GType _getGType() static nothrow
GType _gType() @property nothrow
Builder self() nothrowReturns `this`, for use in `with` statements.
BuilderGidBuilder builder() static nothrowGet builder for [json.builder.Builder] Returns: New builder object
bool immutable_() @property nothrowGet `immutable_` property. Returns: Whether the tree should be immutable when created.
json.builder.Builder newImmutable() static nothrowCreates a new, immutable [json.builder.Builder] instance.
json.builder.Builder addBooleanValue(bool value) nothrowAdds a boolean value to the currently open object member or array.
json.builder.Builder addDoubleValue(double value) nothrowAdds a floating point value to the currently open object member or array.
json.builder.Builder addIntValue(long value) nothrowAdds an integer value to the currently open object member or array.
json.builder.Builder addNullValue() nothrowAdds a null value to the currently open object member or array.
json.builder.Builder addStringValue(string value) nothrowAdds a boolean value to the currently open object member or array.
json.builder.Builder addValue(json.node.Node node) nothrowAdds a value to the currently open object member or array.
json.builder.Builder beginArray() nothrowOpens an array inside the given builder.
json.builder.Builder beginObject() nothrowOpens an object inside the given builder.
json.builder.Builder endArray() nothrowCloses the array inside the given builder that was opened by the most recent call to [json.builder.Builder.beginArray].
json.builder.Builder endObject() nothrowCloses the object inside the given builder that was opened by the most recent call to [json.builder.Builder.beginObject].
json.node.Node getRoot() nothrowReturns the root of the currently constructed tree.
void reset() nothrowResets the state of the builder back to its initial state.
json.builder.Builder setMemberName(string memberName) nothrowSets the name of the member in an object.
Constructors
this(void * ptr, Flag!"Take" take)
this()Creates a new [json.builder.Builder].

Fluent builder implementation template for json.builder.Builder

Methods
T immutable_(bool propval) nothrowSet `immutable_` property. Params: propval = Whether the tree should be immutable when created.

Fluent builder for json.builder.Builder

Methods
Builder build() nothrowCreate object from builder. Returns: New object