json.reader

Module for [Reader] class

Types 3

json.reader.Reader provides a simple, cursor-based API for parsing a JSON DOM.

It is similar, in spirit, to the XML Reader API.

Using json.reader.Reader

g_autoptr(JsonParser) parser = json_parser_new ();

// str is defined elsewhere
json_parser_load_from_data (parser, str, -1, NULL);

g_autoptr(JsonReader) reader = json_reader_new (json_parser_get_root (parser));

json_reader_read_member (reader, "url");
  const char *url = json_reader_get_string_value (reader);
  json_reader_end_member (reader);

json_reader_read_member (reader, "size");
  json_reader_read_element (reader, 0);
    int width = json_reader_get_int_value (reader);
    json_reader_end_element (reader);
  json_reader_read_element (reader, 1);
    int height = json_reader_get_int_value (reader);
    json_reader_end_element (reader);
  json_reader_end_member (reader);

Error handling

In case of error, json.reader.Reader will be set in an error state; all subsequent calls will simply be ignored until a function that resets the error state is called, e.g.:

// ask for the 7th element; if the element does not exist, the
// reader will be put in an error state
json_reader_read_element (reader, 6);

// in case of error, this will return NULL, otherwise it will
// return the value of the element
str = json_reader_get_string_value (value);

// this function resets the error state if any was set
json_reader_end_element (reader);

If you want to detect the error state as soon as possible, you can use json.reader.Reader.getError:

// like the example above, but in this case we print out the
// error immediately
if (!json_reader_read_element (reader, 6))
  {
    const GError *error = json_reader_get_error (reader);
    g_print ("Unable to read the element: %s", error->message);
  }

Methods
GType _getGType() static nothrow
GType _gType() @property nothrow
Reader self() nothrowReturns `this`, for use in `with` statements.
ReaderGidBuilder builder() static nothrowGet builder for [json.reader.Reader] Returns: New builder object
json.node.Node root() @property nothrowGet `root` property. Returns: The root of the JSON tree that the reader should read.
void root(json.node.Node propval) @property nothrowSet `root` property. Params: propval = The root of the JSON tree that the reader should read.
int countElements() nothrowCounts the elements of the current position, if the reader is positioned on an array.
int countMembers() nothrowCounts the members of the current position, if the reader is positioned on an object.
void endElement() nothrowMoves the cursor back to the previous node after being positioned inside an array.
void endMember() nothrowMoves the cursor back to the previous node after being positioned inside an object.
bool getBooleanValue() nothrowRetrieves the boolean value of the current position of the reader.
json.node.Node getCurrentNode() nothrowRetrieves the reader node at the current position. Returns: the current node of the reader
double getDoubleValue() nothrowRetrieves the floating point value of the current position of the reader.
glib.error.ErrorWrap getError() nothrowRetrieves the error currently set on the reader. Returns: the current error
long getIntValue() nothrowRetrieves the integer value of the current position of the reader.
string getMemberName() nothrowRetrieves the name of the current member.
bool getNullValue() nothrowChecks whether the value of the current position of the reader is `null`.
string getStringValue() nothrowRetrieves the string value of the current position of the reader.
json.node.Node getValue() nothrowRetrieves the value node at the current position of the reader.
bool isArray() nothrowChecks whether the reader is currently on an array. Returns: `TRUE` if the reader is on an array
bool isObject() nothrowChecks whether the reader is currently on an object. Returns: `TRUE` if the reader is on an object
bool isValue() nothrowChecks whether the reader is currently on a value. Returns: `TRUE` if the reader is on a value
string[] listMembers() nothrowRetrieves a list of member names from the current position, if the reader is positioned on an object.
bool readElement(uint index) nothrowAdvances the cursor of the reader to the element of the array or the member of the object at the given position.
bool readMember(string memberName) nothrowAdvances the cursor of the reader to the `member_name` of the object at the current position.
void setRoot(json.node.Node root = null) nothrowSets the root node of the JSON tree to be read by reader.
Constructors
this(void * ptr, Flag!"Take" take)
this(json.node.Node node = null)Creates a new reader.

Fluent builder implementation template for json.reader.Reader

Methods
T root(json.node.Node propval) nothrowSet `root` property. Params: propval = The root of the JSON tree that the reader should read. Returns: Builder instance for fluent chaining

Fluent builder for json.reader.Reader

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