Json.Generator

g GObject.Object GObject.Object Json.Generator Json.Generator GObject.Object->Json.Generator

Subclasses:None

Methods

Inherited:GObject.Object (37)
Structs:GObject.ObjectClass (5)
class new ()
  get_indent ()
  get_indent_char ()
  get_pretty ()
  get_root ()
  set_indent (indent_level)
  set_indent_char (indent_char)
  set_pretty (is_pretty)
  set_root (node)
  to_data ()
  to_file (filename)
  to_gstring (string)
  to_stream (stream, cancellable)

Virtual Methods

Inherited:GObject.Object (7)

Properties

Name Type Flags Short Description
indent int r/w Number of indentation spaces
indent-char int r/w Character that should be used when indenting
pretty bool r/w Pretty-print the output
root Json.Node r/w Root of the JSON data tree

Signals

Inherited:GObject.Object (1)

Fields

Inherited:GObject.Object (1)
Name Type Access Description
parent_instance GObject.Object r  

Class Details

class Json.Generator(**kwargs)
Bases:GObject.Object
Abstract:No
Structure:Json.GeneratorClass

JSON data streams generator. The contents of the Json.Generator structure are private and should only be accessed via the provided API.

classmethod new()
Returns:the newly created Json.Generator instance
Return type:Json.Generator

Creates a new Json.Generator. You can use this object to generate a JSON data stream starting from a data object model composed by Json.Nodes.

get_indent()
Returns:the number of repetitions per indentation level
Return type:int

Retrieves the value set using Json.Generator.set_indent().

New in version 0.14.

get_indent_char()
Returns:the character to be used when indenting
Return type:str

Retrieves the value set using Json.Generator.set_indent_char().

New in version 0.14.

get_pretty()
Returns:True if the generated JSON should be pretty-printed, and False otherwise
Return type:bool

Retrieves the value set using Json.Generator.set_pretty().

New in version 0.14.

get_root()
Returns:a Json.Node, or None. The returned node is owned by the Json.Generator and it should not be freed
Return type:Json.Node or None

Retrieves a pointer to the root Json.Node set using Json.Generator.set_root().

New in version 0.14.

set_indent(indent_level)
Parameters:indent_level (int) – the number of repetitions of the indentation character that should be applied when pretty printing

Sets the number of repetitions for each indentation level.

New in version 0.14.

set_indent_char(indent_char)
Parameters:indent_char (str) – a Unicode character to be used when indenting

Sets the character to be used when indenting

New in version 0.14.

set_pretty(is_pretty)
Parameters:is_pretty (bool) – whether the generated string should be pretty printed

Sets whether the generated JSON should be pretty printed, using the indentation character specified in the Json.Generator :indent-char property and the spacing specified in Json.Generator :indent property.

New in version 0.14.

set_root(node)
Parameters:node (Json.Node) – a Json.Node

Sets node as the root of the JSON data stream to be serialized by the Json.Generator.

The passed node is copied by the generator object, so it can be safely freed after calling this function.

to_data()
Returns:a newly allocated buffer holding a JSON data stream. Use GLib.free() to free the allocated resources.
length:return location for the length of the returned buffer, or None
Return type:(str, length: int)

Generates a JSON data stream from self and returns it as a buffer.

to_file(filename)
Parameters:filename (str) – path to the target file
Raises:GLib.Error
Returns:True if saving was successful.
Return type:bool

Creates a JSON data stream and puts it inside filename, overwriting the current file contents. This operation is atomic.

to_gstring(string)
Parameters:string (GLib.String) – a GLib.String
Returns:a GLib.String holding a JSON data stream. Use GLib.String.free() to free the allocated resources.
Return type:GLib.String

Generates a JSON data stream from self and appends it to string.

New in version 1.4.

to_stream(stream, cancellable)
Parameters:
Raises:

GLib.Error

Returns:

True if the write operation was successful, and False on failure. In case of error, the GLib.Error will be filled accordingly

Return type:

bool

Outputs JSON data and streams it (synchronously) to stream.

New in version 0.12.

Property Details

Json.Generator.props.indent
Name:indent
Type:int
Default Value:2
Flags:READABLE, WRITABLE

Number of spaces to be used to indent when pretty printing.

Json.Generator.props.indent_char
Name:indent-char
Type:int
Default Value:32
Flags:READABLE, WRITABLE

The character that should be used when indenting in pretty print.

New in version 0.6.

Json.Generator.props.pretty
Name:pretty
Type:bool
Default Value:False
Flags:READABLE, WRITABLE

Whether the output should be “pretty-printed”, with indentation and newlines. The indentation level can be controlled by using the Json.Generator :indent property

Json.Generator.props.root
Name:root
Type:Json.Node
Default Value:None
Flags:READABLE, WRITABLE

The root Json.Node to be used when constructing a JSON data stream.

New in version 0.4.