Function toJSONValue

Parses a JSON string or token range and returns the result as a JSONValue.

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

JSONValue toJSONValue(Input) (
  Input tokens
)
if (isJSONTokenInputRange!Input);

The input string must be a valid JSON document. In particular, it must not contain any additional text other than whitespace after the end of the JSON document.

See also

parseJSONValue

Example

// parse a simple number
JSONValue a = toJSONValue(`1.0`);
assert(a == 1.0);

// parse an object
JSONValue b = toJSONValue(`{"a": true, "b": "test"}`);
auto bdoc = b.get!(JSONValue[string]);
assert(bdoc.length == 2);
assert(bdoc["a"] == true);
assert(bdoc["b"] == "test");

// parse an array
JSONValue c = toJSONValue(`[1, 2, null]`);
auto cdoc = c.get!(JSONValue[]);
assert(cdoc.length == 3);
assert(cdoc[0] == 1.0);
assert(cdoc[1] == 2.0);
assert(cdoc[2] == null);