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 JsonBuilder

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
module juptune.data.json.builder;

struct JsonBuilder(
    SinkT
)
{
    /++ FUNCTIONS ++/
    
    /// Selectively calls endObject or endArray depending on the latest value in the depth buffer. 
    Result end()();
    /// Ends the current array. 
    Result endArray()();
    /// Ends the current object. 
    Result endObject()();
    /// Checks whether the depth buffer is empty (i.e. the value can be considered "finished"). 
    Result finish();
    ///
    bool inArray();
    ///
    bool inObject();
    ///
    Result put()(const char[] str);
    ///
    Result put(IntT)(IntT number);
    ///
    Result put()(bool bool_);
    ///
    Result put()(typeof(TODO) _);
    /// Puts a value within the current array. 
    Result putArrayValue(ItemT)(ItemT value);
    /// Puts a value with the specified key within the current object. 
    Result putObjectValue(ItemT)(const char[] key, ItemT value);
    /// Starts a new array, using the given key if this array is nested inside an object. 
    Result startArray()(const char[] keyIfInObject = "");
    /// Starts a new object, using the given key if this object is nested inside another object. 
    Result startObject()(const char[] keyIfInObject = "");
    
    /++ MANIFEST CONSTANTS ++/
    
    ///
    enum int BITS_PER_BYTE = 8;
    
    /++ VARIABLES ++/
    
    ///
    size_t _depth;
    ///
    ubyte[] _depthMarkers;
    ///
    bool _isFirstItem;
    ///
    SinkT _sink;
}

Documentation

A lowish-level builder for writing JSON output to a sink.

Depth

This struct does not allocate memory, so in order to keep track of whether the builder 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.

Values

The following types can be passed as values to the putObjectValueand putArrayValuefunctions:

const(char)[]for strings - strings will be ran through juptune.data.utf8.utf8Validate, and outputted as UTF-8.

bool- handled as you'd expect.

Any integer type - signed and unsigned are handled correctly.

Currently there's no support for floating point types due to the lack of a string converter for these types.

Sink

The SinkTshould be a type that matches the following: Result delegate(scope const(char)[] text).

This type is templated in order to easily support the sink having whatever sleuth of attributes you desire. Most functions in this struct are templated so that the compiler can infer attributes.

You'll usually need to store the delegate inside a separate scope variable in order to use this struct under @nogc- e.g. scope sink = delegate(scope const(char)[] text) { return Result.noError; }.

Limitations

This builder does not support outputting UTF-16 surrogate pairs, so some parsers may choke on the generated output if they follow the JSON RFC strictly.

This builder currently does not support pretty printing, largely due to lack of effort :D

This builder does not prevent certain common errors, such as ensuring object keys are unique. This is because it's assumed stuff like this has been handled at a higher level before calling into the builder.

Constants

Name Description
BITS_PER_BYTE

Functions

Name Description
end

Selectively calls endObjector endArraydepending on the latest value in the depth buffer.

endArray

Ends the current array.

endObject

Ends the current object.

finish

Checks whether the depth buffer is empty (i.e. the value can be considered "finished").

inArray
inObject
put
put
put
put
putArrayValue

Puts a value within the current array.

putObjectValue

Puts a value with the specified key within the current object.

startArray

Starts a new array, using the given key if this array is nested inside an object.

startObject

Starts a new object, using the given key if this object is nested inside another object.

Variables

Name Description
_depth
_depthMarkers
_isFirstItem
_sink