JSON document or value for structured data.
You use JSON to work with structured data. You can parse a document from text or build one in code. Operations never throw exceptions, so missing keys or syntax errors won't crash your application.
Call JSON::Parse() to parse JSON text and check for syntax errors:
if (!prefs)
static JSON Parse(const String &text)
Parse JSON text.
unsigned error_line() const
Get the 1-based line number of the parse error (0 unless this is a failed Parse() result).
JSON()
Create an empty, invalid JSON value.
String error_message() const
Get the parse error description ("" unless this is a failed Parse() result).
char * data()
Get raw UTF-8 data.
Definition String8.h:53
String8 & utf8()
Get native UTF-8 string.
Definition String.h:109
Reading Values
You can chain operator[] across keys or indices without checking each step. A missing key, an out-of-range index, or the wrong kind of value anywhere in the chain safely resolves to the fallback passed to Or().
Read nested values with fallback defaults:
void ApplyPrefs(
const JSON& prefs) {
double ui_scale = prefs[
"ui"][
"scale"].
Or(1.0);
String player_name = prefs[
"profile"][
"name"].
Or(
"anonymous");
bool muted = prefs[
"audio"][
"muted"].
Or(
false);
}
double Or(double fallback) const
Read this value as a number.
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
The Or() method reads a value matching the fallback's type without converting between types (reading "3" with a fallback of 0 returns 0). An integer fallback truncates a fractional number toward zero.
Always read values through a const JSON reference. Indexing a non-const document creates missing objects along the path automatically.
- Note
- Testing a lookup in a conditional checks whether the key exists rather than its boolean value. A key holding false still evaluates to true in a conditional. Pass false to Or() to read a JSON boolean.
Iterating Objects and Arrays
Iterate over an object's members using a range-for loop over AsObject():
void ConnectAll(
const JSON& prefs) {
for (
auto [server, address] : prefs[
"servers"].
AsObject())
Connect(server, address.Or(""));
}
JSONObject AsObject() const
Get a typed view of this value as an object (for iteration, Has(), and size()).
Call AsArray() to iterate over array elements (see JSONArray). See JSONObject for object iteration.
Building Documents
Start a new document by calling JSON::Object() or JSON::Array(). Indexing a non-const document with operator[] returns an assignable reference (see JSON::Ref). To build an array, assign JSON::Array() before indexing elements, or call Push() through AsArray() to append items.
Populate nested values and serialize the document to text:
save["player"]["name"] = "ada";
save["player"]["level"] = 12;
save["unlocks"][0] = "tutorial_done";
JSON unlocks = save[
"unlocks"];
void Push(const JSON &value)
Append value, sharing its document.
String Stringify(unsigned indent=0) const
Serialize this value to JSON text.
JSONArray AsArray() const
Get a typed view of this value as an array (for iteration, size(), and Push()).
static JSON Object()
Create a new empty object value.
static JSON Array()
Create a new empty array value.
Copying Documents
A copy shares the document, and a JSON assigned into a document shares its value as well. A change made through one copy shows up in every other copy and in any document the value was assigned into.
Copy a document to share it, or call Clone() to make an independent copy:
JSON Clone() const
Deep-copy this value into an independent document.
- Warning
- Only one thread at a time may access a document. Because copies share the underlying data, accessing copies across threads requires your own synchronization.
- See also
- JSON::Ref, JSONObject, JSONArray
|
| | JSON () |
| | Create an empty, invalid JSON value.
|
| | JSON (const JSON &other) |
| | Create a copy of another JSON.
|
| | JSON (JSON &&other) |
| | Move constructor.
|
| | ~JSON () |
| | Destructor.
|
| JSON & | operator= (const JSON &other) |
| | Assignment operator.
|
| JSON & | operator= (JSON &&other) |
| | Move assignment operator.
|
| bool | is_valid () const |
| | Whether or not this JSON holds a value.
|
| | operator bool () const |
| | Whether or not this JSON holds a value (the same as is_valid()).
|
| String | error_message () const |
| | Get the parse error description ("" unless this is a failed Parse() result).
|
| unsigned | error_line () const |
| | Get the 1-based line number of the parse error (0 unless this is a failed Parse() result).
|
| bool | is_object () const |
| | Whether or not this value is an object.
|
| bool | is_array () const |
| | Whether or not this value is an array.
|
| bool | is_string () const |
| | Whether or not this value is a string.
|
| bool | is_number () const |
| | Whether or not this value is a number.
|
| bool | is_bool () const |
| | Whether or not this value is a boolean.
|
| bool | is_null () const |
| | Whether or not this value is null.
|
| JSON | operator[] (const String &key) const |
| | Read an object member.
|
| JSON | operator[] (size_t index) const |
| | Read an array element.
|
| Ref | operator[] (const String &key) |
| | Get an assignable reference to an object member.
|
| Ref | operator[] (size_t index) |
| | Get an assignable reference to an array element.
|
| double | Or (double fallback) const |
| | Read this value as a number.
|
| int64_t | Or (long long fallback) const |
| | Read this value as a number, truncated toward zero to an integer.
|
| int64_t | Or (int fallback) const |
| | Same as Or(long long).
|
| int64_t | Or (unsigned fallback) const |
| | Same as Or(long long).
|
| int64_t | Or (long fallback) const |
| | Same as Or(long long).
|
| int64_t | Or (unsigned long fallback) const |
| | Same as Or(long long).
|
| int64_t | Or (unsigned long long fallback) const |
| | Same as Or(long long).
|
| double | Or (float fallback) const |
| | Same as Or(double).
|
| bool | Or (bool fallback) const |
| | Read this value as a boolean.
|
| String | Or (const String &fallback) const |
| | Read this value as a string.
|
| String | Or (const char *fallback) const |
| | Same as Or(const String&).
|
| JSONObject | AsObject () const |
| | Get a typed view of this value as an object (for iteration, Has(), and size()).
|
| JSONArray | AsArray () const |
| | Get a typed view of this value as an array (for iteration, size(), and Push()).
|
| String | Stringify (unsigned indent=0) const |
| | Serialize this value to JSON text.
|
| JSON | Clone () const |
| | Deep-copy this value into an independent document.
|