mirror of https://github.com/Hax-io/libpb
Refactored various components into their own modules
This commit is contained in:
parent
3e853e7431
commit
15fe95f532
|
@ -0,0 +1,131 @@
|
||||||
|
module libpb.deserialization;
|
||||||
|
|
||||||
|
import std.json;
|
||||||
|
|
||||||
|
public RecordType fromJSON(RecordType)(JSONValue jsonIn)
|
||||||
|
{
|
||||||
|
RecordType record;
|
||||||
|
|
||||||
|
import std.traits;
|
||||||
|
import std.meta : AliasSeq;
|
||||||
|
|
||||||
|
// Alias as to only expand later when used in compile-time
|
||||||
|
alias structTypes = FieldTypeTuple!(RecordType);
|
||||||
|
alias structNames = FieldNameTuple!(RecordType);
|
||||||
|
alias structValues = record.tupleof;
|
||||||
|
|
||||||
|
static foreach(cnt; 0..structTypes.length)
|
||||||
|
{
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg, structTypes[cnt]);
|
||||||
|
pragma(msg, structNames[cnt]);
|
||||||
|
// pragma(msg, structValues[cnt]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Add all integral types
|
||||||
|
static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(int)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(uint)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(ulong)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(long)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].str();
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]];
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//FIXME: Not sure how to get array support going, very new to meta programming
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), mixin(structTypes[cnt])[]))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// throw new
|
||||||
|
//TODO: Throw error
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg, "Unknown type for de-serialization");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.string : cmp;
|
||||||
|
import std.stdio : writeln;
|
||||||
|
|
||||||
|
struct Person
|
||||||
|
{
|
||||||
|
public string firstname, lastname;
|
||||||
|
public int age;
|
||||||
|
public bool isMale;
|
||||||
|
public JSONValue obj;
|
||||||
|
public int[] list;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONValue json = parseJSON(`{
|
||||||
|
"firstname" : "Tristan",
|
||||||
|
"lastname": "Kildaire",
|
||||||
|
"age": 23,
|
||||||
|
"obj" : {"bruh":1},
|
||||||
|
"isMale": true,
|
||||||
|
"list": [1,2,3]
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
|
||||||
|
Person person = fromJSON!(Person)(json);
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
writeln(person);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(cmp(person.firstname, "Tristan") == 0);
|
||||||
|
assert(cmp(person.lastname, "Kildaire") == 0);
|
||||||
|
assert(person.age == 23);
|
||||||
|
assert(person.isMale == true);
|
||||||
|
assert(person.obj["bruh"].integer() == 1);
|
||||||
|
//TODO: list test case
|
||||||
|
}
|
|
@ -1,69 +1,16 @@
|
||||||
module libpb;
|
module libpb.driver;
|
||||||
|
|
||||||
import std.json;
|
import std.json;
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.net.curl;
|
import std.net.curl;
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
import std.string : cmp;
|
import std.string : cmp;
|
||||||
|
import libpb.exceptions;
|
||||||
public class PBException : Exception
|
import libpb.serialization;
|
||||||
{
|
import libpb.deserialization;
|
||||||
this()
|
|
||||||
{
|
|
||||||
super("bruh todo");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final class RecordNotFoundException : PBException
|
|
||||||
{
|
|
||||||
public const string offendingTable;
|
|
||||||
public const string offendingId;
|
|
||||||
this(string table, string id)
|
|
||||||
{
|
|
||||||
this.offendingTable = table;
|
|
||||||
this.offendingId = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final class NotAuthorized : PBException
|
|
||||||
{
|
|
||||||
public const string offendingTable;
|
|
||||||
public const string offendingId;
|
|
||||||
this(string table, string id)
|
|
||||||
{
|
|
||||||
this.offendingTable = table;
|
|
||||||
this.offendingId = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final class ValidationRequired : PBException
|
|
||||||
{
|
|
||||||
public const string offendingTable;
|
|
||||||
public const string offendingId;
|
|
||||||
this(string table, string id)
|
|
||||||
{
|
|
||||||
this.offendingTable = table;
|
|
||||||
this.offendingId = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
private mixin template AuthTokenHeader(alias http, PocketBase pbInstance)
|
||||||
public final class NetworkException : PBException
|
|
||||||
{
|
|
||||||
this()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final class PocketBaseParsingException : PBException
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mixin template AuthTokenHeader(alias http, PocketBase pbInstance)
|
|
||||||
{
|
{
|
||||||
// Must be an instance of HTTP from `std.curl`
|
// Must be an instance of HTTP from `std.curl`
|
||||||
static assert(__traits(isSame, typeof(http), HTTP));
|
static assert(__traits(isSame, typeof(http), HTTP));
|
||||||
|
@ -143,7 +90,7 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
writeln("Invalid return from curl_easy_escape");
|
writeln("Invalid return from curl_easy_escape");
|
||||||
}
|
}
|
||||||
throw new PBException();
|
throw new NetworkException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert back to D-string (the filter)
|
// Convert back to D-string (the filter)
|
||||||
|
@ -461,245 +408,6 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JSONValue serializeRecord(RecordType)(RecordType record)
|
|
||||||
{
|
|
||||||
import std.traits;
|
|
||||||
import std.meta : AliasSeq;
|
|
||||||
|
|
||||||
// Final JSON to submit
|
|
||||||
JSONValue builtJSON;
|
|
||||||
|
|
||||||
|
|
||||||
// Alias as to only expand later when used in compile-time
|
|
||||||
alias structTypes = FieldTypeTuple!(RecordType);
|
|
||||||
alias structNames = FieldNameTuple!(RecordType);
|
|
||||||
alias structValues = record.tupleof;
|
|
||||||
|
|
||||||
static foreach(cnt; 0..structTypes.length)
|
|
||||||
{
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, structTypes[cnt]);
|
|
||||||
pragma(msg, structNames[cnt]);
|
|
||||||
// pragma(msg, structValues[cnt]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
|
||||||
{
|
|
||||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, "Yaa");
|
|
||||||
}
|
|
||||||
builtJSON[structNames[cnt]] = to!(string)(structValues[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return builtJSON;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static fromJSON(RecordType)(JSONValue jsonIn)
|
|
||||||
{
|
|
||||||
RecordType record;
|
|
||||||
|
|
||||||
import std.traits;
|
|
||||||
import std.meta : AliasSeq;
|
|
||||||
|
|
||||||
// Alias as to only expand later when used in compile-time
|
|
||||||
alias structTypes = FieldTypeTuple!(RecordType);
|
|
||||||
alias structNames = FieldNameTuple!(RecordType);
|
|
||||||
alias structValues = record.tupleof;
|
|
||||||
|
|
||||||
static foreach(cnt; 0..structTypes.length)
|
|
||||||
{
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, structTypes[cnt]);
|
|
||||||
pragma(msg, structNames[cnt]);
|
|
||||||
// pragma(msg, structValues[cnt]);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: Add all integral types
|
|
||||||
static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(int)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(uint)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(ulong)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(long)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].str();
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]];
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//FIXME: Not sure how to get array support going, very new to meta programming
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), mixin(structTypes[cnt])[]))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// throw new
|
|
||||||
//TODO: Throw error
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, "Unknown type for de-serialization");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return record;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum EnumType
|
|
||||||
{
|
|
||||||
DOG,
|
|
||||||
CAT
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test serialization of a struct to JSON
|
|
||||||
unittest
|
|
||||||
{
|
|
||||||
import std.algorithm.searching : canFind;
|
|
||||||
import std.string : cmp;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct Person
|
|
||||||
{
|
|
||||||
public string firstname, lastname;
|
|
||||||
public int age;
|
|
||||||
public string[] list;
|
|
||||||
public JSONValue extraJSON;
|
|
||||||
public EnumType eType;
|
|
||||||
}
|
|
||||||
|
|
||||||
Person p1;
|
|
||||||
p1.firstname = "Tristan";
|
|
||||||
p1.lastname = "Kildaire";
|
|
||||||
p1.age = 23;
|
|
||||||
p1.list = ["1", "2", "3"];
|
|
||||||
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
|
|
||||||
p1.eType = EnumType.CAT;
|
|
||||||
|
|
||||||
JSONValue serialized = PocketBase.serializeRecord(p1);
|
|
||||||
|
|
||||||
string[] keys = serialized.object().keys();
|
|
||||||
assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0);
|
|
||||||
assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0);
|
|
||||||
assert(canFind(keys, "age") && serialized["age"].integer() == 23);
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
writeln(serialized.toPrettyString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
unittest
|
|
||||||
{
|
|
||||||
import std.string : cmp;
|
|
||||||
|
|
||||||
struct Person
|
|
||||||
{
|
|
||||||
public string firstname, lastname;
|
|
||||||
public int age;
|
|
||||||
public bool isMale;
|
|
||||||
public JSONValue obj;
|
|
||||||
public int[] list;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONValue json = parseJSON(`{
|
|
||||||
"firstname" : "Tristan",
|
|
||||||
"lastname": "Kildaire",
|
|
||||||
"age": 23,
|
|
||||||
"obj" : {"bruh":1},
|
|
||||||
"isMale": true,
|
|
||||||
"list": [1,2,3]
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
|
|
||||||
Person person = PocketBase.fromJSON!(Person)(json);
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
writeln(person);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(cmp(person.firstname, "Tristan") == 0);
|
|
||||||
assert(cmp(person.lastname, "Kildaire") == 0);
|
|
||||||
assert(person.age == 23);
|
|
||||||
assert(person.isMale == true);
|
|
||||||
assert(person.obj["bruh"].integer() == 1);
|
|
||||||
//TODO: list test case
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
|
@ -0,0 +1,59 @@
|
||||||
|
module libpb.exceptions;
|
||||||
|
|
||||||
|
public abstract class PBException : Exception
|
||||||
|
{
|
||||||
|
this(string message = "")
|
||||||
|
{
|
||||||
|
super("PBException: "~message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class RecordNotFoundException : PBException
|
||||||
|
{
|
||||||
|
public const string offendingTable;
|
||||||
|
public const string offendingId;
|
||||||
|
this(string table, string id)
|
||||||
|
{
|
||||||
|
this.offendingTable = table;
|
||||||
|
this.offendingId = id;
|
||||||
|
|
||||||
|
super("Could not find record '"~id~"' in table '"~offendingTable~"'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class NotAuthorized : PBException
|
||||||
|
{
|
||||||
|
public const string offendingTable;
|
||||||
|
public const string offendingId;
|
||||||
|
this(string table, string id)
|
||||||
|
{
|
||||||
|
this.offendingTable = table;
|
||||||
|
this.offendingId = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class ValidationRequired : PBException
|
||||||
|
{
|
||||||
|
public const string offendingTable;
|
||||||
|
public const string offendingId;
|
||||||
|
this(string table, string id)
|
||||||
|
{
|
||||||
|
this.offendingTable = table;
|
||||||
|
this.offendingId = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public final class NetworkException : PBException
|
||||||
|
{
|
||||||
|
this()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class PocketBaseParsingException : PBException
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
module libpb;
|
||||||
|
|
||||||
|
public import libpb.exceptions;
|
||||||
|
public import libpb.driver;
|
|
@ -0,0 +1,116 @@
|
||||||
|
module libpb.serialization;
|
||||||
|
|
||||||
|
import std.json;
|
||||||
|
import std.conv : to;
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
import std.stdio : writeln;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONValue serializeRecord(RecordType)(RecordType record)
|
||||||
|
{
|
||||||
|
import std.traits;
|
||||||
|
import std.meta : AliasSeq;
|
||||||
|
|
||||||
|
// Final JSON to submit
|
||||||
|
JSONValue builtJSON;
|
||||||
|
|
||||||
|
|
||||||
|
// Alias as to only expand later when used in compile-time
|
||||||
|
alias structTypes = FieldTypeTuple!(RecordType);
|
||||||
|
alias structNames = FieldNameTuple!(RecordType);
|
||||||
|
alias structValues = record.tupleof;
|
||||||
|
|
||||||
|
static foreach(cnt; 0..structTypes.length)
|
||||||
|
{
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg, structTypes[cnt]);
|
||||||
|
pragma(msg, structNames[cnt]);
|
||||||
|
// pragma(msg, structValues[cnt]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
||||||
|
{
|
||||||
|
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg, "Yaa");
|
||||||
|
}
|
||||||
|
builtJSON[structNames[cnt]] = to!(string)(structValues[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return builtJSON;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test serialization of a struct to JSON
|
||||||
|
public enum EnumType
|
||||||
|
{
|
||||||
|
DOG,
|
||||||
|
CAT
|
||||||
|
}
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.algorithm.searching : canFind;
|
||||||
|
import std.string : cmp;
|
||||||
|
|
||||||
|
struct Person
|
||||||
|
{
|
||||||
|
public string firstname, lastname;
|
||||||
|
public int age;
|
||||||
|
public string[] list;
|
||||||
|
public JSONValue extraJSON;
|
||||||
|
public EnumType eType;
|
||||||
|
}
|
||||||
|
|
||||||
|
Person p1;
|
||||||
|
p1.firstname = "Tristan";
|
||||||
|
p1.lastname = "Kildaire";
|
||||||
|
p1.age = 23;
|
||||||
|
p1.list = ["1", "2", "3"];
|
||||||
|
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
|
||||||
|
p1.eType = EnumType.CAT;
|
||||||
|
|
||||||
|
JSONValue serialized = serializeRecord(p1);
|
||||||
|
|
||||||
|
string[] keys = serialized.object().keys();
|
||||||
|
assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0);
|
||||||
|
assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0);
|
||||||
|
assert(canFind(keys, "age") && serialized["age"].integer() == 23);
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
writeln(serialized.toPrettyString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue