Marmos
v juptune
v asn1
v lang
v ast
v Classes
v Enums
v ir
v parser
v Enums
v Structs
v core
v crypto
v keyexchange
v memory
v data
v alphabet
v buffer
v json
v builder
v parser
v Enums
v Structs
v serialise
v Structs
v Templates
v pem
v Enums
v Structs

Structs JsonParser

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module juptune.data.json.parser;

struct JsonParser
{
    /++ STRUCTS ++/
    
    /// A token generated by JsonParser.next - ALL character slices are from the original input. 
    static struct Token
    {
        /++ FUNCTIONS ++/
        
        /// Retrieves the value of this boolean token. 
        bool asBool();
        /// Provides an InputRange of const(char)[] which will provide the string value of this string/name token, but with any escape characters transformed into their underlying value. 
        auto asEscapedString();
        /// Retrieves the value of this integer token, and attempts to coerce it into the given IntegralT . 
        Result asInt(IntegralT)(IntegralT number);
        /// Retrieves the string value of this string/name token (without the start and end quotes). 
        const char[] asUnescapedString();
        /// Whether this string/name token has escape characters within it. This can be used to avoid needless allocations in certain use cases (rather than always trying to escape the string). 
        bool hasEscapeChars();
        /// Similar to asInt , but will fail an assert if conversion wasn't possible. 
        IntegralT mustAsInt(IntegralT)();
        
        /++ ENUMS ++/
        
        /// What this token represents. 
        enum Type : int
        {
            ///
            FAILSAFE = 0, 
            /// "{" 
            objectStart = 1, 
            /// "}" 
            objectEnd = 2, 
            /// "[" 
            arrayStart = 3, 
            /// "]" 
            arrayEnd = 4, 
            /// Given {"name": "value", "foo": "bar"} , this token is for "name" and "foo" 
            name = 5, 
            /// true or false 
            boolean = 6, 
            /// null 
            null_ = 7, 
            /// 123 or -123 
            integer = 8, 
            /// [-]1.2, [-]1.2e3, [-]1.2e+3, [-]1.2e-3 
            floating = 9, 
            /// "value" 
            string = 10
        }
        
        /++ VARIABLES ++/
        
        /// The whole raw text of this token, e.g. for strings this includes the start and end quotes. 
        const char[] rawText;
        ///
        JsonParser.Token.Type type;
    }
    
    /++ FUNCTIONS ++/
    
    /// Checks whether the depth buffer is empty (i.e. we're not currently inside an array or object), and that the root-level value has been parsed. 
    Result finish();
    /// Parses the next token from the input. 
    Result next(JsonParser.Token token);
    
}

Documentation

A zero-copy, zero-allocation lexer for parsing in-memory JSON input into tokens.

Notes

This is a low-level interface that a higher-level deserialiser/DOM can be built on top off, this generally isn't the most comfortable thing to use.

All slices produced by this parser are taken from the input, so all lifetimes are directly tied to the input slice's lifetime.**

While floating point numbers are parsed... you can't really access them yet since Juptune lacks string<->float conversion.

Similarly to JsonBuilderthere is absolutely no support for UTF16 surrogate characters.

This is a strict parser, trailing commas aren't allowed (and other nicities like comments aren't supported).

Depth

This struct does not allocate memory, so in order to keep track of whether the parser is inside an object or array, the caller must provide a depth buffer.

Each byte in the buffer allows for 8 levels of depth, so an 8 byte buffer would allow for a max of 64 levels of nesting.

Usage

JsonParser.nextshould be called until the caller has finished reading in its root value. e.g. if the root value is an array, then it should know which arrayEndtoken finishes it off.

Once the caller (thinks) has finished reading data, it should call JsonParser.finishso the parser can confirm there's nothing left to read.

JsonParser.nextprovides an example of what its output for a given input looks like.

Functions

Name Description
finish

Checks whether the depth buffer is empty (i.e. we're not currently inside an array or object), and that the root-level value has been parsed.

next

Parses the next token from the input.

Structs

Name Description
Token

A token generated by JsonParser.next- ALL character slices are from the original input.