Function lexJSON

Returns a lazy range of tokens corresponding to the given JSON input string.

JSONLexerRange!(Input,options,String) lexJSON(LexOptions options = LexOptions.init, String, Input) (
  Input input,
  string filename = null
)
if (isInputRange!Input && (isSomeChar!(ElementType!Input) || isIntegral!(ElementType!Input)));

The input must be a valid JSON string, given as an input range of either characters, or of integral values. In case of integral types, the input ecoding is assumed to be a superset of ASCII that is parsed unit by unit.

For inputs of type string and of type immutable(ubyte)[], all string literals will be stored as slices into the original string. String literals containung escape sequences will be unescaped on demand when JSONString.value is accessed.

Throws

Without LexOptions.noThrow, a JSONException is thrown as soon as an invalid token is encountered.

If LexOptions.noThrow is given, lexJSON does not throw any exceptions, apart from letting through any exceptins thrown by the input range. Instead, a token with kind JSONToken.Kind.error is generated as the last token in the range.

Example

import std.algorithm : equal, map;

auto rng = lexJSON(`{"hello": 1.2, "world": [1, true, null]}`);
with (JSONTokenKind)
{
    assert(rng.map!(t => t.kind).equal(
        [objectStart, string, colon, number, comma,
        string, colon, arrayStart, number, comma,
        boolean, comma, null_, arrayEnd,
        objectEnd]));
}

Example

auto rng = lexJSON("true\n   false null\r\n  1.0\r \"test\"");
rng.popFront();
assert(rng.front.boolean == false);
assert(rng.front.location.line == 1 && rng.front.location.column == 3);
rng.popFront();
assert(rng.front.kind == JSONTokenKind.null_);
assert(rng.front.location.line == 1 && rng.front.location.column == 9);
rng.popFront();
assert(rng.front.number == 1.0);
assert(rng.front.location.line == 2 && rng.front.location.column == 2);
rng.popFront();
assert(rng.front.string == "test");
assert(rng.front.location.line == 3 && rng.front.location.column == 1);
rng.popFront();
assert(rng.empty);