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

High level serialization API

Authros Ilya Yaroshenko

void serializeValue(S)(ref S serializer, typeof(null));
null value serialization
Examples:
import mir.ion.ser.json: serializeJson;
assert(serializeJson(null) == `null`, serializeJson(null));
void serializeValue(S, V)(ref S serializer, auto ref const V value)
if (isNumeric!V && !is(V == enum));
Number serialization
Examples:
import mir.ion.ser.json: serializeJson;

assert(serializeJson(2.40f) == `2.4`);
assert(serializeJson(float.nan) == `"nan"`);
assert(serializeJson(float.infinity) == `"+inf"`);
assert(serializeJson(-float.infinity) == `"-inf"`);
void serializeValue(S, V)(ref S serializer, const V value)
if (is(V == bool) && !is(V == enum));
Boolean serialization
void serializeValue(S, V : char)(ref S serializer, const V value)
if (is(V == char) && !is(V == enum));
Char serialization
Examples:
import mir.ion.ser.json: serializeJson;
assert(serializeJson(true) == `true`);
void serializeValue(S, V)(ref S serializer, in V value)
if (is(V == enum));
Enum serialization
Examples:
import mir.ion.ser.json: serializeJson;
import mir.ion.ser.ion: serializeIon;
import mir.ion.ser.text: serializeText;
import mir.ion.deser.ion: deserializeIon;
import mir.small_string;
import mir.rc.array;
enum Key { bar, @serdeKeys("FOO", "foo") foo }
assert(serializeJson(Key.foo) == `"FOO"`);
assert(serializeText(Key.foo) == `FOO`);
assert(serializeIon(Key.foo).deserializeIon!Key == Key.foo);
assert(serializeIon(Key.foo).deserializeIon!string == "FOO");
assert(serializeIon(Key.foo).deserializeIon!(SmallString!32) == "FOO");
auto rcstring = serializeIon(Key.foo).deserializeIon!(RCArray!char);
assert(rcstring[] == "FOO");
void serializeValue(S)(ref S serializer, scope const(char)[] value);
String serialization
Examples:
import mir.ion.ser.json: serializeJson;
assert(serializeJson("\t \" \\") == `"\t \" \\"`, serializeJson("\t \" \\"));
void serializeValue(S, T)(ref S serializer, T[] value)
if (!isSomeChar!T);
Array serialization
void serializeValue(S, V)(ref S serializer, V value)
if (isIterable!V && !isSomeChar!(ForeachType!V) && !isDynamicArray!V && !isNullable!V);
Input range serialization
Examples:
input range serialization
import std.algorithm : filter;

struct Foo
{
    int i;
}

auto ar = [Foo(1), Foo(3), Foo(4), Foo(17)];

auto filtered1 = ar.filter!"a.i & 1";
auto filtered2 = ar.filter!"!(a.i & 1)";

import mir.ion.ser.json: serializeJson;
assert(serializeJson(filtered1) == `[{"i":1},{"i":3},{"i":17}]`);
assert(serializeJson(filtered2) == `[{"i":4}]`);
Examples:
import mir.ion.ser.json: serializeJson;
uint[2] ar = [1, 2];
assert(serializeJson(ar) == `[1,2]`);
assert(serializeJson(ar[]) == `[1,2]`);
assert(serializeJson(ar[0 .. 0]) == `[]`);
assert(serializeJson((uint[]).init) == `[]`);
void serializeValue(S, T)(ref S serializer, auto ref T[string] value);
String-value associative array serialization
Examples:
import mir.ion.ser.json: serializeJson;
import mir.ion.ser.text: serializeText;
uint[string] ar = ["a" : 1];
assert(serializeJson(ar) == `{"a":1}`);
assert(serializeText(ar) == `{a:1}`);
ar.remove("a");
assert(serializeJson(ar) == `{}`);
assert(serializeJson((uint[string]).init) == `{}`);
void serializeValue(S, V : const(T[K]), T, K)(ref S serializer, V value)
if (is(K == enum));
Enumeration-value associative array serialization
Examples:
import mir.ion.ser.json: serializeJson;
enum E { a, b }
uint[E] ar = [E.a : 1];
assert(serializeJson(ar) == `{"a":1}`);
ar.remove(E.a);
assert(serializeJson(ar) == `{}`);
assert(serializeJson((uint[string]).init) == `{}`);
void serializeValue(S, V : const(T[K]), T, K)(ref S serializer, V value)
if (isIntegral!K && !is(K == enum));
integral typed value associative array serialization
Examples:
import mir.ion.ser.json: serializeJson;
uint[short] ar = [256 : 1];
assert(serializeJson(ar) == `{"256":1}`);
ar.remove(256);
assert(serializeJson(ar) == `{}`);
assert(serializeJson((uint[string]).init) == `{}`);
// assert(deserializeJson!(uint[short])(`{"256":1}`) == cast(uint[short]) [256 : 1]);
void serializeValueImpl(S, V)(ref S serializer, auto ref V value)
if (isAggregateType!V && (!isIterable!V || hasUDA!(V, serdeProxy)));
Struct and class type serialization
void serializeValue(S, V)(ref S serializer, auto ref V value)
if (isAggregateType!V && (!isIterable!V || hasUDA!(V, serdeProxy)));
Struct and class type serialization
Examples:
Mir types
import mir.bignum.integer;
import mir.date;
import mir.ion.ser.json: serializeJson;
import mir.ion.ser.text: serializeText;
assert(Date(2021, 4, 24).serializeJson == `"2021-04-24"`);
assert(BigInt!2(123).serializeJson == `123`);
assert(Date(2021, 4, 24).serializeText == `2021-04-24`);
assert(BigInt!2(123).serializeText == `123`);
Examples:
Alias this support
struct S
{
    int u;
}

