Skip to content

Json Parser

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

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

Construct a JSON null 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

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

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

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

serialize

Spice
public f<String> JsonValue.serialize()

Serializes this value into a compact JSON string (no insignificant whitespace). The result round-trips through parseJson.

Returns: String — Compact JSON representation

serializePretty

Spice
public f<String> JsonValue.serializePretty()

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
public p JsonParser.ctor(const String& input)

Construct a JSON parser over the given input string

Parameters

Name Type Description
input const String& JSON text to parse

Methods

parse

Spice
public f<Result<JsonValue*>> JsonParser.parse()

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
public f<Result<JsonValue*>> parseJson(const String& input)

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