Skip to content

Json Value

Spice
import "std/text/json-value";

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
public p JsonValue.ctor()

Construct a JSON null value

ctor

Spice
public p JsonValue.ctor(JsonValueKind kind)

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
public p JsonValue.ctor(bool value)

Construct a JSON boolean value

Parameters

Name Type Description
value bool Boolean value

ctor

Spice
public p JsonValue.ctor(double value)

Construct a JSON number value

Parameters

Name Type Description
value double Number value

ctor

Spice
public p JsonValue.ctor(const String& value)

Construct a JSON string value

Parameters

Name Type Description
value const String& String value

dtor

Spice
public p JsonValue.dtor()

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
public f<JsonValueKind> JsonValue.getKind()

Retrieve the kind of the JSON value

Returns: JsonValueKind — Kind of the value

isNull

Spice
public f<bool> JsonValue.isNull()

Check whether the JSON value is null

Returns: bool — true if the value is null, false otherwise

isBool

Spice
public f<bool> JsonValue.isBool()

Check whether the JSON value is a boolean

Returns: bool — true if the value is a boolean, false otherwise

isNumber

Spice
public f<bool> JsonValue.isNumber()

Check whether the JSON value is a number

Returns: bool — true if the value is a number, false otherwise

isString

Spice
public f<bool> JsonValue.isString()

Check whether the JSON value is a string

Returns: bool — true if the value is a string, false otherwise

isArray

Spice
public f<bool> JsonValue.isArray()

Check whether the JSON value is an array

Returns: bool — true if the value is an array, false otherwise

isObject

Spice
public f<bool> JsonValue.isObject()

Check whether the JSON value is an object

Returns: bool — true if the value is an object, false otherwise

getBool

Spice
public f<bool> JsonValue.getBool()

Retrieve the boolean payload. Panics if the value is not a boolean.

Returns: bool — Boolean value

getNumber

Spice
public f<double> JsonValue.getNumber()

Retrieve the number payload. Panics if the value is not a number.

Returns: double — Number value

getString

Spice
public f<const String&> JsonValue.getString()

Retrieve the string payload. Panics if the value is not a string.

Returns: const String& — String value

getArraySize

Spice
public f<unsigned long> JsonValue.getArraySize()

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
public f<JsonValue*> JsonValue.getArrayItem(unsigned long idx)

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
public f<JsonValue*> JsonValue.getArrayItem(unsigned int idx)

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
public p JsonValue.addArrayItem(JsonValue* item)

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
public f<unsigned long> JsonValue.getObjectSize()

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
public f<const String&> JsonValue.getObjectKey(unsigned long idx)

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
public f<JsonValue*> JsonValue.getObjectValue(unsigned long idx)

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
public f<bool> JsonValue.hasField(string key)

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
public f<JsonValue*> JsonValue.getField(string key)

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
public p JsonValue.addObjectField(const String& key, JsonValue* value)

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
public p deleteJsonValue(JsonValue*& value)

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