JSONStorage.serialiseInto

Formats an object-type JSON storage into an output range sink.

Top-level keys are sorted as per the passed KeyOrderStrategy. This overload is specialised for strategies other than KeyOrderStrategy.inGivenOrder, and as such takes one parameter fewer.

  1. void serialiseInto(Sink sink, string[] givenOrder)
  2. void serialiseInto(Sink sink)
    struct JSONStorage
    @safe
    void
    serialiseInto
    (
    auto ref Sink sink
    )

Parameters

strategy

Order strategy in which to sort top-level keys.

sink Sink

Output sink to fill with formatted output.

Examples

1         import std.array : Appender;
2         import std.json;
3 
4         JSONStorage this_;
5         Appender!(char[]) sink;
6 
7         // Original JSON
8         this_.storage = parseJSON(
9 `{
10 "#abc":
11 {
12 "hirrsteff" : "o",
13 "foobar" : "v"
14 },
15 "#def":
16 {
17 "harrsteff": "v",
18 "flerpeloso" : "o"
19 },
20 "#zzz":
21 {
22 "asdf" : "v"
23 }
24 }`);
25 
26         // KeyOrderStrategy.passthrough
27         this_.serialiseInto!(KeyOrderStrategy.passthrough)(sink);
28         assert((sink.data ==
29 `{
30     "#abc": {
31         "foobar": "v",
32         "hirrsteff": "o"
33     },
34     "#def": {
35         "flerpeloso": "o",
36         "harrsteff": "v"
37     },
38     "#zzz": {
39         "asdf": "v"
40     }
41 }`), '\n' ~ sink.data);
42         sink.clear();
43 
44         // KeyOrderStrategy.sorted
45         this_.serialiseInto!(KeyOrderStrategy.sorted)(sink);
46         assert((sink.data ==
47 `{
48     "#abc": {
49         "foobar": "v",
50         "hirrsteff": "o"
51     },
52     "#def": {
53         "flerpeloso": "o",
54         "harrsteff": "v"
55     },
56     "#zzz": {
57         "asdf": "v"
58     }
59 }`), '\n' ~ sink.data);
60         sink.clear();
61 
62         // KeyOrderStrategy.reverse
63         this_.serialiseInto!(KeyOrderStrategy.reverse)(sink);
64         assert((sink.data ==
65 `{
66     "#zzz": {
67         "asdf": "v"
68     },
69     "#def": {
70         "flerpeloso": "o",
71         "harrsteff": "v"
72     },
73     "#abc": {
74         "foobar": "v",
75         "hirrsteff": "o"
76     }
77 }`), '\n' ~ sink.data);
78         sink.clear();
79 
80         // KeyOrderStrategy.inGivenOrder
81         this_.serialiseInto!(KeyOrderStrategy.inGivenOrder)(sink, [ "#def", "#abc", "#foo", "#fighters" ]);
82         assert((sink.data ==
83 `{
84     "#def": {
85         "flerpeloso": "o",
86         "harrsteff": "v"
87     },
88     "#abc": {
89         "foobar": "v",
90         "hirrsteff": "o"
91     },
92     "#foo": {},
93     "#fighters": {}
94 }`), '\n' ~ sink.data);
95         sink.clear();
96 
97         // Empty JSONValue
98         JSONStorage this2;
99         this2.serialiseInto(sink);
100         assert((sink.data ==
101 `{}`), '\n' ~ sink.data);
102