From 73caebc5010cbfd764b0ec51c85b77dc89143d96 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Wed, 28 Jun 2023 16:50:38 +0200 Subject: [PATCH] Deserializer - Ensure animal is right --- source/jstruct/deserializer.d | 20 +++++++++++++++----- source/jstruct/serializer.d | 12 +++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/source/jstruct/deserializer.d b/source/jstruct/deserializer.d index f40be56..f045347 100644 --- a/source/jstruct/deserializer.d +++ b/source/jstruct/deserializer.d @@ -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(`{ diff --git a/source/jstruct/serializer.d b/source/jstruct/serializer.d index 2f94c17..d0982cd 100644 --- a/source/jstruct/serializer.d +++ b/source/jstruct/serializer.d @@ -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;