json.parser

Module for [Parser] class

Types 3

json.parser.Parser provides an object for parsing a JSON data stream, either inside a file or inside a static buffer.

Using json.parser.Parser

The json.parser.Parser API is fairly simple:

gboolean
parse_json (const char *filename)
{
  g_autoptr(JsonParser) parser = json_parser_new ();
  g_autoptr(GError) error = NULL

  json_parser_load_from_file (parser, filename, &error);
  if (error != NULL)
    {
      g_critical ("Unable to parse '%s': %s", filename, error->message);
      return FALSE;
    }

  g_autoptr(JsonNode) root = json_parser_get_root (parser);

  // manipulate the object tree from the root node

  return TRUE
}

By default, the entire process of loading the data and parsing it is synchronous; the json.parser.Parser.loadFromStreamAsync API will load the data asynchronously, but parse it in the main context as the signals of the parser must be emitted in the same thread. If you do not use signals, and you wish to also parse the JSON data without blocking, you should use a gio.task.Task and the synchronous json.parser.Parser API inside the task itself.

Methods
GType _getGType() static nothrow
GType _gType() @property nothrow
Parser self() nothrowReturns `this`, for use in `with` statements.
ParserGidBuilder builder() static nothrowGet builder for [json.parser.Parser] Returns: New builder object
bool immutable_() @property nothrowGet `immutable_` property. Returns: Whether the tree built by the parser should be immutable when created.
json.parser.Parser newImmutable() static nothrowCreates a new parser instance with its [json.parser.Parser.immutable_] property set to `TRUE` to create immutable output trees. Returns: the newly created parser
uint getCurrentLine() nothrowRetrieves the line currently parsed, starting from 1.
uint getCurrentPos() nothrowRetrieves the current position inside the current line, starting from 0.
json.node.Node getRoot() nothrowRetrieves the top level node from the parsed JSON stream.
bool hasAssignment(out string variableName) nothrowA JSON data stream might sometimes contain an assignment, like:
bool loadFromData(string data)Loads a JSON stream from a buffer and parses it.
bool loadFromFile(string filename)Loads a JSON stream from the content of `filename` and parses it.
bool loadFromMappedFile(string filename)Loads a JSON stream from the content of `filename` and parses it.
bool loadFromStream(gio.input_stream.InputStream stream, gio.cancellable.Cancellable cancellable = null)Loads the contents of an input stream and parses them.
void loadFromStreamAsync(gio.input_stream.InputStream stream, gio.cancellable.Cancellable cancellable = null, gio.types.AsyncReadyCallback callback = null) nothrowAsynchronously reads the contents of a stream.
bool loadFromStreamFinish(gio.async_result.AsyncResult result)Finishes an asynchronous stream loading started with [json.parser.Parser.loadFromStreamAsync].
json.node.Node stealRoot() nothrowSteals the top level node from the parsed JSON stream.
gulong connectArrayElement(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == json.array.Array))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == int))) && (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : json.parser.Parser))) && Parameters!T.length < 4) nothrowConnect to `ArrayElement` signal.
gulong connectArrayEnd(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == json.array.Array))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : json.parser.Parser))) && Parameters!T.length < 3) nothrowConnect to `ArrayEnd` signal.
gulong connectArrayStart(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : json.parser.Parser))) && Parameters!T.length < 2) nothrowConnect to `ArrayStart` signal.
gulong connectError(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == void *))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : json.parser.Parser))) && Parameters!T.length < 3) nothrowConnect to `Error` signal.
gulong connectObjectEnd(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == json.object.ObjectWrap))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : json.parser.Parser))) && Parameters!T.length < 3) nothrowConnect to `ObjectEnd` signal.
gulong connectObjectMember(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == json.object.ObjectWrap))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == string))) && (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] : json.parser.Parser))) && Parameters!T.length < 4) nothrowConnect to `ObjectMember` signal.
gulong connectObjectStart(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : json.parser.Parser))) && Parameters!T.length < 2) nothrowConnect to `ObjectStart` signal.
gulong connectParseEnd(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : json.parser.Parser))) && Parameters!T.length < 2) nothrowConnect to `ParseEnd` signal.
gulong connectParseStart(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : json.parser.Parser))) && Parameters!T.length < 2) nothrowConnect to `ParseStart` signal.
Constructors
this(void * ptr, Flag!"Take" take)
this()Creates a new JSON parser.

Fluent builder implementation template for json.parser.Parser

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

Fluent builder for json.parser.Parser

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