mirror of https://github.com/Hax-io/jstruct
Compare commits
4 Commits
b66c7741af
...
1480a5afca
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 1480a5afca | |
Tristan B. Velloza Kildaire | 73caebc501 | |
Tristan B. Velloza Kildaire | f7d9aee319 | |
Tristan B. Velloza Kildaire | c0bd2d624e |
31
README.md
31
README.md
|
@ -22,6 +22,17 @@ struct Person
|
||||||
public int age;
|
public int age;
|
||||||
public string[] list;
|
public string[] list;
|
||||||
public JSONValue extraJSON;
|
public JSONValue extraJSON;
|
||||||
|
public EnumType eType;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Our enum is defined as:
|
||||||
|
|
||||||
|
```d
|
||||||
|
enum EnumType
|
||||||
|
{
|
||||||
|
DOG,
|
||||||
|
CAT
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -34,6 +45,7 @@ p1.lastname = "Kildaire";
|
||||||
p1.age = 23;
|
p1.age = 23;
|
||||||
p1.list = ["1", "2", "3"];
|
p1.list = ["1", "2", "3"];
|
||||||
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
|
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
|
||||||
|
p1.eType = EnumType.CAT;
|
||||||
```
|
```
|
||||||
|
|
||||||
Now, we make a call to `serializeRecord` as follows:
|
Now, we make a call to `serializeRecord` as follows:
|
||||||
|
@ -47,6 +59,7 @@ This returns the following JSON:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"age": 23,
|
"age": 23,
|
||||||
|
"eType": "CAT",
|
||||||
"extraJSON": {
|
"extraJSON": {
|
||||||
"item": 1,
|
"item": 1,
|
||||||
"items": [
|
"items": [
|
||||||
|
@ -57,7 +70,11 @@ This returns the following JSON:
|
||||||
},
|
},
|
||||||
"firstname": "Tristan",
|
"firstname": "Tristan",
|
||||||
"lastname": "Kildaire",
|
"lastname": "Kildaire",
|
||||||
"list": ["1", "2", "3"]
|
"list": [
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -76,6 +93,8 @@ struct Person
|
||||||
public bool[] list2;
|
public bool[] list2;
|
||||||
public float[] list3;
|
public float[] list3;
|
||||||
public double[] list4;
|
public double[] list4;
|
||||||
|
public string[] list5;
|
||||||
|
public EnumType animal;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -91,7 +110,9 @@ Now, let's say we were given the following JSON:
|
||||||
"list": [1,2,3],
|
"list": [1,2,3],
|
||||||
"list2": [true, false],
|
"list2": [true, false],
|
||||||
"list3": [1.5, 1.4],
|
"list3": [1.5, 1.4],
|
||||||
"list4": [1.5, 1.4]
|
"list4": [1.5, 1.4],
|
||||||
|
"list5": ["baba", "booey"],
|
||||||
|
"animal": "CAT"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -108,7 +129,9 @@ JSONValue json = parseJSON(`{
|
||||||
"list": [1,2,3],
|
"list": [1,2,3],
|
||||||
"list2": [true, false],
|
"list2": [true, false],
|
||||||
"list3": [1.5, 1.4],
|
"list3": [1.5, 1.4],
|
||||||
"list4": [1.5, 1.4]
|
"list4": [1.5, 1.4],
|
||||||
|
"list5": ["baba", "booey"],
|
||||||
|
"animal": "CAT"
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
@ -125,7 +148,7 @@ writeln(person):
|
||||||
Which will output:
|
Which will output:
|
||||||
|
|
||||||
```
|
```
|
||||||
Person("Tristan", "Kildaire", 23, true, {"bruh":1}, [1, 2, 3], [true, false], [1.5, 1.4], [1.5, 1.4])
|
Person("Tristan", "Kildaire", 23, true, {"bruh":1}, [1, 2, 3], [true, false], [1.5, 1.4], [1.5, 1.4], ["baba", "booey"], CAT)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installing
|
## Installing
|
||||||
|
|
|
@ -230,8 +230,11 @@ public RecordType fromJSON(RecordType)(JSONValue jsonIn)
|
||||||
*/
|
*/
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import std.string : cmp;
|
enum EnumType
|
||||||
import std.stdio : writeln;
|
{
|
||||||
|
DOG,
|
||||||
|
CAT
|
||||||
|
}
|
||||||
|
|
||||||
struct Person
|
struct Person
|
||||||
{
|
{
|
||||||
|
@ -244,6 +247,7 @@ unittest
|
||||||
public float[] list3;
|
public float[] list3;
|
||||||
public double[] list4;
|
public double[] list4;
|
||||||
public string[] list5;
|
public string[] list5;
|
||||||
|
public EnumType animal;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONValue json = parseJSON(`{
|
JSONValue json = parseJSON(`{
|
||||||
|
@ -256,7 +260,8 @@ unittest
|
||||||
"list2": [true, false],
|
"list2": [true, false],
|
||||||
"list3": [1.5, 1.4],
|
"list3": [1.5, 1.4],
|
||||||
"list4": [1.5, 1.4],
|
"list4": [1.5, 1.4],
|
||||||
"list5": ["baba", "booey"]
|
"list5": ["baba", "booey"],
|
||||||
|
"animal": "CAT"
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
@ -264,7 +269,7 @@ unittest
|
||||||
|
|
||||||
debug(dbg)
|
debug(dbg)
|
||||||
{
|
{
|
||||||
writeln(person);
|
writeln("Deserialized as: ", person);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(cmp(person.firstname, "Tristan") == 0);
|
assert(cmp(person.firstname, "Tristan") == 0);
|
||||||
|
@ -277,18 +282,25 @@ unittest
|
||||||
assert(person.list3 == [1.5F, 1.4F]);
|
assert(person.list3 == [1.5F, 1.4F]);
|
||||||
assert(person.list4 == [1.5, 1.4]);
|
assert(person.list4 == [1.5, 1.4]);
|
||||||
assert(person.list5 == ["baba", "booey"]);
|
assert(person.list5 == ["baba", "booey"]);
|
||||||
|
assert(person.animal == EnumType.CAT);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
version(unittest)
|
||||||
|
{
|
||||||
|
import std.string : cmp;
|
||||||
|
import std.stdio : writeln;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Another example deserialization of JSON
|
* 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
|
unittest
|
||||||
{
|
{
|
||||||
import std.string : cmp;
|
|
||||||
import std.stdio : writeln;
|
|
||||||
|
|
||||||
struct Person
|
struct Person
|
||||||
{
|
{
|
||||||
public string firstname, lastname;
|
public string firstname, lastname;
|
||||||
|
@ -296,6 +308,7 @@ unittest
|
||||||
public bool isMale;
|
public bool isMale;
|
||||||
public JSONValue obj;
|
public JSONValue obj;
|
||||||
public int[] list;
|
public int[] list;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONValue json = parseJSON(`{
|
JSONValue json = parseJSON(`{
|
||||||
|
@ -347,7 +360,6 @@ unittest
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Person person = fromJSON!(Person)(json);
|
Person person = fromJSON!(Person)(json);
|
||||||
import std.stdio : writeln;
|
|
||||||
writeln(person);
|
writeln(person);
|
||||||
assert(true);
|
assert(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,10 +81,13 @@ public JSONValue serializeRecord(RecordType)(RecordType record)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test serialization of a struct to JSON
|
// Test serialization of a struct to JSON
|
||||||
private enum EnumType
|
|
||||||
|
|
||||||
|
version(unittest)
|
||||||
{
|
{
|
||||||
DOG,
|
import std.algorithm.searching : canFind;
|
||||||
CAT
|
import std.string : cmp;
|
||||||
|
import std.stdio : writeln;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,10 +96,12 @@ private enum EnumType
|
||||||
*/
|
*/
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import std.algorithm.searching : canFind;
|
enum EnumType
|
||||||
import std.string : cmp;
|
{
|
||||||
import std.stdio : writeln;
|
DOG,
|
||||||
|
CAT
|
||||||
|
}
|
||||||
|
|
||||||
struct Person
|
struct Person
|
||||||
{
|
{
|
||||||
public string firstname, lastname;
|
public string firstname, lastname;
|
||||||
|
|
Loading…
Reference in New Issue