Generator
Object Hierarchy:
Description:
[ CCode ( type_id = "json_generator_get_type ()" ) ]
public class Generator : Object
`JsonGenerator` provides an object for generating a JSON data stream from a tree of [struct@Json.
Node] instances, and put it into a buffer or a file.
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
Content:
Properties:
- public uint indent { get; set; }
Number of spaces to be used to indent when pretty printing.
- public uint indent_char { get; set; }
The character that should be used when indenting in pretty print.
- public bool pretty { get; set; }
Whether the output should be "pretty-printed", with indentation and
newlines.
- public Node root { get; set; }
The root node to be used when constructing a JSON data stream.
Creation methods:
Methods:
- public uint get_indent ()
Retrieves the value set using [method@Json.
- public unichar get_indent_char ()
Retrieves the value set using [method@Json.
- public bool get_pretty ()
Retrieves the value set using [method@Json.
- public unowned Node? get_root ()
Retrieves a pointer to the root node set using [method@Json.
- public void set_indent (uint indent_level)
Sets the number of repetitions for each indentation level.
- public void set_indent_char (unichar indent_char)
Sets the character to be used when indenting.
- public void set_pretty (bool is_pretty)
Sets whether the generated JSON should be pretty printed.
- public void set_root (Node node)
Sets the root of the JSON data stream to be serialized by the given
generator.
- public string to_data (out size_t length)
Generates a JSON data stream from this
and returns it as a buffer.
- public bool to_file (string filename) throws Error
Creates a JSON data stream and puts it inside `filename`, overwriting
the file's current contents.
- public unowned StringBuilder to_gstring (StringBuilder string)
Generates a JSON data stream and appends it to the string buffer.
- public bool to_stream (OutputStream stream, Cancellable? cancellable = null) throws Error
Outputs JSON data and writes it (synchronously) to the given stream.
Inherited Members:
All known members inherited from class GLib.Object
- @get
- @new
- @ref
- @set
- add_toggle_ref
- add_weak_pointer
- bind_property
- connect
- constructed
- disconnect
- dispose
- dup_data
- dup_qdata
- force_floating
- freeze_notify
- get_class
- get_data
- get_property
- get_qdata
- get_type
- getv
- interface_find_property
- interface_install_property
- interface_list_properties
- is_floating
- new_valist
- new_with_properties
- newv
- notify
- notify_property
- ref_count
- ref_sink
- remove_toggle_ref
- remove_weak_pointer
- replace_data
- replace_qdata
- set_data
- set_data_full
- set_property
- set_qdata
- set_qdata_full
- set_valist
- setv
- steal_data
- steal_qdata
- thaw_notify
- unref
- watch_closure
- weak_ref
- weak_unref