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.ser.interfaces

The module can be used for scripting languages to register a universal type serializer in the type system.
interface ISerializer;
Unified serializer interface.
ISerializer can be used in serialize method instead of generic serilizer.
struct S
{
    void serialize(ISerializer serializer) const @safe
    ...
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 = size_t.max);
abstract pure scope @safe void structEnd(size_t state);
abstract pure scope @safe size_t listBegin(size_t length = size_t.max);
abstract pure scope @safe void listEnd(size_t state);
abstract pure scope @safe size_t sexpBegin(size_t length = size_t.max);
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);
final pure scope @safe void putValue(ubyte value);
final pure scope @safe void putValue(ushort value);
final pure scope @safe void putValue(uint value);
final pure scope @safe void putValue(byte value);
final pure scope @safe void putValue(short value);
final pure scope @safe void putValue(int value);
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(const ref 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(ISerializer serializer) const @safe
    {
        import mir.ser: serializeValue;
        serializeValue(serializer, value);
    }
}

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

import mir.ion.conv;
import mir.ser.ion;
import mir.ser.json;
import mir.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`);
immutable(ushort)[] imdata = [10, 20, 30];
assert(wrap(imdata).serializeIon.ion2text == `[10,20,30]`);

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);