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

High level serialization API

Authros Ilya Yaroshenko

void serializeValue(S)(ref S serializer, typeof(null));
null value serialization
Examples:
import mir.ser.json: serializeJson;
assert(serializeJson(null) == `null`, serializeJson(null));
void serializeValue(S, V)(ref S serializer, auto const ref V value)
if (isNumeric!V && !is(V == enum));
Number serialization
Examples:
import mir.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.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.ser.json: serializeJson;
import mir.ser.ion: serializeIon;
import mir.ser.text: serializeText;
import mir.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.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, auto ref V value)
if (isIterable!V && (!hasProxy!V || hasLikeList!V) && !isDynamicArray!V && !hasFields!V && !isAssociativeArray!V && !isStdNullable!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.ser.json: serializeJson;
assert(serializeJson(filtered1) == `[{"i":1},{"i":3},{"i":17}]`);
assert(serializeJson(filtered2) == `[{"i":4}]`);
Examples:
import mir.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.ser.json: serializeJson;
import mir.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.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.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 || hasFields!V || hasUDA!(V, serdeProxy) && !hasUDA!(V, serdeLikeList)));
Struct and class type serialization
void serializeValue(S, V)(ref S serializer, auto ref V value)
if (isAggregateType!V && (!isIterable!V || hasFields!V || hasUDA!(V, serdeProxy) && !hasUDA!(V, serdeLikeList)));
Struct and class type serialization
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);