JSONStorage

A wrapped JSONValue with helper functions.

Alias This

storage

Members

Enums

KeyOrderStrategy
enum KeyOrderStrategy

Strategy in which to sort object-type JSON keys when we format/serialise the stored storage to string.

Functions

load
void load(string filename)

Loads JSON from disk.

reset
void reset()

Initialises and clears the JSONValue, preparing it for object storage.

save
void save(string filename, string[] givenOrder)

Saves the JSON storage to disk. Formatting is done as specified by the passed KeyOrderStrategy argument.

serialiseInto
void serialiseInto(Sink sink, string[] givenOrder)

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

serialiseInto
void serialiseInto(Sink sink)

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

Variables

storage
JSONValue storage;

The underlying JSONValue storage of this JSONStorage.

Examples

JSONStorage s;

s.reset();  // not always necessary

s.storage["foo"] = null;  // JSONValue quirk
s.storage["foo"]["abc"] = JSONValue(42);
s.storage["foo"]["def"] = JSONValue(3.14f);
s.storage["foo"]["ghi"] = JSONValue([ "bar", "baz", "qux" ]);
s.storage["bar"] = JSONValue("asdf");

assert(s.storage.length == 2);
import std.conv : text;
import std.json : JSONValue;

JSONStorage s;
s.reset();

s.storage["key"] = null;
s.storage["key"]["subkey1"] = "abc";
s.storage["key"]["subkey2"] = "def";
s.storage["key"]["subkey3"] = "ghi";
assert((s.storage["key"].object.length == 3), s.storage["key"].object.length.text);

s.storage["foo"] = null;
s.storage["foo"]["arr"] = JSONValue([ "blah "]);
s.storage["foo"]["arr"].array ~= JSONValue("bluh");
assert((s.storage["foo"]["arr"].array.length == 2), s.storage["foo"]["arr"].array.length.text);

Meta