1 package extra
2
3 import (
4 "testing"
5
6 "github.com/json-iterator/go"
7 "github.com/stretchr/testify/require"
8 )
9
10 func init() {
11 RegisterFuzzyDecoders()
12 }
13
14 func Test_any_to_string(t *testing.T) {
15 should := require.New(t)
16 var val string
17 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
18 should.Equal("100", val)
19 should.Nil(jsoniter.UnmarshalFromString("10", &val))
20 should.Equal("10", val)
21 should.Nil(jsoniter.UnmarshalFromString("10.1", &val))
22 should.Equal("10.1", val)
23 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
24 should.Equal("10.1", val)
25 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
26 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
27 }
28 func Test_any_to_int64(t *testing.T) {
29 should := require.New(t)
30 var val int64
31
32 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
33 should.Equal(int64(100), val)
34 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
35 should.Equal(int64(10), val)
36 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
37 should.Equal(int64(10), val)
38 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
39 should.Equal(int64(10), val)
40 should.Nil(jsoniter.UnmarshalFromString(`""`, &val))
41 should.Equal(int64(0), val)
42
43
44 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
45 should.Equal(int64(0), val)
46 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
47 should.Equal(int64(1), val)
48
49 should.Nil(jsoniter.UnmarshalFromString(`-10`, &val))
50 should.Equal(int64(-10), val)
51 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
52 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
53
54 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
55 }
56
57 func Test_any_to_int(t *testing.T) {
58 should := require.New(t)
59 var val int
60 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
61 should.Equal(100, val)
62 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
63 should.Equal(10, val)
64 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
65 should.Equal(10, val)
66 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
67 should.Equal(10, val)
68
69
70 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
71 should.Equal(0, val)
72 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
73 should.Equal(1, val)
74
75 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
76 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
77
78 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
79 }
80
81 func Test_any_to_int16(t *testing.T) {
82 should := require.New(t)
83 var val int16
84 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
85 should.Equal(int16(100), val)
86 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
87 should.Equal(int16(10), val)
88 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
89 should.Equal(int16(10), val)
90 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
91 should.Equal(int16(10), val)
92
93
94 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
95 should.Equal(int16(0), val)
96 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
97 should.Equal(int16(1), val)
98
99 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
100 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
101
102 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
103 }
104
105 func Test_any_to_int32(t *testing.T) {
106 should := require.New(t)
107 var val int32
108 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
109 should.Equal(int32(100), val)
110 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
111 should.Equal(int32(10), val)
112 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
113 should.Equal(int32(10), val)
114 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
115 should.Equal(int32(10), val)
116
117
118 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
119 should.Equal(int32(0), val)
120 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
121 should.Equal(int32(1), val)
122
123 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
124 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
125
126 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
127 }
128
129 func Test_any_to_int8(t *testing.T) {
130 should := require.New(t)
131 var val int8
132 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
133 should.Equal(int8(100), val)
134 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
135 should.Equal(int8(10), val)
136 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
137 should.Equal(int8(10), val)
138 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
139 should.Equal(int8(10), val)
140
141
142 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
143 should.Equal(int8(0), val)
144 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
145 should.Equal(int8(1), val)
146
147 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
148 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
149
150 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
151 }
152
153 func Test_any_to_uint8(t *testing.T) {
154 should := require.New(t)
155 var val uint8
156 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
157 should.Equal(uint8(100), val)
158 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
159 should.Equal(uint8(10), val)
160 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
161 should.Equal(uint8(10), val)
162 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
163 should.Equal(uint8(10), val)
164
165
166 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
167 should.Equal(uint8(0), val)
168 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
169 should.Equal(uint8(1), val)
170
171 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
172 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
173
174 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
175 }
176
177 func Test_any_to_uint64(t *testing.T) {
178 should := require.New(t)
179 var val uint64
180
181 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
182 should.Equal(uint64(100), val)
183 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
184 should.Equal(uint64(10), val)
185 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
186 should.Equal(uint64(10), val)
187 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
188 should.Equal(uint64(10), val)
189
190
191 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
192 should.Equal(uint64(0), val)
193 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
194 should.Equal(uint64(1), val)
195
196
197 should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
198 should.Equal(uint64(0), val)
199 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
200 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
201
202 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
203 }
204 func Test_any_to_uint32(t *testing.T) {
205 should := require.New(t)
206 var val uint32
207
208 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
209 should.Equal(uint32(100), val)
210 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
211 should.Equal(uint32(10), val)
212 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
213 should.Equal(uint32(10), val)
214 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
215 should.Equal(uint32(10), val)
216
217
218 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
219 should.Equal(uint32(0), val)
220 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
221 should.Equal(uint32(1), val)
222
223
224 should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
225 should.Equal(uint32(0), val)
226 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
227 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
228
229 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
230 }
231 func Test_any_to_uint16(t *testing.T) {
232 should := require.New(t)
233 var val uint16
234
235 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
236 should.Equal(uint16(100), val)
237 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
238 should.Equal(uint16(10), val)
239 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
240 should.Equal(uint16(10), val)
241 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
242 should.Equal(uint16(10), val)
243
244
245 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
246 should.Equal(uint16(0), val)
247 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
248 should.Equal(uint16(1), val)
249
250
251 should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
252 should.Equal(uint16(0), val)
253 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
254 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
255
256 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
257 }
258 func Test_any_to_uint(t *testing.T) {
259 should := require.New(t)
260 var val uint
261 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
262 should.Equal(uint(100), val)
263 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
264 should.Equal(uint(10), val)
265 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
266 should.Equal(uint(10), val)
267 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
268 should.Equal(uint(10), val)
269
270 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
271 should.Equal(uint(0), val)
272 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
273 should.Equal(uint(1), val)
274
275 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
276 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
277
278 should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
279 }
280
281 func Test_any_to_float32(t *testing.T) {
282 should := require.New(t)
283 var val float32
284 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
285 should.Equal(float32(100), val)
286
287 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
288 should.Equal(float32(10.1), val)
289 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
290 should.Equal(float32(10.1), val)
291 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
292 should.Equal(float32(10), val)
293
294
295 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
296 should.Equal(float32(0), val)
297 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
298 should.Equal(float32(1), val)
299
300 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
301 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
302 }
303
304 func Test_any_to_float64(t *testing.T) {
305 should := require.New(t)
306 var val float64
307
308 should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
309 should.Equal(float64(100), val)
310
311 should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
312 should.Equal(float64(10.1), val)
313 should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
314 should.Equal(float64(10.1), val)
315 should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
316 should.Equal(float64(10), val)
317
318
319 should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
320 should.Equal(float64(0), val)
321 should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
322 should.Equal(float64(1), val)
323
324 should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
325 should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
326 }
327
328 func Test_empty_array_as_map(t *testing.T) {
329 should := require.New(t)
330 var val map[string]interface{}
331 should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
332 should.Equal(map[string]interface{}{}, val)
333 }
334
335 func Test_empty_array_as_object(t *testing.T) {
336 should := require.New(t)
337 var val struct{}
338 should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
339 should.Equal(struct{}{}, val)
340 }
341
342 func Test_bad_case(t *testing.T) {
343 var jsonstr = `
344 {
345 "extra_type": 181760,
346 "combo_type": 0,
347 "trigger_time_ms": 1498800398000,
348 "_create_time": "2017-06-16 11:21:39",
349 "_msg_type": 41000
350 }
351 `
352
353 type OrderEventRequestParams struct {
354 ExtraType uint64 `json:"extra_type"`
355 }
356
357 var a OrderEventRequestParams
358 err := jsoniter.UnmarshalFromString(jsonstr, &a)
359 should := require.New(t)
360 should.Nil(err)
361 }
362
363 func Test_null_to_string(t *testing.T) {
364 should := require.New(t)
365 body := []byte(`null`)
366 var message string
367 err := jsoniter.Unmarshal(body, &message)
368 should.NoError(err)
369 }
370
371 func Test_null_to_int(t *testing.T) {
372 should := require.New(t)
373 body := []byte(`null`)
374 var message int
375 err := jsoniter.Unmarshal(body, &message)
376 should.NoError(err)
377 }
378
379 func Test_null_to_float32(t *testing.T) {
380 should := require.New(t)
381 body := []byte(`null`)
382 var message float32
383 err := jsoniter.Unmarshal(body, &message)
384 should.NoError(err)
385 }
386
387 func Test_null_to_float64(t *testing.T) {
388 should := require.New(t)
389 body := []byte(`null`)
390 var message float64
391 err := jsoniter.Unmarshal(body, &message)
392 should.NoError(err)
393 }
394
View as plain text