Json Value¶
| Spice | |
|---|---|
JsonValue struct¶
Represents a single JSON value.
Composite values (arrays and objects) own their children as heap pointers, so a JsonValue is always reached via heap JsonValue* once it has been parsed. Numbers are stored as double; integers are representable up to 2^53. Object fields preserve insertion order.
Parsing lives in "std/text/json-parser" and serialization in "std/text/json-serializer", so programs only pull in what they use.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a JSON null value
ctor¶
| Spice | |
|---|---|
Construct an empty JSON value of the given kind. Useful for building arrays and objects from scratch.
Parameters
| Name | Type | Description |
|---|---|---|
kind |
JsonValueKind |
Kind of the value |
ctor¶
| Spice | |
|---|---|
Construct a JSON boolean value
Parameters
| Name | Type | Description |
|---|---|---|
value |
bool |
Boolean value |
ctor¶
| Spice | |
|---|---|
Construct a JSON number value
Parameters
| Name | Type | Description |
|---|---|---|
value |
double |
Number value |
ctor¶
| Spice | |
|---|---|
Construct a JSON string value
Parameters
| Name | Type | Description |
|---|---|---|
value |
const String& |
String value |
Methods¶
getKind¶
| Spice | |
|---|---|
Retrieve the kind of the JSON value
Returns: JsonValueKind — Kind of the value
isNull¶
| Spice | |
|---|---|
Check whether the JSON value is null
Returns: bool — true if the value is null, false otherwise
isBool¶
| Spice | |
|---|---|
Check whether the JSON value is a boolean
Returns: bool — true if the value is a boolean, false otherwise
isNumber¶
| Spice | |
|---|---|
Check whether the JSON value is a number
Returns: bool — true if the value is a number, false otherwise
isString¶
| Spice | |
|---|---|
Check whether the JSON value is a string
Returns: bool — true if the value is a string, false otherwise
isArray¶
| Spice | |
|---|---|
Check whether the JSON value is an array
Returns: bool — true if the value is an array, false otherwise
isObject¶
| Spice | |
|---|---|
Check whether the JSON value is an object
Returns: bool — true if the value is an object, false otherwise
getBool¶
| Spice | |
|---|---|
Retrieve the boolean payload. Panics if the value is not a boolean.
Returns: bool — Boolean value
getNumber¶
| Spice | |
|---|---|
Retrieve the number payload. Panics if the value is not a number.
Returns: double — Number value
getString¶
| Spice | |
|---|---|
Retrieve the string payload. Panics if the value is not a string.
Returns: const String& — String value
getArraySize¶
| Spice | |
|---|---|
Retrieve the number of items in the array. Panics if the value is not an array.
Returns: unsigned long — Number of array items
getArrayItem¶
| Spice | |
|---|---|
Retrieve the array item at the given index. Panics if the value is not an array.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the item |
Returns: JsonValue* — Pointer to the array item
getArrayItem¶
| Spice | |
|---|---|
Retrieve the array item at the given index. Panics if the value is not an array.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned int |
Index of the item |
Returns: JsonValue* — Pointer to the array item
addArrayItem¶
| Spice | |
|---|---|
Append an item to the array. Panics if the value is not an array. The array takes ownership of the heap-allocated item.
Parameters
| Name | Type | Description |
|---|---|---|
item |
JsonValue* |
Item to append |
getObjectSize¶
| Spice | |
|---|---|
Retrieve the number of fields in the object. Panics if the value is not an object.
Returns: unsigned long — Number of object fields
getObjectKey¶
| Spice | |
|---|---|
Retrieve the key of the object field at the given index, in insertion order. Panics if the value is not an object.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the field |
Returns: const String& — Field key
getObjectValue¶
| Spice | |
|---|---|
Retrieve the value of the object field at the given index, in insertion order. Panics if the value is not an object.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the field |
Returns: JsonValue* — Pointer to the field value
hasField¶
| Spice | |
|---|---|
Check whether the object contains a field with the given key
Parameters
| Name | Type | Description |
|---|---|---|
key |
string |
Field key to look for |
Returns: bool — true if the field exists, false otherwise
getField¶
| Spice | |
|---|---|
Retrieve the value of the object field with the given key. Panics if the value is not an object or the field does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
key |
string |
Field key to look up |
Returns: JsonValue* — Pointer to the field value
addObjectField¶
| Spice | |
|---|---|
Append a field to the object. Panics if the value is not an object. The object takes ownership of the heap-allocated field value.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const String& |
Field key |
value |
JsonValue* |
Field value |
JsonValueKind enum¶
Tag for the variant stored inside a JsonValue.
| Item | Value | Description |
|---|---|---|
JSON_NULL |
||
JSON_BOOL |
||
JSON_NUMBER |
||
JSON_STRING |
||
JSON_ARRAY |
||
JSON_OBJECT |