| JSON-GLib Reference Manual | ||||
|---|---|---|---|---|
| Top | Description | Object Hierarchy | Properties | ||||
Synopsis
struct JsonGenerator; struct JsonGeneratorClass; JsonGenerator * json_generator_new (void); gboolean json_generator_to_file (JsonGenerator *generator,const gchar *filename,GError **error); gchar * json_generator_to_data (JsonGenerator *generator,gsize *length); gboolean json_generator_to_stream (JsonGenerator *generator,GOutputStream *stream,GCancellable *cancellable,GError **error); void json_generator_set_root (JsonGenerator *generator,JsonNode *node);
Properties
"indent" guint : Read / Write "indent-char" guint : Read / Write "pretty" gboolean : Read / Write "root" JsonNode* : Read / Write
Description
JsonGenerator provides an object for generating a JSON data stream and put it into a buffer or a file.
Details
struct JsonGenerator
struct JsonGenerator;
JSON data streams generator. The contents of the JsonGenerator structure are private and should only be accessed via the provided API.
json_generator_new ()
JsonGenerator * json_generator_new (void);
Creates a new JsonGenerator. You can use this object to generate a JSON data stream starting from a data object model composed by JsonNodes.
Returns : |
the newly created JsonGenerator instance |
json_generator_to_file ()
gboolean json_generator_to_file (JsonGenerator *generator,const gchar *filename,GError **error);
Creates a JSON data stream and puts it inside filename, overwriting the
current file contents. This operation is atomic.
|
a JsonGenerator |
|
path to the target file |
|
return location for a GError, or NULL
|
Returns : |
TRUE if saving was successful. |
json_generator_to_data ()
gchar * json_generator_to_data (JsonGenerator *generator,gsize *length);
Generates a JSON data stream from generator and returns it as a
buffer.
|
a JsonGenerator |
|
return location for the length of the returned
buffer, or NULL. [out]
|
Returns : |
a newly allocated buffer holding a JSON data stream.
Use g_free() to free the allocated resources. |
json_generator_to_stream ()
gboolean json_generator_to_stream (JsonGenerator *generator,GOutputStream *stream,GCancellable *cancellable,GError **error);
Outputs JSON data and streams it (synchronously) to stream.
|
a JsonGenerator |
|
a GOutputStream |
|
a GCancellable, or NULL. [allow-none]
|
|
return location for a GError, or NULL
|
Returns : |
TRUE if the write operation was successful, and FALSE
on failure. In case of error, the GError will be filled accordingly |
Since 0.12
json_generator_set_root ()
void json_generator_set_root (JsonGenerator *generator,JsonNode *node);
Sets node as the root of the JSON data stream to be serialized by
the JsonGenerator.
Note
The node is copied by the generator object, so it can be safely freed after calling this function.
|
a JsonGenerator |
|
a JsonNode |
Property Details
The "indent" property
"indent" guint : Read / Write
Number of spaces to be used to indent when pretty printing.
Default value: 2
The "indent-char" property
"indent-char" guint : Read / Write
The character that should be used when indenting in pretty print.
Default value: ' '
Since 0.6
The "pretty" property
"pretty" gboolean : Read / Write
Whether the output should be "pretty-printed", with indentation and newlines. The indentation level can be controlled by using the JsonGenerator:indent property
Default value: FALSE
