v2 / vlib / x / json2 / tests / json2_tests / decode_map_test.v
58 lines · 52 sloc · 1.11 KB · 2d33a7f2819dd5fc1f4aa3b3ca0bcc660810d7af
Raw
1import x.json2 as json
2
3const data = '
4{
5 "comments": {
6 "26788945": {
7 "id": "26788945",
8 "message": "some comment 1"
9 },
10 "26788946": {
11 "id": "26788946",
12 "message": "some comment 2"
13 },
14 "26788947": {
15 "id": "26788947",
16 "message": "some comment 3"
17 }
18 },
19 "comments2": {
20 "26788945": true,
21 "26788946": false,
22 "26788947": true
23 },
24 "comments3": {
25 "26788945": 1,
26 "26788946": 2,
27 "26788947": 3
28 }
29}
30'
31
32pub struct Comment {
33 id string
34 message string
35}
36
37struct Comments {
38mut:
39 comments map[string]Comment
40 comments2 map[string]bool
41 comments3 map[string]int
42}
43
44fn test_main() {
45 mut root := json.decode[Comments](data)!
46 assert root.comments.len == 3
47 assert root.comments['26788945']!.id == '26788945'
48 assert root.comments['26788946']!.id == '26788946'
49 assert root.comments['26788947']!.id == '26788947'
50
51 assert root.comments2['26788945']! == true
52 assert root.comments2['26788946']! == false
53 assert root.comments2['26788947']! == true
54
55 assert root.comments3['26788945']! == 1
56 assert root.comments3['26788946']! == 2
57 assert root.comments3['26788947']! == 3
58}
59