Skip to content

Commit

Permalink
Merge pull request #10 from libmir/devel
Browse files Browse the repository at this point in the history
  • Loading branch information
9il authored Nov 24, 2020
2 parents c7bea0c + e664c84 commit 79eb622
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 740 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dub.selections.json

libasdf.a
builddir

example/filter_and_arrays/filter_and_arrays

examples/filter_and_arrays/filter_and_arrays
Expand Down
274 changes: 131 additions & 143 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ import asdf;
struct Simple
{
string name;
ulong level;
string name;
ulong level;
}
void main()
{
auto o = Simple("asdf", 42);
string data = `{"name":"asdf","level":42}`;
assert(o.serializeToJson() == data);
assert(data.deserialize!Simple == o);
auto o = Simple("asdf", 42);
string data = `{"name":"asdf","level":42}`;
assert(o.serializeToJson() == data);
assert(data.deserialize!Simple == o);
}
```
#### Documentation
Expand Down Expand Up @@ -77,12 +77,12 @@ Now you need to edit the `dub.json` add `asdf` as dependency and set its targetT
(dub.json)
```json
{
...
"dependencies": {
"asdf": "~><current-version>"
},
"targetType": "executable",
"dflags-ldc": ["-mcpu=native"]
...
"dependencies": {
"asdf": "~><current-version>"
},
"targetType": "executable",
"dflags-ldc": ["-mcpu=native"]
}
```

Expand Down Expand Up @@ -116,7 +116,6 @@ For example, `-mattr=+sse4.2`. ASDF has specialized code for
| `@serdeKeys("bar_common", "bar")` | tries to read the data from either property. saves it to the first one |
| `@serdeKeysIn("a", "b")` | tries to read the data from `a`, then `b`. last one occuring in the json wins |
| `@serdeKeyOut("a")` | writes it to `a` |
| `@serializationMultiKeysIn(["a", "b", "c"])` | tries to get the data from a sub object. this has not optimal performance yet if you are using more than 1 serializationMultiKeysIn in an object |
| `@serdeIgnore` | ignore this property completely |
| `@serdeIgnoreIn` | don't read this property |
| `@serdeIgnoreOut` | don't write this property |
Expand All @@ -125,8 +124,8 @@ For example, `-mattr=+sse4.2`. ASDF has specialized code for
| `@serdeProxy!string` | call to!string |
| `@serdeTransformIn!fin` | call function `fin` to transform the data |
| `@serdeTransformOut!fout` | run function `fout` on serialization, different notation |
| `@serdeFlexible` | be flexible on the datatype on reading, e.g. read longs that are wrapped as strings |
| `@serdeRequired` | Force deserialiser to throw AsdfException if field was not found in the input. |
| `@serdeAllowMultiple` | Allows deserialiser to serialize multiple keys for the same object member input. |
| `@serdeOptional` | Allows deserialiser to to skip member desrization of no keys corresponding keys input. |


Please also look into the Docs or Unittest for concrete examples!
Expand All @@ -140,23 +139,23 @@ import asdf;
void main()
{
auto target = Asdf("red");
File("input.jsonl")
// Use at least 4096 bytes for real world apps
.byChunk(4096)
// 32 is minimum size for internal buffer. Buffer can be reallocated to get more memory.
.parseJsonByLine(4096)
.filter!(object => object
// opIndex accepts array of keys: {"key0": {"key1": { ... {"keyN-1": <value>}... }}}
["colors"]
// iterates over an array
.byElement
// Comparison with ASDF is little bit faster
// than comparison with a string.
.canFind(target))
//.canFind("red"))
// Formatting uses internal buffer to reduce system delegate and system function calls
.each!writeln;
auto target = Asdf("red");
File("input.jsonl")
// Use at least 4096 bytes for real world apps
.byChunk(4096)
// 32 is minimum size for internal buffer. Buffer can be reallocated to get more memory.
.parseJsonByLine(4096)
.filter!(object => object
// opIndex accepts array of keys: {"key0": {"key1": { ... {"keyN-1": <value>}... }}}
["colors"]
// iterates over an array
.byElement
// Comparison with ASDF is little bit faster
// than comparison with a string.
.canFind(target))
//.canFind("red"))
// Formatting uses internal buffer to reduce system delegate and system function calls
.each!writeln;
}
```

Expand All @@ -169,7 +168,7 @@ null
{"colors": ["red"]}
{"a":"b", "colors": [4, "red", "string"]}
{"colors":["red"],
"comment" : "this is broken (multiline) object"}
"comment" : "this is broken (multiline) object"}
{"colors": "green"}
{"colors": "red"]}}
[]
Expand All @@ -189,77 +188,79 @@ null
```d
struct S
{
string a;
long b;
private int c; // private fields are ignored
package int d; // package fields are ignored
// all other fields in JSON are ignored
string a;
long b;
private int c; // private fields are ignored
package int d; // package fields are ignored
// all other fields in JSON are ignored
}
```

