Function jsonLexer
Convenience function to create a [JsonLexer] and infer the range type.
auto jsonLexer(IRangeT)
(
IRangeT iRange
);
Example
import std .exception : assertNotThrown;
struct Test {
string text;
Token expected;
bool function(JsonLexer!string) lexerValidator;
}
Test[] tests = [
Test("null", Token .NULL, (p) => true),
Test(" true", Token .BOOL, (p) => p .boolValue() == true),
Test("false ", Token .BOOL, (p) => p .boolValue() == false),
Test("7", Token .LONG, (p) => p .longValue() == 7),
Test("-6", Token .LONG, (p) => p .longValue() == -6),
Test("34.56", Token .DOUBLE, (p) => p .doubleValue() == 34.56),
Test("-4.567", Token .DOUBLE, (p) => p .doubleValue() == -4.567),
Test("1.23e20", Token .DOUBLE, (p) => p .doubleValue() == 1.23e20),
Test("-1.23e+20", Token .DOUBLE, (p) => p .doubleValue() == -1.23e20),
Test("-1.23e-20", Token .DOUBLE, (p) => p .doubleValue() == -1.23e-20),
Test("\"b\\t\\u25BDb\"", Token .STRING, (p) => p .rawString() == "b\\t\\u25BDb"),
Test("\"b\\t\\u25BDb\"", Token .STRING, (p) => p .stringValue() == "b\t▽b"),
];
foreach (size_t i, Test test; tests) {
auto lexer = jsonLexer(test .text);
assertNotThrown(lexer .expectToken(test .expected), "Invalid token in test " ~ i .to!string);
assert(test .lexerValidator(lexer), "Lexer invalid in test " ~ i .to!string);
}
Example
auto lexer = jsonLexer(" [\"b[ob\" ,\"h]am\"] ");
lexer .expectToken(Token .ARRAY_START);
lexer .expectToken(Token .STRING);
lexer .expectToken(Token .STRING);
lexer .expectToken(Token .ARRAY_END);
Example
auto lexer = jsonLexer("{\n \"a\" : \"b[]{}ob\"\n ,\"b\" : \"h{[]}m\"} ");
lexer .expectToken(Token .OBJECT_START);
lexer .expectToken(Token .STRING);
lexer .expectToken(Token .STRING);
lexer .expectToken(Token .STRING);
lexer .expectToken(Token .STRING);
lexer .expectToken(Token .OBJECT_END);
Example
import std .exception : assertThrown;
auto lexer = jsonLexer(" [\"b[ob\" ,\"h]am\"] ");
assertThrown!JsonLexException(lexer .expectToken(Token .OBJECT_START));
Example
auto lexer = jsonLexer("{\n \"a\" : \"b[]{}ob\"\n ,\"b\" : \"h{[]}m\"} ");
assert(lexer .peek() == Token .OBJECT_START);
assert(lexer .peek() == Token .OBJECT_START);
assert(lexer .advance() == Token .OBJECT_START);
assert(lexer .advance() == Token .STRING);
assert(lexer .advance() == Token .STRING);
assert(lexer .peek() == Token .STRING);
assert(lexer .peek() == Token .STRING);
assert(lexer .advance() == Token .STRING);
assert(lexer .advance() == Token .STRING);
assert(lexer .advance() == Token .OBJECT_END);