funkwerk.stdx.data.json.parser

Provides various means for parsing JSON documents.

This module contains two different parser implementations. The first implementation returns a single JSON document in the form of a JSONValue, while the second implementation returns a stream of nodes. The stream based parser is particularly useful for deserializing with few allocations or for processing large documents.

Members

Enums

JSONParserNodeKind
enum JSONParserNodeKind

Identifies the kind of a parser node.

isJSONParserNodeInputRange
eponymoustemplate isJSONParserNodeInputRange(R)

Tests if a given type is an input range of JSONParserNode.

isJSONTokenInputRange
eponymoustemplate isJSONTokenInputRange(R)

Tests if a given type is an input range of JSONToken.

Functions

parseJSONStream
JSONParserRange!(JSONLexerRange!(Input, options, String)) parseJSONStream(Input input, string filename)
JSONParserRange!Input parseJSONStream(Input tokens)

Parses a JSON document using a lazy parser node range.

parseJSONValue
JSONValue parseJSONValue(Input input, string filename, int maxDepth)

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

parseJSONValue
JSONValue parseJSONValue(Input tokens, int maxDepth)

* Parses a stream of JSON tokens and returns the result as a JSONValue. * * 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.

readArray
void readArray(R nodes, void delegate() @(safe) del)

Reads an array and issues a callback for each entry.

readBool
bool readBool(R nodes)

Reads a single double value.

readDouble
double readDouble(R nodes)

Reads a single double value.

readObject
void readObject(R nodes, void delegate(string key) @(safe) del)

Reads an object and issues a callback for each field.

readString
string readString(R nodes)

Reads a single double value.

skipToKey
bool skipToKey(R nodes, string key)

Skips all entries in an object until a certain key is reached.

skipValue
void skipValue(R nodes)

Skips a single JSON value in a parser stream.

toJSONValue
JSONValue toJSONValue(Input input, string filename, int maxDepth)
JSONValue toJSONValue(Input tokens, int maxDepth)

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

Manifest constants

defaultMaxDepth
enum defaultMaxDepth;

The default amount of nesting in the input allowed by toJSONValue and parseJSONValue.

Structs

JSONParserNode
struct JSONParserNode(String)

Represents a single node of a JSON parse tree.

JSONParserRange
struct JSONParserRange(Input)

Lazy input range of JSON parser nodes.

Examples

import std.algorithm : equal, map;
import std.format : format;

// Parse a JSON string to a single value
JSONValue value = toJSONValue(`{"name": "D", "kind": "language"}`);

// Parse a JSON string to a node stream
auto nodes = parseJSONStream(`{"name": "D", "kind": "language"}`);
with (JSONParserNodeKind) {
    assert(nodes.map!(n => n.kind).equal(
        [objectStart, key, literal, key, literal, objectEnd]));
}

// Parse a list of tokens instead of a string
auto tokens = lexJSON(`{"name": "D", "kind": "language"}`);
JSONValue value2 = toJSONValue(tokens);
assert(value == value2, format!"%s != %s"(value, value2));

Meta

Authors

Sönke Ludwig