| 1 | import json |
| 2 | |
| 3 | struct Struct { |
| 4 | a int |
| 5 | } |
| 6 | |
| 7 | struct Test { |
| 8 | a ?int |
| 9 | b ?string |
| 10 | c ?Struct |
| 11 | } |
| 12 | |
| 13 | fn test_main() { |
| 14 | a := json.decode(Test, '{"a": 1, "b": "foo"}')! |
| 15 | dump(a) |
| 16 | |
| 17 | assert a.a != none |
| 18 | assert a.b != none |
| 19 | |
| 20 | b := json.decode(Test, '{"a": 1}')! |
| 21 | dump(b) |
| 22 | assert b.a != none |
| 23 | assert b.b == none |
| 24 | |
| 25 | c := json.decode(Test, '{"a": 1, "b": null}')! |
| 26 | dump(b) |
| 27 | assert c.a != none |
| 28 | assert c.b == none |
| 29 | |
| 30 | d := json.decode(Test, '{"a": null, "b": null}')! |
| 31 | dump(d) |
| 32 | assert d.a == none |
| 33 | assert d.b == none |
| 34 | |
| 35 | e := json.decode(Test, '{"a": null, "b": null, "c": null}')! |
| 36 | dump(e) |
| 37 | assert e.a == none |
| 38 | assert e.b == none |
| 39 | assert e.c == none |
| 40 | |
| 41 | f := json.decode(Test, '{"a": null, "b": null, "c": {"a":1}}')! |
| 42 | dump(f) |
| 43 | assert f.a == none |
| 44 | assert f.b == none |
| 45 | assert f.c != none |
| 46 | } |
| 47 | |