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.

mir.ion.ser.script

The module can be used for scripting languages to register a universal type serializer in the type system.
interface ISerializer;
Unified serializer interface
abstract pure scope @safe void putStringPart(scope const(char)[] value);
Puts string part. The implementation allows to split string unicode points.
abstract pure scope @safe void stringEnd(size_t state);
abstract pure scope @safe size_t structBegin(size_t length = 0);
abstract pure scope @safe void structEnd(size_t state);
abstract pure scope @safe size_t listBegin(size_t length = 0);
abstract pure scope @safe void listEnd(size_t state);
abstract pure scope @safe size_t sexpBegin(size_t length = 0);
abstract pure scope @safe void sexpEnd(size_t state);
abstract pure scope @safe void putSymbol(scope const char[] symbol);
abstract pure scope @safe size_t annotationsBegin();
abstract pure scope @safe void putAnnotation(scope const(char)[] annotation);
abstract pure scope @safe void annotationsEnd(size_t state);
abstract pure scope @safe size_t annotationWrapperBegin();
abstract pure scope @safe void annotationWrapperEnd(size_t state);
abstract pure scope @safe void nextTopLevelValue();
abstract pure scope @safe void putKey(scope const char[] key);
abstract pure scope @safe void putValue(long value);
abstract pure scope @safe void putValue(ulong value);
abstract pure scope @safe void putValue(float value);
abstract pure scope @safe void putValue(double value);
abstract pure scope @safe void putValue(real value);
abstract pure scope @safe void putValue(BigIntView!(const(ubyte), WordEndian.big) value);
abstract pure scope @safe void putValue(ref const Decimal!256 value);
abstract pure scope @safe void putValue(typeof(null));

abstract pure scope @safe void putNull(IonTypeCode code);
abstract pure scope @safe void putValue(bool b);
abstract pure scope @safe void putValue(scope const char[] value);
abstract pure scope @safe void putValue(Clob value);
abstract pure scope @safe void putValue(Blob value);
abstract pure scope @safe void putValue(Timestamp value);
abstract pure scope @safe void elemBegin();
abstract pure scope @safe void sexpElemBegin();
abstract const pure nothrow @property scope @safe int serdeTarget();
class SerializerWrapper(S): ISerializer;
Serializer interface wrapper for common serializers.
Examples:
static struct Wrapper(T)
{
    T value;

    void serialize(S)(ref S serializer) const @safe
    {
        import mir.ion.ser: serializeValue;
        scope s = new SerializerWrapper!S(serializer);
        auto i = s.ISerializer;
        serializeValue(i, value);
    }
}

static auto wrap(T)(T value)
{
    return Wrapper!T(value);
}

import mir.ion.conv;
import mir.ion.ser.ion;
import mir.ion.ser.json;
import mir.ion.ser.text;
import mir.ion.stream;
import std.datetime.date;

assert(wrap(Date(1234, 5, 6)).serializeJson == `"1234-05-06"`);
assert(wrap(Date(1234, 5, 6)).serializeText == `1234-05-06`);
assert(wrap(Date(1234, 5, 6)).serializeIon.ion2text == `1234-05-06`);

const ubyte[] data = [0xe0, 0x01, 0x00, 0xea, 0xe9, 0x81, 0x83, 0xd6, 0x87, 0xb4, 0x81, 0x61, 0x81, 0x62, 0xd6, 0x8a, 0x21, 0x01, 0x8b, 0x21, 0x02];
auto json = wrap(data.IonValueStream).serializeJson;
assert(json == `{"a":1,"b":2}`);
pure nothrow @nogc @trusted this(return ref S serializer);