v2 / vlib / x / json2 / tests / decode_and_encode_struct_any_test.v
28 lines · 23 sloc · 994 bytes · 2d43f38bf389757eb49ae56c298d2c2dbcd4b5cb
Raw
1import x.json2 as json
2
3struct AnyStruct[T] {
4 val T
5}
6
7struct OptAnyStruct[T] {
8 val ?T
9}
10
11/*struct OptAnyArrStruct {
12 val []?json.Any
13}*/
14
15fn 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