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
    (
    string suffixToStrip = "Settings"
    Sink
    QualThing
    )
    (
    auto ref Sink sink
    ,
    auto ref QualThing thing
    )

Parameters

suffixToStrip

Substring to strip off the end of struct names when writing them to the output range. Defaults to "Settings". May be empty.

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