| 1 | import x.json2 |
| 2 | |
| 3 | const 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 | |
| 32 | pub struct Comment { |
| 33 | id string |
| 34 | message string |
| 35 | } |
| 36 | |
| 37 | struct Comments { |
| 38 | mut: |
| 39 | comments map[string]Comment |
| 40 | comments2 map[string]bool |
| 41 | comments3 map[string]int |
| 42 | } |
| 43 | |
| 44 | fn test_main() { |
| 45 | mut root := json2.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 | |