Json Parser¶
| 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.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a JSON null 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
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
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
serialize¶
| Spice | |
|---|---|
Serializes this value into a compact JSON string (no insignificant whitespace). The result round-trips through parseJson.
Returns: String — Compact JSON representation
serializePretty¶
| Spice | |
|---|---|
Serializes this value into a human-readable JSON string using two-space indentation for nested arrays and objects.
Returns: String — Pretty-printed JSON representation
JsonParser struct¶
Recursive-descent JSON parser.
Use parseJson for one-shot parsing; the struct is exposed mainly so the parser state (position, error message) can be inspected when needed.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a JSON parser over the given input string
Parameters
| Name | Type | Description |
|---|---|---|
input |
const String& |
JSON text to parse |
Methods¶
parse¶
| Spice | |
|---|---|
Parse the input string into a JSON value tree
Returns: Result<JsonValue*> — Result holding the root JSON value, or an error if the input is malformed
Functions¶
parseJson¶
| Spice | |
|---|---|
Parse a JSON document.
On success returns the root JsonValue (heap-allocated; the caller owns it). On failure returns an error describing the first problem encountered.
Parameters
| Name | Type | Description |
|---|---|---|
input |
const String& |
Returns: Result<JsonValue*>
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 |