| 1 | import x.json2 as json |
| 2 | |
| 3 | struct AnyStruct[T] { |
| 4 | val T |
| 5 | } |
| 6 | |
| 7 | struct OptAnyStruct[T] { |
| 8 | val ?T |
| 9 | } |
| 10 | |
| 11 | /*struct OptAnyArrStruct { |
| 12 | val []?json.Any |
| 13 | }*/ |
| 14 | |
| 15 | fn test_values() { |
| 16 | assert json.decode[AnyStruct[json.Any]]('{"val":5}')!.val.int() == 5 |
| 17 | assert json.decode[OptAnyStruct[json.Any]]('{}')!.val == none |
| 18 | assert json.decode[AnyStruct[[]json.Any]]('{"val":[5,10]}')!.val.map(it.int()) == [ |
| 19 | 5, |
| 20 | 10, |
| 21 | ] |
| 22 | // assert json.decode[OptAnyArrStruct]('{"val":[5,null,10]}')!.val == [?json.Any(5),json.Null{},10] skipped because test still fails even though they're the same |
| 23 | |
| 24 | assert json.encode[AnyStruct[json.Any]](AnyStruct[json.Any]{json.Any(5)}) == '{"val":5}' |
| 25 | assert json.encode[OptAnyStruct[json.Any]](OptAnyStruct[json.Any]{none}) == '{}' |
| 26 | assert json.encode[AnyStruct[[]json.Any]](AnyStruct[[]json.Any]{[json.Any(5), 10]}) == '{"val":[5,10]}' |
| 27 | // assert json.encode[OptAnyArrStruct](OptAnyArrStruct{[?json.Any(5),none,10]}) == '{"val":[5,null,10]}' encode_array has not implemented optional arrays yet |
| 28 | } |
| 29 | |