v / vlib / json / tests / json_decode_test.v
165 lines · 146 sloc · 3.61 KB · 6f6c7ed153432cc8813a13fbd9869aa9d889f7ec
Raw
1import json
2
3struct TestTwin {
4 id int
5 seed string
6 pubkey string
7}
8
9struct TestTwins {
10mut:
11 twins []TestTwin @[required]
12}
13
14fn 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
23fn 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
37struct Mount {
38 size u64
39}
40
41fn test_decode_u64() {
42 data := '{"size": 10737418240}'
43 m := json.decode(Mount, data)!
44 assert m.size == 10737418240
45 // println(m)
46}
47
48fn test_decode_large_u64_from_decimal_json() {
49 cases := [
50 u64(9007199254740991),
51 u64(9007199254740992),
52 u64(9007199254740993),
53 u64(9223372036854775807),
54 u64(9223372036854775808),
55 u64(9223372036854775809),
56 u64(18446744073709551614),
57 u64(18446744073709551615),
58 ]
59 for want in cases {
60 got := json.decode([]u64, '[${want}]')!
61 assert got.len == 1
62 assert got[0] == want
63 }
64}
65
66fn test_encode_decode_large_u64_roundtrip() {
67 cases := [
68 u64(9007199254740991),
69 u64(9007199254740992),
70 u64(9007199254740993),
71 u64(9223372036854775807),
72 u64(9223372036854775808),
73 u64(9223372036854775809),
74 u64(18446744073709551614),
75 u64(18446744073709551615),
76 ]
77 for want in cases {
78 encoded := json.encode([want])
79 assert encoded == '[${want}]'
80 got := json.decode([]u64, encoded)!
81 assert got.len == 1
82 assert got[0] == want
83 }
84}
85
86//
87
88pub struct Comment {
89pub mut:
90 id string
91 comment string
92}
93
94pub struct Task {
95mut:
96 description string
97 id int
98 total_comments int
99 file_name string @[skip]
100 comments []Comment @[skip]
101 skip_field string @[json: '-']
102}
103
104fn test_skip_fields_should_be_initialised_by_json_decode() {
105 data := '{"total_comments": 55, "id": 123}'
106 mut task := json.decode(Task, data)!
107 assert task.id == 123
108 assert task.total_comments == 55
109 assert task.comments == []
110}
111
112fn test_skip_should_be_ignored() {
113 data := '{"total_comments": 55, "id": 123, "skip_field": "foo"}'
114 mut task := json.decode(Task, data)!
115 assert task.id == 123
116 assert task.total_comments == 55
117 assert task.comments == []
118 assert task.skip_field == ''
119}
120
121//
122
123struct DbConfig {
124 host string
125 dbname string
126 user string
127}
128
129fn test_decode_error_message_should_have_enough_context_empty() {
130 json.decode(DbConfig, '') or {
131 assert err.msg() == 'failed to decode JSON string'
132 return
133 }
134 assert false
135}
136
137fn test_decode_error_message_should_have_enough_context_just_brace() {
138 json.decode(DbConfig, '{') or {
139 assert err.msg() == 'failed to decode JSON string: {'
140 return
141 }
142 assert false
143}
144
145fn test_decode_error_message_should_have_enough_context_trailing_comma_at_end() {
146 txt := '{
147 "host": "localhost",
148 "dbname": "alex",
149 "user": "alex",
150}'
151 json.decode(DbConfig, txt) or {
152 assert err.msg().contains(' "user": "alex",\n}')
153 return
154 }
155 assert false
156}
157
158fn test_decode_error_message_should_have_enough_context_in_the_middle() {
159 txt := '{"host": "localhost", "dbname": "alex" "user": "alex", "port": "1234"}'
160 json.decode(DbConfig, txt) or {
161 assert err.msg().contains('ost", "dbname": "alex" "user":')
162 return
163 }
164 assert false
165}
166