Deserializer

- Ensure animal is right
This commit is contained in:
Tristan B. Velloza Kildaire 2023-06-28 16:50:38 +02:00
parent f7d9aee319
commit 73caebc501
2 changed files with 22 additions and 10 deletions

View File

@ -230,8 +230,11 @@ public RecordType fromJSON(RecordType)(JSONValue jsonIn)
*/
unittest
{
import std.string : cmp;
import std.stdio : writeln;
enum EnumType
{
DOG,
CAT
}
struct Person
{
@ -244,6 +247,7 @@ unittest
public float[] list3;
public double[] list4;
public string[] list5;
public EnumType animal;
}
JSONValue json = parseJSON(`{
@ -256,7 +260,8 @@ unittest
"list2": [true, false],
"list3": [1.5, 1.4],
"list4": [1.5, 1.4],
"list5": ["baba", "booey"]
"list5": ["baba", "booey"],
"animal": "CAT"
}
`);
@ -264,7 +269,7 @@ unittest
debug(dbg)
{
writeln(person);
writeln("Deserialized as: ", person);
}
assert(cmp(person.firstname, "Tristan") == 0);
@ -277,6 +282,7 @@ unittest
assert(person.list3 == [1.5F, 1.4F]);
assert(person.list4 == [1.5, 1.4]);
assert(person.list5 == ["baba", "booey"]);
assert(person.animal == EnumType.CAT);
}
@ -288,7 +294,10 @@ version(unittest)
/**
* Another example deserialization of JSON
* to our `Person` struct
* to our `Person` struct but here there
* is a problem with deserialization as
* there is a missing field `isMale`
* in the provided JSON
*/
unittest
{
@ -299,6 +308,7 @@ unittest
public bool isMale;
public JSONValue obj;
public int[] list;
}
JSONValue json = parseJSON(`{

View File

@ -81,11 +81,7 @@ public JSONValue serializeRecord(RecordType)(RecordType record)
}
// Test serialization of a struct to JSON
private enum EnumType
{
DOG,
CAT
}
version(unittest)
{
@ -100,6 +96,12 @@ version(unittest)
*/
unittest
{
enum EnumType
{
DOG,
CAT
}
struct Person
{
public string firstname, lastname;