serialise

Serialises the fields of an object into an .ini file-like format.

It only serialises fields not annotated with Unserialisable, and it doesn't recurse into other structs or classes.

  1. void serialise(Sink sink, Things things)
  2. void serialise(Sink sink, QualThing thing)
    void
    serialise
    (
    Sink
    QualThing
    )
    (
    auto ref Sink sink
    ,
    auto ref QualThing thing
    )

Parameters

sink Sink

Reference output range to write to, usually an Appender.

thing QualThing

Object to serialise.

Examples

struct Foo
{
    // ...
}

Foo foo;

Appender!(char[]) sink;

sink.serialise(foo);
assert(!sink.data.empty);
1     import lu.uda : Separator, Quoted;
2     import std.array : Appender;
3 
4     struct FooSettings
5     {
6         string fooasdf = "foo 1";
7         string bar = "foo 1";
8         string bazzzzzzz = "foo 1";
9         @Quoted flerrp = "hirr steff  ";
10         double pi = 3.14159;
11         @Separator(",") int[] arr = [ 1, 2, 3 ];
12         @Separator(";") string[] harbl = [ "harbl;;", ";snarbl;", "dirp" ];
13 
14         static if (__VERSION__ >= 2087L)
15         {
16             @("|") string[] matey = [ "a", "b", "c" ];
17         }
18     }
19 
20     struct BarSettings
21     {
22         string foofdsa = "foo 2";
23         string bar = "bar 2";
24         string bazyyyyyyy = "baz 2";
25         @Quoted flarrp = "   hirrsteff";
26         double pipyon = 3.0;
27     }
28 
29     static if (__VERSION__ >= 2087L)
30     {
31         enum fooSerialised =
32 `[Foo]
33 fooasdf foo 1
34 bar foo 1
35 bazzzzzzz foo 1
36 flerrp "hirr steff  "
37 pi 3.14159
38 arr 1,2,3
39 harbl harbl\;\;;\;snarbl\;;dirp
40 matey a|b|c`;
41     }
42     else
43     {
44         enum fooSerialised =
45 `[Foo]
46 fooasdf foo 1
47 bar foo 1
48 bazzzzzzz foo 1
49 flerrp "hirr steff  "
50 pi 3.14159
51 arr 1,2,3
52 harbl harbl\;\;;\;snarbl\;;dirp`;
53     }
54 
55     Appender!(char[]) fooSink;
56     fooSink.reserve(64);
57 
58     fooSink.serialise(FooSettings.init);
59     assert((fooSink.data == fooSerialised), '\n' ~ fooSink.data);
60 
61     enum barSerialised =
62 `[Bar]
63 foofdsa foo 2
64 bar bar 2
65 bazyyyyyyy baz 2
66 flarrp "   hirrsteff"
67 pipyon 3`;
68 
69     Appender!(char[]) barSink;
70     barSink.reserve(64);
71 
72     barSink.serialise(BarSettings.init);
73     assert((barSink.data == barSerialised), '\n' ~ barSink.data);
74 
75     // try two at once
76     Appender!(char[]) bothSink;
77     bothSink.reserve(128);
78     bothSink.serialise(FooSettings.init, BarSettings.init);
79     assert(bothSink.data == fooSink.data ~ "\n\n" ~ barSink.data);
80 
81     class C
82     {
83         int i;
84         bool b;
85     }
86 
87     C c = new C;
88     c.i = 42;
89     c.b = true;
90 
91     enum cSerialised =
92 `[C]
93 i 42
94 b true`;
95 
96     Appender!(char[]) cSink;
97     cSink.reserve(128);
98     cSink.serialise(c);
99     assert((cSink.data == cSerialised), '\n' ~ cSink.data);
100 
101     enum Letters { abc, def, ghi, }
102 
103     struct Struct
104     {
105         Letters let = Letters.def;
106     }
107 
108     enum enumTestSerialised =
109 `[Struct]
110 let def`;
111 
112     Struct st;
113     Appender!(char[]) enumTestSink;
114     enumTestSink.serialise(st);
115     assert((enumTestSink.data == enumTestSerialised), '\n' ~ enumTestSink.data);