The OpenD Programming Language

jsonSerializer

Creates JSON serialization back-end. Use sep equal to "\t" or " " for pretty formatting.

template jsonSerializer(string sep = "")
jsonSerializer
(
Appender
)
(
return Appender* appender
,
int serdeTarget = SerdeTarget.json
)

Members

Functions

jsonSerializer
auto jsonSerializer(Appender* appender, int serdeTarget)

Examples

import mir.format: stringBuf;
import mir.bignum.integer;

auto buffer = stringBuf;
auto ser = jsonSerializer((()@trusted=>&buffer)(), 3);
auto state0 = ser.structBegin;

    ser.putKey("null");
    ser.putValue(null);

    ser.putKey("array");
    auto state1 = ser.listBegin();
        ser.elemBegin; ser.putValue(null);
        ser.elemBegin; ser.putValue(123);
        ser.elemBegin; ser.putValue(12300000.123);
        ser.elemBegin; ser.putValue("\t");
        ser.elemBegin; ser.putValue("\r");
        ser.elemBegin; ser.putValue("\n");
        ser.elemBegin; ser.putValue(BigInt!2(1234567890));
    ser.listEnd(state1);

ser.structEnd(state0);

assert(buffer.data == `{"null":null,"array":[null,123,1.2300000123e+7,"\t","\r","\n",1234567890]}`);
    import std.array;
    import mir.bignum.integer;

    auto app = appender!string;
    auto ser = jsonSerializer!"    "(&app);
    auto state0 = ser.structBegin;

        ser.putKey("null");
        ser.putValue(null);

        ser.putKey("array");
        auto state1 = ser.listBegin();
            ser.elemBegin; ser.putValue(null);
            ser.elemBegin; ser.putValue(123);
            ser.elemBegin; ser.putValue(12300000.123);
            ser.elemBegin; ser.putValue("\t");
            ser.elemBegin; ser.putValue("\r");
            ser.elemBegin; ser.putValue("\n");
            ser.elemBegin; ser.putValue(BigInt!2("1234567890"));
        ser.listEnd(state1);

    ser.structEnd(state0);

    assert(app.data ==
`{
    "null": null,
    "array": [
        null,
        123,
        1.2300000123e+7,
        "\t",
        "\r",
        "\n",
        1234567890
    ]
}`, app.data);

Meta