serializeText

Ion serialization function.

  1. string serializeText(V value, int serdeTarget)
    string
    serializeText
    (
    V
    )
    (
    auto scope ref const V value
    ,
    int serdeTarget = SerdeTarget.ion
    )
  2. void serializeText(Appender appender, V value, int serdeTarget)

Examples

struct S
{
    string foo;
    uint bar;
}

assert(serializeText(S("str", 4)) == `{foo:"str",bar:4}`, serializeText(S("str", 4)));
import mir.serde: serdeIgnoreDefault;

static struct Decor
{
    int candles; // 0
    float fluff = float.infinity; // inf 
}

static struct Cake
{
    @serdeIgnoreDefault
    string name = "Chocolate Cake";
    int slices = 8;
    float flavor = 1;
    @serdeIgnoreDefault
    Decor dec = Decor(20); // { 20, inf }
}

assert(Cake("Normal Cake").serializeText == `{name:"Normal Cake",slices:8,flavor:1.0}`);
auto cake = Cake.init;
cake.dec = Decor.init;
assert(cake.serializeText == `{slices:8,flavor:1.0,dec:{candles:0,fluff:+inf}}`, cake.serializeText);
assert(cake.dec.serializeText == `{candles:0,fluff:+inf}`);

static struct A
{
    @serdeIgnoreDefault
    string str = "Banana";
    int i = 1;
}
assert(A.init.serializeText == `{i:1}`);

static struct S
{
    @serdeIgnoreDefault
    A a;
}
assert(S.init.serializeText == `{}`);
assert(S(A("Berry")).serializeText == `{a:{str:"Berry",i:1}}`);

static struct D
{
    S s;
}
assert(D.init.serializeText == `{s:{}}`);
assert(D(S(A("Berry"))).serializeText == `{s:{a:{str:"Berry",i:1}}}`);
assert(D(S(A(null, 0))).serializeText == `{s:{a:{str:null.string,i:0}}}`, D(S(A(null, 0))).serializeText);

static struct F
{
    D d;
}
assert(F.init.serializeText == `{d:{s:{}}}`);
import mir.serde: serdeIgnoreIn;

static struct S
{
    @serdeIgnoreIn
    string s;
}
// assert(`{"s":"d"}`.deserializeText!S.s == null, `{"s":"d"}`.serializeText!S.s);
assert(S("d").serializeText == `{s:"d"}`);
import mir.deser.ion;

static struct S
{
    @serdeIgnoreOut
    string s;
}
// assert(`{s:"d"}`.serializeText!S.s == "d");
assert(S("d").serializeText == `{}`);
import mir.serde: serdeIgnoreOutIf;

static struct S
{
    @serdeIgnoreOutIf!`a < 0`
    int a;
}

assert(serializeText(S(3)) == `{a:3}`, serializeText(S(3)));
assert(serializeText(S(-3)) == `{}`);
import mir.rc.array;
auto ar = rcarray!int(1, 2, 4);
assert(ar.serializeText == "[1,2,4]");
import mir.deser.ion;
import std.range;
import std.algorithm;
import std.conv;

static struct S
{
    @serdeTransformIn!"a += 2"
    @serdeTransformOut!(a =>"str".repeat.take(a).joiner("_").to!string)
    int a;
}

assert(serializeText(S(5)) == `{a:"str_str_str_str_str"}`);

Meta