Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

asdf.jsonparser

JSON Parsing API
Authors:
Ilya Yaroshenko
License:
MIT
Asdf parseJson(Flag!"includingNewLine" includingNewLine = Yes.includingNewLine, Flag!"spaces" spaces = Yes.spaces, Chunks)(Chunks chunks, size_t initLength = 32)
if (is(ElementType!Chunks : const(ubyte)[]));
Parses json value
Parameters:
Chunks chunks input range composed of elements type of const(ubyte)[]. chunks can use the same buffer for each chunk.
size_t initLength initial output buffer length. Minimum value is 32.
Returns:
ASDF value
Examples:
import std.range: chunks;
auto text = cast(const ubyte[])`true `;
auto ch = text.chunks(3);
assert(ch.parseJson(32).data == [1]);
Asdf parseJson(Flag!"includingNewLine" includingNewLine = Yes.includingNewLine, Flag!"spaces" spaces = Yes.spaces, Flag!"assumeValid" assumeValid = No.assumeValid, Allocator)(in char[] str, auto ref Allocator allocator);

Asdf parseJson(Flag!"includingNewLine" includingNewLine = Yes.includingNewLine, Flag!"spaces" spaces = Yes.spaces, Flag!"assumeValid" assumeValid = No.assumeValid)(in char[] str);
Parses json value
Parameters:
char[] str input string
Allocator allocator (optional) memory allocator
Returns:
ASDF value
Examples:
import std.experimental.allocator.mallocator: Mallocator;
import std.experimental.allocator.showcase: StackFront;

StackFront!(1024, Mallocator) allocator;
auto json = parseJson(`{"ak": {"sub": "subval"} }`, allocator);
assert(json["ak", "sub"] == "subval");
Examples:
Faulty location
import asdf;
try
{
    auto data = `[1, 2, ]`.parseJson;
}
catch(AsdfSerdeException e)
{
    import std.conv;
    /// zero based index
    assert(e.location == 7);
    return;
}
assert(0);
Examples:
assert(`{"ak": {"sub": "subval"} }`.parseJson["ak", "sub"] == "subval");
auto parseJsonByLine(Flag!"spaces" spaces = Yes.spaces, Flag!"throwOnInvalidLines" throwOnInvalidLines = No.throwOnInvalidLines, Input)(Input input);
Parses JSON value in each line from a Range of buffers.
Parameters:
spaces adds support for spaces beetwen json tokens. Default value is Yes.
throwOnInvalidLines throws an SerdeException on invalid lines if Yes and ignore invalid lines if No. Default value is No.
Input input input range composed of elements type of const(ubyte)[] or string / const(char)[]. chunks can use the same buffer for each chunk.
Returns:
Input range composed of ASDF values. Each value uses the same internal buffer.
Examples:
import asdf.jsonparser;
import std.range: chunks;
auto text = cast(const ubyte[])"\t true \r\r\n false\t";
auto values = text.chunks(3).parseJsonByLine;
assert(values.front.data == [1]);
values.popFront;
assert(values.front.data == [2]);
values.popFront;
assert(values.empty);
Examples:
string text =  "\t " ~ `{"key": "a"}` ~ "\r\r\n" ~ `{"key2": "b"}`;
auto values = text.parseJsonByLine();
assert( values.front["key"] == "a");
values.popFront;
assert( values.front["key2"] == "b");
values.popFront;
auto jsonParser(bool includingNewLine, bool hasSpaces, bool assumeValid, Allocator, Input = const(ubyte)[])(auto ref Allocator allocator, Input input);
struct JsonParser(bool includingNewLine, bool hasSpaces, bool assumeValid, Allocator, Input = const(ubyte)[]);