The Ioto JSON parser is a high performance JSON parser, serializer and query engine.
It parses JSON text into an in-memory tree for efficient examination, storage and export.
- JSON and JSON5 parser
- Stores parsed JSON as an in-memory tree
- Fast, flexible query engine mange and navigate the JSON tree
- Simple get/set API to retrieve and update JSON values.
- APIs to walk, iterate and navigate a parsed tree
- Overlay and blend JSON trees
- Supports JSON/5 with extensions
- Serialize in strict or JSON/5 formats
- Persist to flash/disk.
API Quick Tour
To parse a JSON string, call the jsonParse API:
This parses the given JSON text and returns a Json instance which represents the in-memory parsed JSON tree.
You can provide JSON or JSON/5 which permits more relaxed JSON format where keys may omit quotes and single quotes can be used on values.
To query a value from the JSON tree, use jsonGet:
This will retrieve the weather value (Sunny) as a static string that does not need freeing.
When calling jsonGet, you can provide keys with dots:
and you can provide a default value to be returned as the last parameter if the property is not defined in the JSON tree.
You can use the jsonGetInt and jsonGetBool APIs to return data as an integer or boolean data type.
To update or set a value, use the jsonSet API:
This will update the value of the given property in-memory. Again, you can use dotted key properties of any depth.
The last parameter to jsonSet can be used to specify a data type for the value. In general, the Ioto JSON API can sleuth the data type for you. But as data values are supplied as strings, you may need to specify a type if you want the value to be stored as a number.
The valid types are: JSON_OBJECT, JSON_ARRAY, JSON_STRING and JSON_PRIMITIVE. The primitives are false, true, null, undefined and numbers.
To remove a property, use jsonRemove:
When you are finished with a JSON tree, free it with jsonFree
This will release allocated memory.
To save your in-memory tree as a string, use jsonToString:
This will save the in-memory tree as JSON/5.
To save in strict JSON, use:
To save a sub-set of the tree, specify the root node to export:
This will save all properties under the "address" (including address) property.
Working with Files
You can parse a JSON file with the jsonParseFile API:
and you can save the in-memory JSON tree using jsonSave:
Navigating and Iterating
Properties in the JSON tree are stored as nodes. These nodes can be identified by a numeric ID or by node reference. Nodes and node IDs are used to very efficiently traverse and enumerate property values.
To get a node ID, use jsonGetId:
You can retrieve a node using another node as a starting point:
In fact, most APIs such as jsonGet and jsonSet take a node ID as a parameter that specifies a root of the tree to base the operation. For example, these are equivalent:
Similarly, you can retreive a node reference:
You can iterate over nodes using the ITERATE_JSON macro:
Under the hood, ITERATE_JSON follows child
Use the jsonPrint API to print a JSON tree to the console:
JSON/5 an extension of JSON that makes it easier to create, read and maintain configuration files in JSON.
- Objects or arrays may have a trailing comma
- Strings may be single quoted
- Strings may span multiple lines (single, double or back-tick quotes)
- Numbers may have a leading or trailing decimal point, be hexadecimal, may begin with a +.
- Values may be regular expressions
- Undefined is a valid value
- Single and multiline comments are allowed and preserved
The JSON engine returns results as static pointers into the in-memory tree. Results are returned as (const char) or (cchar) values and this saves having to duplicate strings. This reduces memory footprint and is much more efficient.
It is important not to cast returned values to (char*) or to modify the referenced strings using "dirty" programming to break the (const char) typing protection.