1 | import json |
2 | |
3 | struct TestTwin { |
4 | id int |
5 | seed string |
6 | pubkey string |
7 | } |
8 | |
9 | struct TestTwins { |
10 | mut: |
11 | twins []TestTwin [required] |
12 | } |
13 | |
14 | fn test_json_decode_fails_to_decode_unrecognised_array_of_dicts() { |
15 | data := '[{"twins":[{"id":123,"seed":"abcde","pubkey":"xyzasd"},{"id":456,"seed":"dfgdfgdfgd","pubkey":"skjldskljh45sdf"}]}]' |
16 | json.decode(TestTwins, data) or { |
17 | assert err.msg() == "expected field 'twins' is missing" |
18 | return |
19 | } |
20 | assert false |
21 | } |
22 | |
23 | fn test_json_decode_works_with_a_dict_of_arrays() { |
24 | data := '{"twins":[{"id":123,"seed":"abcde","pubkey":"xyzasd"},{"id":456,"seed":"dfgdfgdfgd","pubkey":"skjldskljh45sdf"}]}' |
25 | res := json.decode(TestTwins, data) or { |
26 | assert false |
27 | exit(1) |
28 | } |
29 | assert res.twins[0].id == 123 |
30 | assert res.twins[0].seed == 'abcde' |
31 | assert res.twins[0].pubkey == 'xyzasd' |
32 | assert res.twins[1].id == 456 |
33 | assert res.twins[1].seed == 'dfgdfgdfgd' |
34 | assert res.twins[1].pubkey == 'skjldskljh45sdf' |
35 | } |
36 | |
37 | struct Mount { |
38 | size u64 |
39 | } |
40 | |
41 | fn test_decode_u64() { |
42 | data := '{"size": 10737418240}' |
43 | m := json.decode(Mount, data)! |
44 | assert m.size == 10737418240 |
45 | // println(m) |
46 | } |
47 | |
48 | // |
49 | |
50 | pub struct Comment { |
51 | pub mut: |
52 | id string |
53 | comment string |
54 | } |
55 | |
56 | pub struct Task { |
57 | mut: |
58 | description string |
59 | id int |
60 | total_comments int |
61 | file_name string [skip] |
62 | comments []Comment [skip] |
63 | skip_field string [json: '-'] |
64 | } |
65 | |
66 | fn test_skip_fields_should_be_initialised_by_json_decode() { |
67 | data := '{"total_comments": 55, "id": 123}' |
68 | mut task := json.decode(Task, data)! |
69 | assert task.id == 123 |
70 | assert task.total_comments == 55 |
71 | assert task.comments == [] |
72 | } |
73 | |
74 | fn test_skip_should_be_ignored() { |
75 | data := '{"total_comments": 55, "id": 123, "skip_field": "foo"}' |
76 | mut task := json.decode(Task, data)! |
77 | assert task.id == 123 |
78 | assert task.total_comments == 55 |
79 | assert task.comments == [] |
80 | assert task.skip_field == '' |
81 | } |
82 | |
83 | // |
84 | |
85 | struct DbConfig { |
86 | host string |
87 | dbname string |
88 | user string |
89 | } |
90 | |
91 | fn test_decode_error_message_should_have_enough_context_empty() { |
92 | json.decode(DbConfig, '') or { |
93 | assert err.msg().len < 2 |
94 | return |
95 | } |
96 | assert false |
97 | } |
98 | |
99 | fn test_decode_error_message_should_have_enough_context_just_brace() { |
100 | json.decode(DbConfig, '{') or { |
101 | assert err.msg() == '{' |
102 | return |
103 | } |
104 | assert false |
105 | } |
106 | |
107 | fn test_decode_error_message_should_have_enough_context_trailing_comma_at_end() { |
108 | txt := '{ |
109 | "host": "localhost", |
110 | "dbname": "alex", |
111 | "user": "alex", |
112 | }' |
113 | json.decode(DbConfig, txt) or { |
114 | assert err.msg() == ' "user": "alex",\n}' |
115 | return |
116 | } |
117 | assert false |
118 | } |
119 | |
120 | fn test_decode_error_message_should_have_enough_context_in_the_middle() { |
121 | txt := '{"host": "localhost", "dbname": "alex" "user": "alex", "port": "1234"}' |
122 | json.decode(DbConfig, txt) or { |
123 | assert err.msg() == 'ost", "dbname": "alex" "user":' |
124 | return |
125 | } |
126 | assert false |
127 | } |