`JsonReader` provides a simple, cursor-based API for parsing a JSON DOM.
It is similar, in spirit, to the XML Reader API.
Using `JsonReader`
```c 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, `JsonReader` 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.:
```c // 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 [[email protected]_error]:
```c // 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); } ```
Example: Reader (cursor-based API):
public static int main (string[] args) {
string str = """
{
"url" : "http://www.gnome.org/img/flash/two-thirty.png",
"size" : [ 652, 242 ]
}""";
// Load a file:
Json.Parser parser = new Json.Parser ();
try {
parser.load_from_data (str);
} catch (Error e) {
print ("Unable to parse data: %s\n", e.message);
return -1;
}
// Create a cursor:
Json.Node node = parser.get_root ();
Json.Reader reader = new Json.Reader (node);
// Read the file:
// We use assert for format validation to keep the sample small.
string url = null;
bool has_size = false;
int64 width = -1;
int64 height = -1;
foreach (string member in reader.list_members ()) {
switch (member) {
case "url":
bool tmp = reader.read_member ("url");
assert (tmp == true);
assert (reader.is_value ());
url = reader.get_string_value ();
reader.end_member ();
break;
case "size":
bool tmp = reader.read_member ("size");
assert (tmp == true);
assert (reader.is_array ());
assert (reader.count_elements () == 2);
// Element 0:
reader.read_element (0);
assert (reader.is_value ());
width = reader.get_int_value ();
reader.end_element ();
// Element 1:
reader.read_element (1);
assert (reader.is_value ());
height = reader.get_int_value ();
reader.end_element ();
reader.end_member ();
has_size = true;
break;
default:
assert_not_reached ();
}
}
if (has_size == false || url == null) {
assert_not_reached ();
}
// Print the data:
print ("url: %s\n", url);
print ("width: %" + int64.FORMAT + "\n", width);
print ("height: %" + int64.FORMAT + "\n", height);
return 0;
}
valac --pkg json-glib-1.0 reader-example.vala
- public int count_elements ()
Counts the elements of the current position, if the reader is
positioned on an array.
- public int count_members ()
Counts the members of the current position, if the reader is
positioned on an object.
- public void end_element ()
Moves the cursor back to the previous node after being positioned
inside an array.
- public void end_member ()
Moves the cursor back to the previous node after being positioned
inside an object.
- public bool get_boolean_value ()
Retrieves the boolean value of the current position of the reader.
- public double get_double_value ()
Retrieves the floating point value of the current position of the
reader.
- public unowned Error? get_error ()
Retrieves the error currently set on the reader.
- public int64 get_int_value ()
Retrieves the integer value of the current position of the reader.
- public unowned string? get_member_name ()
Retrieves the name of the current member.
- public bool get_null_value ()
Checks whether the value of the current position of the reader is
`null`.
- public unowned string get_string_value ()
Retrieves the string value of the current position of the reader.
- public unowned Node? get_value ()
Retrieves the value node at the current position of the reader.
- public bool is_array ()
Checks whether the reader is currently on an array.
- public bool is_object ()
Checks whether the reader is currently on an object.
- public bool is_value ()
Checks whether the reader is currently on a value.
- public string[] list_members ()
Retrieves a list of member names from the current position, if the
reader is positioned on an object.
- public bool read_element (uint index_)
Advances the cursor of the reader to the element of the array or the
member of the object at the given position.
- public bool read_member (string member_name)
Advances the cursor of the reader to the `member_name` of the object
at the current position.
- public void set_root (Node? root)
Sets the root node of the JSON tree to be read by
this.