v2 / vlib / json / tests / json_option_raw_test.v
62 lines · 49 sloc · 1002 bytes · 8ebbacecd60366ac4ba68aa35f9b0e7a0e56ff61
Raw
1import json
2
3pub struct Dto {
4pub:
5 key string @[raw]
6 key2 string @[raw]
7 data ?string @[raw]
8 optional ?string @[raw]
9}
10
11fn test_main() {
12 raw_json := '{
13 "key": [1, 2, "test"],
14 "key2": { "test": 1 },
15 "data": { "test": 1 },
16 "optional": "test"
17 }'
18
19 dto := json.decode(Dto, raw_json)!
20
21 println(dto)
22 assert dto.data? == '{"test":1}'
23}
24
25fn test_none() {
26 raw_json := '{
27 "key": [1, 2, "test"],
28 "optional": "test"
29 }'
30
31 dto := json.decode(Dto, raw_json)!
32
33 println(dto)
34 assert dto.data == none
35 assert dto.optional? == '"test"'
36}
37
38fn test_null() {
39 raw_json := '{
40 "key": [1, 2, "test"],
41 "key2": null,
42 "data": null,
43 "optional": "test"
44 }'
45
46 dto := json.decode(Dto, raw_json)!
47
48 println(dto)
49 assert dto.key2 == 'null'
50 assert dto.data? == 'null'
51}
52
53fn test_not_set() {
54 raw_json := '{
55 }'
56
57 dto := json.decode(Dto, raw_json)!
58
59 println(dto)
60 assert dto.data == none
61 assert dto.optional == none
62}
63