It should be able to override the default struct serializer
struct A {} string serializer(A) { return "custom value"; } auto registry = new SerializerRegistry(); registry.register(&serializer); registry.serialize(A()).should.equal("custom value"); registry.serialize([A()]).should.equal("[custom value]"); registry.serialize(["key": A()]).should.equal(`["key":custom value]`);
It should be able to override the default const struct serializer
struct A {} string serializer(const A) { return "custom value"; } auto registry = new SerializerRegistry(); registry.register(&serializer); const A value; registry.serialize(value).should.equal("custom value"); registry.serialize([value]).should.equal("[custom value]"); registry.serialize(["key": value]).should.equal(`["key":custom value]`);
It should be able to override the default immutable struct serializer
struct A {} string serializer(immutable A) { return "value"; } auto registry = new SerializerRegistry(); registry.register(&serializer); immutable A ivalue; const A cvalue; A value; registry.serialize(value).should.equal("A()"); registry.serialize(cvalue).should.equal("A()"); registry.serialize(ivalue).should.equal("value"); registry.serialize(ivalue).should.equal("value"); registry.serialize([ivalue]).should.equal("[value]"); registry.serialize(["key": ivalue]).should.equal(`["key":value]`);
It should be able to override the default class serializer
class A {} string serializer(A) { return "custom value"; } auto registry = new SerializerRegistry(); registry.register(&serializer); registry.serialize(new A()).should.equal("custom value"); registry.serialize([new A()]).should.equal("[custom value]"); registry.serialize(["key": new A()]).should.equal(`["key":custom value]`);
It should be able to override the default const class serializer
class A {} string serializer(const A) { return "custom value"; } auto registry = new SerializerRegistry(); registry.register(&serializer); const A value = new A; registry.serialize(value).should.equal("custom value"); registry.serialize([value]).should.equal("[custom value]"); registry.serialize(["key": value]).should.equal(`["key":custom value]`);
It should be able to override the default immutable class serializer
class A {} string serializer(immutable A) { return "value"; } auto registry = new SerializerRegistry(); registry.register(&serializer); immutable A ivalue; const A cvalue; A value; registry.serialize(value).should.equal("null"); registry.serialize(cvalue).should.equal("null"); registry.serialize(ivalue).should.equal("value"); registry.serialize(ivalue).should.equal("value"); registry.serialize([ivalue]).should.equal("[value]"); registry.serialize(["key": ivalue]).should.equal(`["key":value]`);
It should serialize a char
char ch = 'a'; const char cch = 'a'; immutable char ich = 'a'; SerializerRegistry.instance.serialize(ch).should.equal("'a'"); SerializerRegistry.instance.serialize(cch).should.equal("'a'"); SerializerRegistry.instance.serialize(ich).should.equal("'a'");
It should serialize a SysTime
SysTime val = SysTime.fromISOExtString("2010-07-04T07:06:12"); const SysTime cval = SysTime.fromISOExtString("2010-07-04T07:06:12"); immutable SysTime ival = SysTime.fromISOExtString("2010-07-04T07:06:12"); SerializerRegistry.instance.serialize(val).should.equal("2010-07-04T07:06:12"); SerializerRegistry.instance.serialize(cval).should.equal("2010-07-04T07:06:12"); SerializerRegistry.instance.serialize(ival).should.equal("2010-07-04T07:06:12");
It should serialize a string
string str = "aaa"; const string cstr = "aaa"; immutable string istr = "aaa"; SerializerRegistry.instance.serialize(str).should.equal(`"aaa"`); SerializerRegistry.instance.serialize(cstr).should.equal(`"aaa"`); SerializerRegistry.instance.serialize(istr).should.equal(`"aaa"`);
It should serialize an int
int value = 23; const int cvalue = 23; immutable int ivalue = 23; SerializerRegistry.instance.serialize(value).should.equal(`23`); SerializerRegistry.instance.serialize(cvalue).should.equal(`23`); SerializerRegistry.instance.serialize(ivalue).should.equal(`23`);
It should serialize an int list
int[] value = [2,3]; const int[] cvalue = [2,3]; immutable int[] ivalue = [2,3]; SerializerRegistry.instance.serialize(value).should.equal(`[2, 3]`); SerializerRegistry.instance.serialize(cvalue).should.equal(`[2, 3]`); SerializerRegistry.instance.serialize(ivalue).should.equal(`[2, 3]`);
It should serialize a void list
void[] value = []; const void[] cvalue = []; immutable void[] ivalue = []; SerializerRegistry.instance.serialize(value).should.equal(`[]`); SerializerRegistry.instance.serialize(cvalue).should.equal(`[]`); SerializerRegistry.instance.serialize(ivalue).should.equal(`[]`);
It should serialize a nested int list
int[][] value = [[0,1],[2,3]]; const int[][] cvalue = [[0,1],[2,3]]; immutable int[][] ivalue = [[0,1],[2,3]]; SerializerRegistry.instance.serialize(value).should.equal(`[[0, 1], [2, 3]]`); SerializerRegistry.instance.serialize(cvalue).should.equal(`[[0, 1], [2, 3]]`); SerializerRegistry.instance.serialize(ivalue).should.equal(`[[0, 1], [2, 3]]`);
It should serialize an assoc array
int[string] value = ["a": 2,"b": 3, "c": 4]; const int[string] cvalue = ["a": 2,"b": 3, "c": 4]; immutable int[string] ivalue = ["a": 2,"b": 3, "c": 4]; SerializerRegistry.instance.serialize(value).should.equal(`["a":2, "b":3, "c":4]`); SerializerRegistry.instance.serialize(cvalue).should.equal(`["a":2, "b":3, "c":4]`); SerializerRegistry.instance.serialize(ivalue).should.equal(`["a":2, "b":3, "c":4]`);
It should serialize a string enum
enum TestType : string { a = "a", b = "b" } TestType value = TestType.a; const TestType cvalue = TestType.a; immutable TestType ivalue = TestType.a; SerializerRegistry.instance.serialize(value).should.equal(`"a"`); SerializerRegistry.instance.serialize(cvalue).should.equal(`"a"`); SerializerRegistry.instance.serialize(ivalue).should.equal(`"a"`);
Singleton used to serialize to string the tested values