Json.Reader

g GObject.Object GObject.Object Json.Reader Json.Reader GObject.Object->Json.Reader

Subclasses:None

Methods

Inherited:GObject.Object (37)
Structs:GObject.ObjectClass (5)
class new (node)
  count_elements ()
  count_members ()
  end_element ()
  end_member ()
  get_boolean_value ()
  get_double_value ()
  get_error ()
  get_int_value ()
  get_member_name ()
  get_null_value ()
  get_string_value ()
  get_value ()
  is_array ()
  is_object ()
  is_value ()
  list_members ()
  read_element (index_)
  read_member (member_name)
  set_root (root)

Virtual Methods

Inherited:GObject.Object (7)

Properties

Name Type Flags Short Description
root Json.Node r/w/c The root of the tree to read

Signals

Inherited:GObject.Object (1)

Fields

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

Class Details

class Json.Reader(**kwargs)
Bases:GObject.Object
Abstract:No
Structure:Json.ReaderClass

The JsonReader structure contains only private data and should be accessed using the provided API

New in version 0.12.

classmethod new(node)
Parameters:node (Json.Node or None) – a Json.Node, or None
Returns:the newly created Json.Reader. Use GObject.Object.unref() to release the allocated resources when done
Return type:Json.Reader

Creates a new Json.Reader. You can use this object to read the contents of the JSON tree starting from node

New in version 0.12.

count_elements()
Returns:the number of elements, or -1. In case of failure the Json.Reader is set in an error state
Return type:int

Counts the elements of the current position, if self is positioned on an array

New in version 0.12.

count_members()
Returns:the number of members, or -1. In case of failure the Json.Reader is set in an error state
Return type:int

Counts the members of the current position, if self is positioned on an object

New in version 0.12.

end_element()

Moves the cursor back to the previous node after being positioned inside an array

This function resets the error state of self, if any was set

New in version 0.12.

end_member()

Moves the cursor back to the previous node after being positioned inside an object

This function resets the error state of self, if any was set

New in version 0.12.

get_boolean_value()
Returns:the boolean value
Return type:bool

Retrieves the boolean value of the current position of self

New in version 0.12.

get_double_value()
Returns:the floating point value
Return type:float

Retrieves the floating point value of the current position of self

New in version 0.12.

get_error()
Returns:the pointer to the error, or None
Return type:GLib.Error or None

Retrieves the GLib.Error currently set on self, if the Json.Reader is in error state

New in version 0.12.

get_int_value()
Returns:the integer value
Return type:int

Retrieves the integer value of the current position of self

New in version 0.12.

get_member_name()
Returns:the name of the member, or None
Return type:str or None

Retrieves the name of the current member.

New in version 0.14.

get_null_value()
Returns:True if ‘null’ is set, and False otherwise
Return type:bool

Checks whether the value of the current position of self is ‘null’

New in version 0.12.

get_string_value()
Returns:the string value
Return type:str

Retrieves the string value of the current position of self

New in version 0.12.

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

Retrieves the Json.Node of the current position of self

New in version 0.12.

is_array()
Returns:True if the Json.Reader is on an array, and False otherwise
Return type:bool

Checks whether the self is currently on an array

New in version 0.12.

is_object()
Returns:True if the Json.Reader is on an object, and False otherwise
Return type:bool

Checks whether the self is currently on an object

New in version 0.12.

is_value()
Returns:True if the Json.Reader is on a value, and False otherwise
Return type:bool

Checks whether the self is currently on a value

New in version 0.12.

list_members()
Returns:a newly allocated, None-terminated array of strings holding the members name. Use GLib.strfreev() when done.
Return type:[str]

Retrieves a list of member names from the current position, if self is positioned on an object.

New in version 0.14.

read_element(index_)
Parameters:index (int) – the index of the element
Returns:True on success, and False otherwise
Return type:bool

Advances the cursor of self to the element index_ of the array or the object at the current position.

You can use the Json.Reader.get_value family of functions to retrieve the value of the element; for instance:

json_reader_read_element (reader, 0);
int_value = json_reader_get_int_value (reader);

After reading the value, Json.Reader.end_element() should be called to reposition the cursor inside the Json.Reader, e.g.:

json_reader_read_element (reader, 1);
str_value = json_reader_get_string_value (reader);
json_reader_end_element (reader);

json_reader_read_element (reader, 2);
str_value = json_reader_get_string_value (reader);
json_reader_end_element (reader);

If self is not currently on an array or an object, or if the index_ is bigger than the size of the array or the object, the Json.Reader will be put in an error state until Json.Reader.end_element() is called. This means that if used conditionally, Json.Reader.end_element() must be called on both code paths:

if (!json_reader_read_element (reader, 1))
  {
    json_reader_end_element (reader);
    g_set_error (error, …);
    return FALSE;
  }

str_value = json_reader_get_string_value (reader);
json_reader_end_element (reader);

New in version 0.12.

read_member(member_name)
Parameters:member_name (str) – the name of the member to read
Returns:True on success, and False otherwise
Return type:bool

Advances the cursor of self to the member_name of the object at the current position.

You can use the Json.Reader.get_value family of functions to retrieve the value of the member; for instance:

json_reader_read_member (reader, "width");
width = json_reader_get_int_value (reader);

After reading the value, Json.Reader.end_member() should be called to reposition the cursor inside the Json.Reader, e.g.:

json_reader_read_member (reader, "author");
author = json_reader_get_string_value (reader);
json_reader_end_member (reader);

json_reader_read_member (reader, "title");
title = json_reader_get_string_value (reader);
json_reader_end_member (reader);

If self is not currently on an object, or if the member_name is not defined in the object, the Json.Reader will be put in an error state until Json.Reader.end_member() is called. This means that if used conditionally, Json.Reader.end_member() must be called on both code paths:

if (!json_reader_read_member (reader, "title"))
  {
    json_reader_end_member (reader);
    g_set_error (error, …);
    return FALSE;
  }

str_value = json_reader_get_string_value (reader);
json_reader_end_member (reader);

New in version 0.12.

set_root(root)
Parameters:root (Json.Node or None) – a Json.Node

Sets the root Json.Node to be read by self. The self will take a copy of root

If another Json.Node is currently set as root, it will be replaced.

New in version 0.12.

Property Details

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

The root of the JSON tree that the Json.Reader should read.

New in version 0.12.