Mir types
import mir.bignum.integer; import mir.date; import mir.ser.json: serializeJson; import mir.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`);
Alias this support
struct S { int u; } struct C { int b; S s; alias s this; } import mir.ser.json: serializeJson; assert(C(4, S(3)).serializeJson == `{"u":3,"b":4}`);
Custom serialize
struct S { void serialize(S)(scope ref S serializer) scope const @safe { auto state = serializer.structBegin(1); serializer.putKey("foo"); serializer.putValue("bar"); serializer.structEnd(state); } } import mir.ser.json: serializeJson; assert(serializeJson(S()) == `{"foo":"bar"}`);
Nullable type serialization
import mir.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}}`);
1 import mir.algebraic; 2 import mir.small_string; 3 4 @serdeAlgebraicAnnotation("$B") 5 static struct B 6 { 7 double number; 8 } 9 10 @serdeAlgebraicAnnotation("$A") 11 static struct A 12 { 13 @serdeAnnotation 14 SmallString!32 id1; 15 16 @serdeAnnotation 17 string[] id2; 18 19 B c; 20 alias c this; 21 22 string s; 23 } 24 25 26 static assert(serdeHasAlgebraicAnnotation!B); 27 static assert(serdeGetAlgebraicAnnotation!B == "$B"); 28 static assert(serdeHasAlgebraicAnnotation!A); 29 static assert(serdeGetAlgebraicAnnotation!A == "$A"); 30 31 @serdeAlgebraicAnnotation("$c") 32 static struct C 33 { 34 } 35 36 @serdeAlgebraicAnnotation("$E") 37 enum E { e1, e2 } 38 39 @serdeAlgebraicAnnotation("$S") 40 static struct S 41 { 42 @serdeAnnotation 43 string sid; 44 45 @serdeAnnotation 46 string sid2; 47 48 alias Data = Nullable!(A, C, long, E); 49 50 alias data this; 51 52 Data data; 53 } 54 55 56 @serdeAlgebraicAnnotation("$Y") 57 static struct Y 58 { 59 alias Data = Nullable!(A, C, long); 60 61 alias data this; 62 63 Data data; 64 } 65 66 67 import mir.ion.conv: ion2text; 68 import mir.ser.ion: serializeIon; 69 import mir.ser.text: serializeText; 70 import mir.test; 71 72 () { 73 Nullable!S value = S("LIBOR", "S", S.Data(A("Rate".SmallString!32, ["USD", "GBP"]))); 74 static immutable text = `LIBOR::S::$A::Rate::USD::GBP::{number:nan,s:null.string}`; 75 value.serializeText.should == text; 76 auto binary = value.serializeIon; 77 binary.ion2text.should == text; 78 import mir.deser.ion: deserializeIon; 79 binary.deserializeIon!S.serializeText.should == text; 80 } (); 81 82 () { 83 S value = S("LIBOR", "S", S.Data(E.e2)); 84 static immutable text = `LIBOR::S::$E::e2`; 85 value.serializeText.should == text; 86 auto binary = value.serializeIon; 87 binary.ion2text.should == text; 88 import mir.deser.ion: deserializeIon; 89 binary.deserializeIon!S.serializeText.should == text; 90 } (); 91 92 () { 93 auto value = Y(Y.Data(A("Rate".SmallString!32, ["USD", "GBP"]))); 94 static immutable text = `$A::Rate::USD::GBP::{number:nan,s:null.string}`; 95 auto binary = value.serializeIon; 96 binary.ion2text.should == text; 97 import mir.deser.ion: deserializeIon; 98 binary.deserializeIon!Y.serializeText.should == text; 99 } (); 100 101 () { 102 auto value = S("USD", "S", S.Data(3)); 103 static immutable text = `USD::S::3`; 104 auto binary = value.serializeIon; 105 binary.ion2text.should == text; 106 import mir.deser.ion: deserializeIon; 107 binary.deserializeIon!S.serializeText.should == text; 108 } ();