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.
Destroying a value recursively destroys everything below it, so a whole tree is released with a single deleteJsonValue call on its root. Accessors like getField hand out borrowed pointers that stay owned by their parent, while addArrayItem and addObjectField take ownership of what they are given.
Note that the root cannot simply be held in a heap JsonValue* and left to the compiler: the heap qualifier only emits a raw deallocation when the variable goes out of scope and never runs the destructor of the pointee, so the children below the root would be lost.
Parsing lives in "std/text/json/json-parser" and serialization in "std/text/json/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 |
dtor¶
| Spice | |
|---|---|
Destruct the JSON value, releasing every array item and object field it owns. Since the children are destroyed the same way, this tears down the whole subtree below the 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 |
Functions¶
deleteJsonValue¶
| Spice | |
|---|---|
Destroys a heap-allocated JSON value together with everything it owns and sets the given handle to nil, so releasing a whole tree only takes one call on its root.
Note that this is what sDelete would normally be used for. It cannot be used here, because a value tree is destroyed recursively and the compiler does not emit an explicit dtor that is only reached through sDestruct.
Parameters
| Name | Type | Description |
|---|---|---|
value |
JsonValue*& |
Handle of the value to destroy |
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 |