toJSONValue

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

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.

  1. JSONValue toJSONValue(Input input, string filename, int maxDepth)
    toJSONValue
    (
    Input
    )
    (
    Input input
    ,
    string filename = ""
    ,)
    if (
    isInputRange!Input &&
    (
    isSomeChar!(ElementType!Input) ||
    isIntegral!(ElementType!Input)
    )
    )
  2. JSONValue toJSONValue(Input tokens, int maxDepth)

Examples

// 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);

import std.conv;
JSONValue jv = toJSONValue(`{ "a": 1234 }`);
assert(jv["a"].to!int == 1234);

See Also

parseJSONValue

Meta