struct C
{
    int b;
    S s;
    alias s this; 
}

import mir.ion.ser.json: serializeJson;
assert(C(4, S(3)).serializeJson == `{"u":3,"b":4}`);
Examples:
Custom serialize
struct S
{
    void serialize(S)(ref S serializer) const
    {
        auto state = serializer.structBegin;
        serializer.putKey("foo");
        serializer.putValue("bar");
        serializer.structEnd(state);
    }
}

import mir.ion.ser.json: serializeJson;
assert(serializeJson(S()) == `{"foo":"bar"}`);
Examples:
Nullable type serialization
import mir.ion.ser.json: serializeJson;
import mir.algebraic: Nullable;

struct Nested
{
    float f;
}

struct T
{
    string str;
    Nullable!Nested nested;
}

T t;
assert(t.serializeJson == `{"str":"","nested":{}}`);
t.str = "txt";
t.nested = Nested(123);
assert(t.serializeJson == `{"str":"txt","nested":{"f":123.0}}`);
Examples:
import mir.algebraic;
import mir.small_string;

@serdeAlgebraicAnnotation("$a")
static struct B
{
    double number;
}

static struct A
{
    @serdeAnnotation
    SmallString!32 id1;

    @serdeAnnotation
    string[] id2;

    // Alias this is transparent for members and can catch algebraic annotation
    alias c this;
    B c;

    string s;
}


static assert(serdeHasAlgebraicAnnotation!B);
static assert(serdeGetAlgebraicAnnotation!B == "$a");
static assert(serdeHasAlgebraicAnnotation!A);
static assert(serdeGetAlgebraicAnnotation!A == "$a");

@serdeAlgebraicAnnotation("$c")
static struct C
{
}

@serdeAlgebraicAnnotation("$S")
static struct S
{
    @serdeAnnotation
    string sid;

    alias Data = Nullable!(A, C, int);

    alias data this;

    Data data;
}


@serdeAlgebraicAnnotation("$S2")
static struct S2
{
    alias Data = Nullable!(A, C, int);

    alias data this;

    Data data;
}


import mir.ion.conv: ion2text;
import mir.ion.ser.ion: serializeIon;
import mir.ion.ser.text: serializeText;

{
    Nullable!S value = S("LIBOR", S.Data(A("Rate".SmallString!32, ["USD", "GBP"])));
    static immutable text = `LIBOR::$a::Rate::USD::GBP::{number:nan,s:null.string}`;
    assert(value.serializeText == text);
    auto binary = value.serializeIon;
    assert(binary.ion2text == text);
    import mir.ion.deser.ion: deserializeIon;
    assert(binary.deserializeIon!S.serializeText == text);
}

{
    auto value = S2(S2.Data(A("Rate".SmallString!32, ["USD", "GBP"])));
    static immutable text = `$a::Rate::USD::GBP::{number:nan,s:null.string}`;
    auto binary = value.serializeIon;
    assert(binary.ion2text == text);
    import mir.ion.deser.ion: deserializeIon;
    assert(binary.deserializeIon!S2.serializeText == text);
}
auto beginList(S, V)(ref S serializer, ref V value);
auto beginSexp(S, V)(ref S serializer, ref V value);
auto beginStruct(S, V)(ref S serializer, ref V value);