| 1 | import json |
| 2 | |
| 3 | struct Test { |
| 4 | optional_alias ?MyAlias // primitive |
| 5 | optional_struct ?MyAlias2 // complex |
| 6 | } |
| 7 | |
| 8 | struct Complex { |
| 9 | a int = 3 |
| 10 | } |
| 11 | |
| 12 | type MyAlias = int |
| 13 | type MyAlias2 = Complex |
| 14 | |
| 15 | fn test_empty() { |
| 16 | test := Test{} |
| 17 | encoded := json.encode(test) |
| 18 | assert dump(encoded) == '{}' |
| 19 | assert json.decode(Test, '{}')! == test |
| 20 | } |
| 21 | |
| 22 | fn test_value() { |
| 23 | test := Test{ |
| 24 | optional_alias: 1 |
| 25 | } |
| 26 | encoded := json.encode(test) |
| 27 | assert dump(encoded) == '{"optional_alias":1}' |
| 28 | assert json.decode(Test, '{"optional_alias":1}')! == test |
| 29 | } |
| 30 | |
| 31 | fn test_value_2() { |
| 32 | test := Test{ |
| 33 | optional_alias: 1 |
| 34 | optional_struct: Complex{ |
| 35 | a: 1 |
| 36 | } |
| 37 | } |
| 38 | encoded := json.encode(test) |
| 39 | assert dump(encoded) == '{"optional_alias":1,"optional_struct":{"a":1}}' |
| 40 | assert json.decode(Test, '{"optional_alias":1,"optional_struct":{"a":1}}')! == test |
| 41 | } |
| 42 | |