##### Selection
```d
struct S
{
// ignored
@serdeIgnore int temp;
// can be formatted to json
@serdeIgnoreIn int a;
//can be parsed from json
@serdeIgnoreOut int b;
// ignored if negative
@serdeIgnoreOutIf!`a < 0` int c;
// ignored
@serdeIgnore int temp;
// can be formatted to json
@serdeIgnoreIn int a;
//can be parsed from json
@serdeIgnoreOut int b;
// ignored if negative
@serdeIgnoreOutIf!`a < 0` int c;
}
```

##### Key overriding
```d
struct S
{
// key is overrided to "aaa"
@serdeKeys("aaa") int a;
// overloads multiple keys for parsing
@serdeKeysIn("b", "_b")
// overloads key for generation
@serdeKeyOut("_b_")
int b;
// key is overrided to "aaa"
@serdeKeys("aaa") int a;
// overloads multiple keys for parsing
@serdeKeysIn("b", "_b")
// overloads key for generation
@serdeKeyOut("_b_")
int b;
}
```

##### User-Defined Serialization
```d
struct DateTimeProxy
{
DateTime datetime;
alias datetime this;
static DateTimeProxy deserialize(Asdf data)
{
string val;
deserializeScopedString(data, val);
return DateTimeProxy(DateTime.fromISOString(val));
}
void serialize(S)(ref S serializer)
{
serializer.putValue(datetime.toISOString);
}
DateTime datetime;
alias datetime this;
SerdeException deserializeFromAsdf(Asdf data)
{
string val;
if (auto exc = deserializeScopedString(data, val))
return exc;
this = DateTimeProxy(DateTime.fromISOString(val));
return null;
}
void serialize(S)(ref S serializer)
{
serializer.putValue(datetime.toISOString);
}
}
```

```d
//serialize a Doubly Linked list into an Array
struct SomeDoublyLinkedList
{
@serdeIgnore DList!(SomeArr[]) myDll;
alias myDll this;
@serdeIgnore DList!(SomeArr[]) myDll;
alias myDll this;
//no template but a function this time!
void serialize(ref AsdfSerializer serializer)
//no template but a function this time!
void serialize(ref AsdfSerializer serializer)
{
auto state = serializer.arrayBegin();
foreach (ref elem; myDll)
Expand All @@ -276,66 +277,66 @@ struct SomeDoublyLinkedList
```d
struct S
{
@serdeProxy!DateTimeProxy DateTime time;
@serdeProxy!DateTimeProxy DateTime time;
}
```

```d
@serdeProxy!ProxyE
enum E
{
none,
bar,
none,
bar,
}
// const(char)[] doesn't reallocate ASDF data.
@serdeProxy!(const(char)[])
struct ProxyE
{
E e;
this(E e)
{
this.e = e;
}
this(in char[] str)
{
switch(str)
{
case "NONE":
case "NA":
case "N/A":
e = E.none;
break;
case "BAR":
case "BR":
e = E.bar;
break;
default:
throw new Exception("Unknown: " ~ cast(string)str);
}
}
string toString()
{
if (e == E.none)
return "NONE";
else
return "BAR";
}
E opCast(T : E)()
{
return e;
}
E e;
this(E e)
{
this.e = e;
}
this(in char[] str)
{
switch(str)
{
case "NONE":
case "NA":
case "N/A":
e = E.none;
break;
case "BAR":
case "BR":
e = E.bar;
break;
default:
throw new Exception("Unknown: " ~ cast(string)str);
}
}
string toString()
{
if (e == E.none)
return "NONE";
else
return "BAR";
}
E opCast(T : E)()
{
return e;
}
}
unittest
{
assert(serializeToJson(E.bar) == `"BAR"`);
assert(`"N/A"`.deserialize!E == E.none);
assert(`"NA"`.deserialize!E == E.none);
assert(serializeToJson(E.bar) == `"BAR"`);
assert(`"N/A"`.deserialize!E == E.none);
assert(`"NA"`.deserialize!E == E.none);
}
```

Expand All @@ -346,31 +347,18 @@ If you need to do additional calculations or etl transformations that happen to
```d
struct S
{
string a;
int b;
@serdeIgnoreIn double sum;
void finalizeDeserialization(Asdf data)
{
auto r = data["c", "d"];
auto a = r["e"].get(0.0);
auto b = r["g"].get(0.0);
sum = a + b;
}
string a;
int b;
@serdeIgnoreIn double sum;
void finalizeDeserialization(Asdf data)
{
auto r = data["c", "d"];
auto a = r["e"].get(0.0);
auto b = r["g"].get(0.0);
sum = a + b;
}
}
assert(`{"a":"bar","b":3,"c":{"d":{"e":6,"g":7}}}`.deserialize!S == S("bar", 3, 13));
```

##### serdeFlexible
```D
static struct S
{
@serdeFlexible uint a;
}
assert(`{"a":"100"}`.deserialize!S.a == 100);
assert(`{"a":true}`.deserialize!S.a == 1);
assert(`{"a":null}`.deserialize!S.a == 0);
```

Loading

0 comments on commit 79eb622

Please sign in to comment.