parseJSONValue - multiple declarations

Function parseJSONValue

Consumes a single JSON value from the input range and returns the result as a JSONValue.

JSONValue parseJSONValue(LexOptions options = LexOptions.init, Input) (
  ref Input input,
  string filename = ""
)
if (isInputRange!Input && (isSomeChar!(ElementType!Input) || isIntegral!(ElementType!Input)));

The input string must start with a valid JSON document. Any characters occurring after this document will be left in the input range. Use toJSONValue instead if you wish to perform a normal string to JSONValue conversion.

See also

toJSONValue

Example

Parse an object

// parse an object
string str = `{"a": true, "b": "test"}`;
JSONValue v = parseJSONValue(str);
assert(!str.length); // the input has been consumed

auto obj = v.get!(JSONValue[string]);
assert(obj.length == 2);
assert(obj["a"] == true);
assert(obj["b"] == "test");

Example

Parse multiple consecutive values

string str = `1.0 2.0`;
JSONValue v1 = parseJSONValue(str);
assert(v1 == 1.0);
assert(str == `2.0`);
JSONValue v2 = parseJSONValue(str);
assert(v2 == 2.0);
assert(str == ``);

Function parseJSONValue

Parses a stream of JSON tokens and returns the result as a JSONValue.

JSONValue parseJSONValue(Input) (
  ref Input tokens
) @safe
if (isJSONTokenInputRange!Input);

All tokens belonging to the document will be consumed from the input range. Any tokens after the end of the first JSON document will be left in the input token range for possible later consumption.

Example

// lex
auto tokens = lexJSON(`[1, 2, 3]`);

// parse
auto doc = parseJSONValue(tokens);

auto arr = doc.get!(JSONValue[]);
assert(arr.length == 3);
assert(arr[0] == 1.0);
assert(arr[1] == 2.0);
assert(arr[2] == 3.0);