Debian Codegraph @

decode_test.go

raw symbols SCIP
1 // Copyright 2016 Richard Gibson. All rights reserved.
2 // Copyright 2010 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 package canonicaljson
7
8 import (
9 "bytes"
10 "encoding"
11 "fmt"
12 "image"
13 "net"
14 "reflect"
15 "strings"
16 "testing"
17 "time"
18 )
19
20 type T struct {
21 X string
22 Y int
23 Z int `json:"-"`
24 }
25
26 type U struct {
27 Alphabet string `json:"alpha"`
28 }
29
30 type V struct {
31 F1 interface{}
32 F2 int32
33 F3 Number
34 }
35
36 // ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and
37 // without UseNumber
38 var ifaceNumAsFloat64 = map[string]interface{}{
39 "k1": float64(1),
40 "k2": "s",
41 "k3": []interface{}{float64(1), float64(2.0), float64(3e-3)},
42 "k4": map[string]interface{}{"kk1": "s", "kk2": float64(2)},
43 }
44
45 var ifaceNumAsNumber = map[string]interface{}{
46 "k1": Number("1"),
47 "k2": "s",
48 "k3": []interface{}{Number("1"), Number("2"), Number("3.0E-3")},
49 "k4": map[string]interface{}{"kk1": "s", "kk2": Number("2")},
50 }
51
52 type tx struct {
53 x int
54 }
55
56 // A type that can unmarshal itself.
57
58 type unmarshaler struct {
59 T bool
60 }
61
62 func (u *unmarshaler) UnmarshalJSON(b []byte) error {
63 *u = unmarshaler{true} // All we need to see that UnmarshalJSON is called.
64 return nil
65 }
66
67 type ustruct struct {
68 M unmarshaler
69 }
70
71 type unmarshalerText struct {
72 T bool
73 }
74
75 // needed for re-marshaling tests
76 func (u *unmarshalerText) MarshalText() ([]byte, error) {
77 return []byte(""), nil
78 }
79
80 func (u *unmarshalerText) UnmarshalText(b []byte) error {
81 *u = unmarshalerText{true} // All we need to see that UnmarshalText is called.
82 return nil
83 }
84
85 var _ encoding.TextUnmarshaler = (*unmarshalerText)(nil)
86
87 type ustructText struct {
88 M unmarshalerText
89 }
90
91 var (
92 um0, um1 unmarshaler // target2 of unmarshaling
93 ump = &um1
94 umtrue = unmarshaler{true}
95 umslice = []unmarshaler{{true}}
96 umslicep = new([]unmarshaler)
97 umstruct = ustruct{unmarshaler{true}}
98
99 um0T, um1T unmarshalerText // target2 of unmarshaling
100 umpT = &um1T
101 umtrueT = unmarshalerText{true}
102 umsliceT = []unmarshalerText{{true}}
103 umslicepT = new([]unmarshalerText)
104 umstructT = ustructText{unmarshalerText{true}}
105 )
106
107 // Test data structures for anonymous fields.
108
109 type Point struct {
110 Z int
111 }
112
113 type Top struct {
114 Level0 int
115 Embed0
116 *Embed0a
117 *Embed0b `json:"e,omitempty"` // treated as named
118 Embed0c `json:"-"` // ignored
119 Loop
120 Embed0p // has Point with X, Y, used
121 Embed0q // has Point with Z, used
122 embed // contains exported field
123 }
124
125 type Embed0 struct {
126 Level1a int // overridden by Embed0a's Level1a with json tag
127 Level1b int // used because Embed0a's Level1b is renamed
128 Level1c int // used because Embed0a's Level1c is ignored
129 Level1d int // annihilated by Embed0a's Level1d
130 Level1e int `json:"x"` // annihilated by Embed0a.Level1e
131 }
132
133 type Embed0a struct {
134 Level1a int `json:"Level1a,omitempty"`
135 Level1b int `json:"LEVEL1B,omitempty"`
136 Level1c int `json:"-"`
137 Level1d int // annihilated by Embed0's Level1d
138 Level1f int `json:"x"` // annihilated by Embed0's Level1e
139 }
140
141 type Embed0b Embed0
142
143 type Embed0c Embed0
144
145 type Embed0p struct {
146 image.Point
147 }
148
149 type Embed0q struct {
150 Point
151 }
152
153 type embed struct {
154 Q int
155 }
156
157 type Loop struct {
158 Loop1 int `json:",omitempty"`
159 Loop2 int `json:",omitempty"`
160 *Loop
161 }
162
163 // From reflect test:
164 // The X in S6 and S7 annihilate, but they also block the X in S8.S9.
165 type S5 struct {
166 S6
167 S7
168 S8
169 }
170
171 type S6 struct {
172 X int
173 }
174
175 type S7 S6
176
177 type S8 struct {
178 S9
179 }
180
181 type S9 struct {
182 X int
183 Y int
184 }
185
186 // From reflect test:
187 // The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9.
188 type S10 struct {
189 S11
190 S12
191 S13
192 }
193
194 type S11 struct {
195 S6
196 }
197
198 type S12 struct {
199 S6
200 }
201
202 type S13 struct {
203 S8
204 }
205
206 type unmarshalTest struct {
207 label string
208 in string
209 ptr interface{}
210 out interface{}
211 err error
212 useNumber bool
213 }
214
215 type Ambig struct {
216 // Given "hello", the first match should win.
217 First int `json:"HELLO"`
218 Second int `json:"Hello"`
219 }
220
221 type XYZ struct {
222 X interface{}
223 Y interface{}
224 Z interface{}
225 }
226
227 func sliceAddr(x []int) *[]int { return &x }
228 func mapAddr(x map[string]int) *map[string]int { return &x }
229
230 // wtf8 returns a UTF-8 like encoding of a code point, even if it is a surrogate.
231 // See http://www.unicode.org/versions/Unicode10.0.0/ch03.pdf#G7404
232 func wtf8(cp rune) string {
233 var result []byte
234 if cp < 0x80 {
235 result = []byte{byte(cp)}
236 } else if cp < 0x800 {
237 result = []byte{
238 0xC0 | byte(cp>>6),
239 0x80 | byte(cp&0x3F),
240 }
241 } else if cp < 0x10000 {
242 result = []byte{
243 0xE0 | byte(cp>>12),
244 0x80 | byte((cp>>6)&0x3F),
245 0x80 | byte(cp&0x3F),
246 }
247 } else {
248 result = []byte{
249 0xF0 | byte(cp>>18),
250 0x80 | byte((cp>>12)&0x3F),
251 0x80 | byte((cp>>6)&0x3F),
252 0x80 | byte(cp&0x3F),
253 }
254 }
255 return string(result)
256 }
257
258 var unmarshalTests = []unmarshalTest{
259 // basic types
260 {in: `true`, ptr: new(bool), out: true},
261 {in: `1`, ptr: new(int), out: 1},
262 {in: `1.2`, ptr: new(float64), out: 1.2},
263 {in: `-5`, ptr: new(int16), out: int16(-5)},
264 {in: `2`, ptr: new(Number), out: Number("2"), useNumber: true},
265 {in: `2`, ptr: new(Number), out: Number("2")},
266 {in: `2`, ptr: new(interface{}), out: float64(2.0)},
267 {in: `2`, ptr: new(interface{}), out: Number("2"), useNumber: true},
268 {in: `"a\u1234"`, ptr: new(string), out: "a\u1234"},
269 {in: `"http:\/\/"`, ptr: new(string), out: "http://"},
270 {in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"},
271 {in: `"lone surrogates: \uD834x\uDD1E"`, ptr: new(string), out: "lone surrogates: " + wtf8(0xD834) + "x" + wtf8(0xDD1E)},
272 {in: "null", ptr: new(interface{}), out: nil},
273 {in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeOf(""), 7}},
274 {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
275 {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}},
276 {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true},
277 {label: "interface as float64", in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64},
278 {label: "interface as Number", in: `{"k1":1,"k2":"s","k3":[1,2,3.0E-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsNumber, useNumber: true},
279
280 // raw values with whitespace
281 {in: "\n true ", ptr: new(bool), out: true},
282 {in: "\t 1 ", ptr: new(int), out: 1},
283 {in: "\r 1.2 ", ptr: new(float64), out: 1.2},
284 {in: "\t -5 \n", ptr: new(int16), out: int16(-5)},
285 {in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"},
286
287 // Z has a "-" tag.
288 {in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}},
289
290 {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}},
291 {in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}},
292 {in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}},
293
294 // syntax errors
295 {in: `{"X": "foo", "Y"}`, err: &SyntaxError{"invalid character '}' after object key", 17}},
296 {in: `[1, 2, 3+]`, err: &SyntaxError{"invalid character '+' after array element", 9}},
297 {in: `{"X":12x}`, err: &SyntaxError{"invalid character 'x' after object key:value pair", 8}, useNumber: true},
298
299 // raw value errors
300 {in: "\x01 42", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}},
301 {in: " 42 \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 5}},
302 {in: "\x01 true", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}},
303 {in: " false \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 8}},
304 {in: "\x01 1.2", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}},
305 {in: " 3.4 \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 6}},
306 {in: "\x01 \"string\"", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}},
307 {in: " \"string\" \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 11}},
308
309 // array tests
310 {in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}},
311 {in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}},
312 {in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}},
313
314 // empty array to interface test
315 {in: `[]`, ptr: new([]interface{}), out: []interface{}{}},
316 {in: `null`, ptr: new([]interface{}), out: []interface{}(nil)},
317 {in: `{"T":[]}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}},
318 {in: `{"T":null}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": interface{}(nil)}},
319
320 // composite tests
321 {in: allValueIndent, ptr: new(All), out: allValue},
322 {in: allValueCompact, ptr: new(All), out: allValue},
323 {in: allValueIndent, ptr: new(*All), out: &allValue},
324 {in: allValueCompact, ptr: new(*All), out: &allValue},
325 {in: pallValueIndent, ptr: new(All), out: pallValue},
326 {in: pallValueCompact, ptr: new(All), out: pallValue},
327 {in: pallValueIndent, ptr: new(*All), out: &pallValue},
328 {in: pallValueCompact, ptr: new(*All), out: &pallValue},
329
330 // unmarshal interface test
331 {in: `{"T":false}`, ptr: &um0, out: umtrue}, // use "false" so test will fail if custom unmarshaler is not called
332 {in: `{"T":false}`, ptr: &ump, out: &umtrue},
333 {in: `[{"T":false}]`, ptr: &umslice, out: umslice},
334 {in: `[{"T":false}]`, ptr: &umslicep, out: &umslice},
335 {in: `{"M":{"T":false}}`, ptr: &umstruct, out: umstruct},
336
337 // UnmarshalText interface test
338 {in: `"X"`, ptr: &um0T, out: umtrueT}, // use "false" so test will fail if custom unmarshaler is not called
339 {in: `"X"`, ptr: &umpT, out: &umtrueT},
340 {in: `["X"]`, ptr: &umsliceT, out: umsliceT},
341 {in: `["X"]`, ptr: &umslicepT, out: &umsliceT},
342 {in: `{"M":"X"}`, ptr: &umstructT, out: umstructT},
343
344 // Overwriting of data.
345 // This is different from package xml, but it's what we've always done.
346 // Now documented and tested.
347 {in: `[2]`, ptr: sliceAddr([]int{1}), out: []int{2}},
348 {in: `{"key": 2}`, ptr: mapAddr(map[string]int{"old": 0, "key": 1}), out: map[string]int{"key": 2}},
349
350 {
351 in: `{
352 "Level0": 1,
353 "Level1b": 2,
354 "Level1c": 3,
355 "x": 4,
356 "Level1a": 5,
357 "LEVEL1B": 6,
358 "e": {
359 "Level1a": 8,
360 "Level1b": 9,
361 "Level1c": 10,
362 "Level1d": 11,
363 "x": 12
364 },
365 "Loop1": 13,
366 "Loop2": 14,
367 "X": 15,
368 "Y": 16,
369 "Z": 17,
370 "Q": 18
371 }`,
372 ptr: new(Top),
373 out: Top{
374 Level0: 1,
375 Embed0: Embed0{
376 Level1b: 2,
377 Level1c: 3,
378 },
379 Embed0a: &Embed0a{
380 Level1a: 5,
381 Level1b: 6,
382 },
383 Embed0b: &Embed0b{
384 Level1a: 8,
385 Level1b: 9,
386 Level1c: 10,
387 Level1d: 11,
388 Level1e: 12,
389 },
390 Loop: Loop{
391 Loop1: 13,
392 Loop2: 14,
393 },
394 Embed0p: Embed0p{
395 Point: image.Point{X: 15, Y: 16},
396 },
397 Embed0q: Embed0q{
398 Point: Point{Z: 17},
399 },
400 embed: embed{
401 Q: 18,
402 },
403 },
404 },
405 {
406 in: `{"hello": 1}`,
407 ptr: new(Ambig),
408 out: Ambig{First: 1},
409 },
410
411 {
412 in: `{"X": 1,"Y":2}`,
413 ptr: new(S5),
414 out: S5{S8: S8{S9: S9{Y: 2}}},
415 },
416 {
417 in: `{"X": 1,"Y":2}`,
418 ptr: new(S10),
419 out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}},
420 },
421
422 // Ill-formed UTF-8 is rejected.
423 {
424 in: "\"hello\xffworld\"",
425 err: &SyntaxError{"invalid character '\\xff' in string literal (expecting UTF-8 leading byte)", 7},
426 },
427 {
428 in: "\"hello\xc2\xc2world\"",
429 err: &SyntaxError{"invalid character '\\xc2' in UTF-8 multi-byte sequence (expecting UTF-8 continuation byte)", 8},
430 },
431 {
432 in: "\"hello\xc2\xffworld\"",
433 err: &SyntaxError{"invalid character '\\xff' in UTF-8 multi-byte sequence (expecting UTF-8 continuation byte)", 8},
434 },
435 {
436 in: "\"hello\\ud800world\"",
437 ptr: new(string),
438 out: "hello" + wtf8(0xd800) + "world",
439 },
440 {
441 in: "\"hello\\ud800\\ud800world\"",
442 ptr: new(string),
443 out: "hello" + wtf8(0xd800) + wtf8(0xd800) + "world",
444 },
445 {
446 in: "\"hello\xed\xa0\x80\xed\xb0\x80world\"",
447 err: &SyntaxError{"invalid character '\\xa0' in UTF-8 three-byte sequence (expecting well-formed UTF-8 continuation byte)", 8},
448 },
449 {
450 label: "issue 8305",
451 in: `{"2009-11-10T23:00:00Z": "hello world"}`,
452 ptr: &map[time.Time]string{},
453 err: &UnmarshalTypeError{"object", reflect.TypeOf(map[time.Time]string{}), 1},
454 },
455 }
456
457 // TestMarshalNumberZeroVal ensures that, unlike encoding/json, we don't
458 // marshal empty strings as "0".
459 func TestMarshalNumberZeroVal(t *testing.T) {
460 n := Number("")
461 _, err := Marshal(n)
462 got := fmt.Sprintf("%v", err)
463 want := `canonicaljson: invalid number literal ""`
464 if got != want {
465 t.Fatalf("got err = %q, want %q", got, err)
466 }
467 }
468
469 func TestMarshalEmbeds(t *testing.T) {
470 top := &Top{
471 Level0: 1,
472 Embed0: Embed0{
473 Level1b: 2,
474 Level1c: 3,
475 },
476 Embed0a: &Embed0a{
477 Level1a: 5,
478 Level1b: 6,
479 },
480 Embed0b: &Embed0b{
481 Level1a: 8,
482 Level1b: 9,
483 Level1c: 10,
484 Level1d: 11,
485 Level1e: 12,
486 },
487 Loop: Loop{
488 Loop1: 13,
489 Loop2: 14,
490 },
491 Embed0p: Embed0p{
492 Point: image.Point{X: 15, Y: 16},
493 },
494 Embed0q: Embed0q{
495 Point: Point{Z: 17},
496 },
497 embed: embed{
498 Q: 18,
499 },
500 }
501 b, err := Marshal(top)
502 if err != nil {
503 t.Fatal(err)
504 }
505 want := `{"LEVEL1B":6,"Level0":1,"Level1a":5,"Level1b":2,"Level1c":3,"Loop1":13,"Loop2":14,"Q":18,"X":15,"Y":16,"Z":17,"e":{"Level1a":8,"Level1b":9,"Level1c":10,"Level1d":11,"x":12}}`
506 if string(b) != want {
507 t.Errorf("Wrong marshal result.\n got: %q\nwant: %q", b, want)
508 }
509 }
510
511 func TestUnmarshal(t *testing.T) {
512 for i, tt := range unmarshalTests {
513 label := fmt.Sprintf("#%d %s [%q]", i, tt.label, tt.in)
514 var scan scanner
515 in := []byte(tt.in)
516 if err := checkValid(in, &scan); err != nil {
517 if !reflect.DeepEqual(err, tt.err) {
518 t.Errorf("%s: checkValid: %#v", label, err)
519 continue
520 }
521 }
522 if tt.ptr == nil {
523 continue
524 }
525
526 // v = new(right-type)
527 v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
528 dec := NewDecoder(bytes.NewReader(in))
529 if tt.useNumber {
530 dec.UseNumber()
531 }
532 if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.err) {
533 t.Errorf("%s: Decode error: have %v, want %v", label, err, tt.err)
534 continue
535 } else if err != nil {
536 continue
537 }
538 if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
539 t.Errorf("%s: Decode mismatch\nhave: %#+v\nwant: %#+v", label, v.Elem().Interface(), tt.out)
540 data, _ := Marshal(v.Elem().Interface())
541 println(string(data))
542 data, _ = Marshal(tt.out)
543 println(string(data))
544 continue
545 }
546
547 // Check round trip.
548 if tt.err == nil {
549 enc, err := Marshal(v.Interface())
550 if err != nil {
551 t.Errorf("%s: error re-marshaling: %v", label, err)
552 continue
553 }
554 vv := reflect.New(reflect.TypeOf(tt.ptr).Elem())
555 dec = NewDecoder(bytes.NewReader(enc))
556 if tt.useNumber {
557 dec.UseNumber()
558 }
559 if err := dec.Decode(vv.Interface()); err != nil {
560 t.Errorf("%s: error re-unmarshaling %#q: %v", label, enc, err)
561 continue
562 }
563 if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) {
564 t.Errorf("%s: round-trip mismatch\nhave: %#+v\nwant: %#+v", label, v.Elem().Interface(), vv.Elem().Interface())
565 t.Errorf(" In: %q", strings.Map(noSpace, string(in)))
566 t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc)))
567 continue
568 }
569 }
570 }
571 }
572
573 var numberTests = []struct {
574 in string
575 i int64
576 intErr string
577 f float64
578 floatErr string
579 }{
580 {in: "-1.23e1", intErr: "strconv.ParseInt: parsing \"-1.23e1\": invalid syntax", f: -1.23e1},
581 {in: "-12", i: -12, f: -12.0},
582 {in: "1e1000", intErr: "strconv.ParseInt: parsing \"1e1000\": invalid syntax", floatErr: "strconv.ParseFloat: parsing \"1e1000\": value out of range"},
583 }
584
585 // Independent of Decode, basic coverage of the accessors in Number
586 func TestNumberAccessors(t *testing.T) {
587 for _, tt := range numberTests {
588 n := Number(tt.in)
589 if s := n.String(); s != tt.in {
590 t.Errorf("Number(%q).String() is %q", tt.in, s)
591 }
592 if i, err := n.Int64(); err == nil && tt.intErr == "" && i != tt.i {
593 t.Errorf("Number(%q).Int64() is %d", tt.in, i)
594 } else if (err == nil && tt.intErr != "") || (err != nil && err.Error() != tt.intErr) {
595 t.Errorf("Number(%q).Int64() wanted error %q but got: %v", tt.in, tt.intErr, err)
596 }
597 if f, err := n.Float64(); err == nil && tt.floatErr == "" && f != tt.f {
598 t.Errorf("Number(%q).Float64() is %g", tt.in, f)
599 } else if (err == nil && tt.floatErr != "") || (err != nil && err.Error() != tt.floatErr) {
600 t.Errorf("Number(%q).Float64() wanted error %q but got: %v", tt.in, tt.floatErr, err)
601 }
602 }
603 }
604
605 func TestLargeByteSlice(t *testing.T) {
606 s0 := make([]byte, 2000)
607 for i := range s0 {
608 s0[i] = byte(i)
609 }
610 b, err := Marshal(s0)
611 if err != nil {
612 t.Fatalf("Marshal: %v", err)
613 }
614 var s1 []byte
615 if err := Unmarshal(b, &s1); err != nil {
616 t.Fatalf("Unmarshal: %v", err)
617 }
618 if !bytes.Equal(s0, s1) {
619 t.Errorf("Marshal large byte slice")
620 diff(t, s0, s1)
621 }
622 }
623
624 type Xint struct {
625 X int
626 }
627
628 func TestUnmarshalInterface(t *testing.T) {
629 var xint Xint
630 var i interface{} = &xint
631 if err := Unmarshal([]byte(`{"X":1}`), &i); err != nil {
632 t.Fatalf("Unmarshal: %v", err)
633 }
634 if xint.X != 1 {
635 t.Fatalf("Did not write to xint")
636 }
637 }
638
639 func TestUnmarshalPtrPtr(t *testing.T) {
640 var xint Xint
641 pxint := &xint
642 if err := Unmarshal([]byte(`{"X":1}`), &pxint); err != nil {
643 t.Fatalf("Unmarshal: %v", err)
644 }
645 if xint.X != 1 {
646 t.Fatalf("Did not write to xint")
647 }
648 }
649
650 func TestEscape(t *testing.T) {
651 const input = `"foobar"<html>` + " [\u2028 \u2029]"
652 const expected = `"` + `\"foobar\"<html>` + " [\u2028 \u2029]" + `"`
653 b, err := Marshal(input)
654 if err != nil {
655 t.Fatalf("Marshal error: %v", err)
656 }
657 if s := string(b); s != expected {
658 t.Errorf("Encoding of [%s]:\n got [%s]\nwant [%s]", input, s, expected)
659 }
660 }
661
662 // WrongString is a struct that's misusing the ,string modifier.
663 type WrongString struct {
664 Message string `json:"result,string"`
665 }
666
667 type wrongStringTest struct {
668 in, err string
669 }
670
671 var wrongStringTests = []wrongStringTest{
672 {`{"result":"x"}`, `canonicaljson: invalid use of ,string struct tag, trying to unmarshal "x" into string`},
673 {`{"result":"foo"}`, `canonicaljson: invalid use of ,string struct tag, trying to unmarshal "foo" into string`},
674 {`{"result":"123"}`, `canonicaljson: invalid use of ,string struct tag, trying to unmarshal "123" into string`},
675 {`{"result":123}`, `canonicaljson: invalid use of ,string struct tag, trying to unmarshal unquoted value into string`},
676 }
677
678 // If people misuse the ,string modifier, the error message should be
679 // helpful, telling the user that they're doing it wrong.
680 func TestErrorMessageFromMisusedString(t *testing.T) {
681 for n, tt := range wrongStringTests {
682 r := strings.NewReader(tt.in)
683 var s WrongString
684 err := NewDecoder(r).Decode(&s)
685 got := fmt.Sprintf("%v", err)
686 if got != tt.err {
687 t.Errorf("%d. got err = %q, want %q", n, got, tt.err)
688 }
689 }
690 }
691
692 func noSpace(c rune) rune {
693 if isSpace(byte(c)) { //only used for ascii
694 return -1
695 }
696 return c
697 }
698
699 type All struct {
700 Bool bool
701 Int int
702 Int8 int8
703 Int16 int16
704 Int32 int32
705 Int64 int64
706 Uint uint
707 Uint8 uint8
708 Uint16 uint16
709 Uint32 uint32
710 Uint64 uint64
711 Uintptr uintptr
712 Float32 float32
713 Float64 float64
714
715 Foo string `json:"bar"`
716 Foo2 string `json:"bar2,dummyopt"`
717
718 IntStr int64 `json:",string"`
719
720 PBool *bool
721 PInt *int
722 PInt8 *int8
723 PInt16 *int16
724 PInt32 *int32
725 PInt64 *int64
726 PUint *uint
727 PUint8 *uint8
728 PUint16 *uint16
729 PUint32 *uint32
730 PUint64 *uint64
731 PUintptr *uintptr
732 PFloat32 *float32
733 PFloat64 *float64
734
735 String string
736 PString *string
737
738 Map map[string]Small
739 MapP map[string]*Small
740 PMap *map[string]Small
741 PMapP *map[string]*Small
742
743 EmptyMap map[string]Small
744 NilMap map[string]Small
745
746 Slice []Small
747 SliceP []*Small
748 PSlice *[]Small
749 PSliceP *[]*Small
750
751 EmptySlice []Small
752 NilSlice []Small
753
754 StringSlice []string
755 ByteSlice []byte
756
757 Small Small
758 PSmall *Small
759 PPSmall **Small
760
761 Interface interface{}
762 PInterface *interface{}
763
764 unexported int
765 }
766
767 type Small struct {
768 Tag string
769 }
770
771 var allValue = All{
772 Bool: true,
773 Int: 2,
774 Int8: 3,
775 Int16: 4,
776 Int32: 5,
777 Int64: 6,
778 Uint: 7,
779 Uint8: 8,
780 Uint16: 9,
781 Uint32: 10,
782 Uint64: 11,
783 Uintptr: 12,
784 Float32: 14.1,
785 Float64: 15.1,
786 Foo: "foo",
787 Foo2: "foo2",
788 IntStr: 42,
789 String: "16",
790 Map: map[string]Small{
791 "17": {Tag: "tag17"},
792 "18": {Tag: "tag18"},
793 },
794 MapP: map[string]*Small{
795 "19": {Tag: "tag19"},
796 "20": nil,
797 },
798 EmptyMap: map[string]Small{},
799 Slice: []Small{{Tag: "tag20"}, {Tag: "tag21"}},
800 SliceP: []*Small{{Tag: "tag22"}, nil, {Tag: "tag23"}},
801 EmptySlice: []Small{},
802 StringSlice: []string{"str24", "str25", "str26"},
803 ByteSlice: []byte{27, 28, 29},
804 Small: Small{Tag: "tag30"},
805 PSmall: &Small{Tag: "tag31"},
806 Interface: 5.2,
807 }
808
809 var pallValue = All{
810 PBool: &allValue.Bool,
811 PInt: &allValue.Int,
812 PInt8: &allValue.Int8,
813 PInt16: &allValue.Int16,
814 PInt32: &allValue.Int32,
815 PInt64: &allValue.Int64,
816 PUint: &allValue.Uint,
817 PUint8: &allValue.Uint8,
818 PUint16: &allValue.Uint16,
819 PUint32: &allValue.Uint32,
820 PUint64: &allValue.Uint64,
821 PUintptr: &allValue.Uintptr,
822 PFloat32: &allValue.Float32,
823 PFloat64: &allValue.Float64,
824 PString: &allValue.String,
825 PMap: &allValue.Map,
826 PMapP: &allValue.MapP,
827 PSlice: &allValue.Slice,
828 PSliceP: &allValue.SliceP,
829 PPSmall: &allValue.PSmall,
830 PInterface: &allValue.Interface,
831 }
832
833 var allValueIndent = `{
834 "Bool": true,
835 "Int": 2,
836 "Int8": 3,
837 "Int16": 4,
838 "Int32": 5,
839 "Int64": 6,
840 "Uint": 7,
841 "Uint8": 8,
842 "Uint16": 9,
843 "Uint32": 10,
844 "Uint64": 11,
845 "Uintptr": 12,
846 "Float32": 14.1,
847 "Float64": 15.1,
848 "bar": "foo",
849 "bar2": "foo2",
850 "IntStr": "42",
851 "PBool": null,
852 "PInt": null,
853 "PInt8": null,
854 "PInt16": null,
855 "PInt32": null,
856 "PInt64": null,
857 "PUint": null,
858 "PUint8": null,
859 "PUint16": null,
860 "PUint32": null,
861 "PUint64": null,
862 "PUintptr": null,
863 "PFloat32": null,
864 "PFloat64": null,
865 "String": "16",
866 "PString": null,
867 "Map": {
868 "17": {
869 "Tag": "tag17"
870 },
871 "18": {
872 "Tag": "tag18"
873 }
874 },
875 "MapP": {
876 "19": {
877 "Tag": "tag19"
878 },
879 "20": null
880 },
881 "PMap": null,
882 "PMapP": null,
883 "EmptyMap": {},
884 "NilMap": null,
885 "Slice": [
886 {
887 "Tag": "tag20"
888 },
889 {
890 "Tag": "tag21"
891 }
892 ],
893 "SliceP": [
894 {
895 "Tag": "tag22"
896 },
897 null,
898 {
899 "Tag": "tag23"
900 }
901 ],
902 "PSlice": null,
903 "PSliceP": null,
904 "EmptySlice": [],
905 "NilSlice": null,
906 "StringSlice": [
907 "str24",
908 "str25",
909 "str26"
910 ],
911 "ByteSlice": "Gxwd",
912 "Small": {
913 "Tag": "tag30"
914 },
915 "PSmall": {
916 "Tag": "tag31"
917 },
918 "PPSmall": null,
919 "Interface": 5.2,
920 "PInterface": null
921 }`
922
923 var allValueCompact = strings.Map(noSpace, allValueIndent)
924
925 var pallValueIndent = `{
926 "Bool": false,
927 "Int": 0,
928 "Int8": 0,
929 "Int16": 0,
930 "Int32": 0,
931 "Int64": 0,
932 "Uint": 0,
933 "Uint8": 0,
934 "Uint16": 0,
935 "Uint32": 0,
936 "Uint64": 0,
937 "Uintptr": 0,
938 "Float32": 0,
939 "Float64": 0,
940 "bar": "",
941 "bar2": "",
942 "IntStr": "0",
943 "PBool": true,
944 "PInt": 2,
945 "PInt8": 3,
946 "PInt16": 4,
947 "PInt32": 5,
948 "PInt64": 6,
949 "PUint": 7,
950 "PUint8": 8,
951 "PUint16": 9,
952 "PUint32": 10,
953 "PUint64": 11,
954 "PUintptr": 12,
955 "PFloat32": 14.1,
956 "PFloat64": 15.1,
957 "String": "",
958 "PString": "16",
959 "Map": null,
960 "MapP": null,
961 "PMap": {
962 "17": {
963 "Tag": "tag17"
964 },
965 "18": {
966 "Tag": "tag18"
967 }
968 },
969 "PMapP": {
970 "19": {
971 "Tag": "tag19"
972 },
973 "20": null
974 },
975 "EmptyMap": null,
976 "NilMap": null,
977 "Slice": null,
978 "SliceP": null,
979 "PSlice": [
980 {
981 "Tag": "tag20"
982 },
983 {
984 "Tag": "tag21"
985 }
986 ],
987 "PSliceP": [
988 {
989 "Tag": "tag22"
990 },
991 null,
992 {
993 "Tag": "tag23"
994 }
995 ],
996 "EmptySlice": null,
997 "NilSlice": null,
998 "StringSlice": null,
999 "ByteSlice": null,
1000 "Small": {
1001 "Tag": ""
1002 },
1003 "PSmall": null,
1004 "PPSmall": {
1005 "Tag": "tag31"
1006 },
1007 "Interface": null,
1008 "PInterface": 5.2
1009 }`
1010
1011 var pallValueCompact = strings.Map(noSpace, pallValueIndent)
1012
1013 func TestRefUnmarshal(t *testing.T) {
1014 type S struct {
1015 // Ref is defined in encode_test.go.
1016 R0 Ref
1017 R1 *Ref
1018 R2 RefText
1019 R3 *RefText
1020 }
1021 want := S{
1022 R0: 12,
1023 R1: new(Ref),
1024 R2: 13,
1025 R3: new(RefText),
1026 }
1027 *want.R1 = 12
1028 *want.R3 = 13
1029
1030 var got S
1031 if err := Unmarshal([]byte(`{"R0":"ref","R1":"ref","R2":"ref","R3":"ref"}`), &got); err != nil {
1032 t.Fatalf("Unmarshal: %v", err)
1033 }
1034 if !reflect.DeepEqual(got, want) {
1035 t.Errorf("got %+v, want %+v", got, want)
1036 }
1037 }
1038
1039 // Test that the empty string doesn't panic decoding when ,string is specified
1040 // Issue 3450
1041 func TestEmptyString(t *testing.T) {
1042 type T2 struct {
1043 Number1 int `json:",string"`
1044 Number2 int `json:",string"`
1045 }
1046 data := `{"Number1":"1", "Number2":""}`
1047 dec := NewDecoder(strings.NewReader(data))
1048 var t2 T2
1049 err := dec.Decode(&t2)
1050 if err == nil {
1051 t.Fatal("Decode: did not return error")
1052 }
1053 if t2.Number1 != 1 {
1054 t.Fatal("Decode: did not set Number1")
1055 }
1056 }
1057
1058 // Test that a null for ,string is not replaced with the previous quoted string (issue 7046).
1059 // It should also not be an error (issue 2540, issue 8587).
1060 func TestNullString(t *testing.T) {
1061 type T struct {
1062 A int `json:",string"`
1063 B int `json:",string"`
1064 C *int `json:",string"`
1065 }
1066 data := []byte(`{"A": "1", "B": null, "C": null}`)
1067 var s T
1068 s.B = 1
1069 s.C = new(int)
1070 *s.C = 2
1071 err := Unmarshal(data, &s)
1072 if err != nil {
1073 t.Fatalf("Unmarshal: %v", err)
1074 }
1075 if s.B != 1 || s.C != nil {
1076 t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C)
1077 }
1078 }
1079
1080 func intp(x int) *int {
1081 p := new(int)
1082 *p = x
1083 return p
1084 }
1085
1086 func intpp(x *int) **int {
1087 pp := new(*int)
1088 *pp = x
1089 return pp
1090 }
1091
1092 var interfaceSetTests = []struct {
1093 pre interface{}
1094 json string
1095 post interface{}
1096 }{
1097 {"foo", `"bar"`, "bar"},
1098 {"foo", `2`, 2.0},
1099 {"foo", `true`, true},
1100 {"foo", `null`, nil},
1101
1102 {nil, `null`, nil},
1103 {new(int), `null`, nil},
1104 {(*int)(nil), `null`, nil},
1105 {new(*int), `null`, new(*int)},
1106 {(**int)(nil), `null`, nil},
1107 {intp(1), `null`, nil},
1108 {intpp(nil), `null`, intpp(nil)},
1109 {intpp(intp(1)), `null`, intpp(nil)},
1110 }
1111
1112 func TestInterfaceSet(t *testing.T) {
1113 for _, tt := range interfaceSetTests {
1114 b := struct{ X interface{} }{tt.pre}
1115 blob := `{"X":` + tt.json + `}`
1116 if err := Unmarshal([]byte(blob), &b); err != nil {
1117 t.Errorf("Unmarshal %#q: %v", blob, err)
1118 continue
1119 }
1120 if !reflect.DeepEqual(b.X, tt.post) {
1121 t.Errorf("Unmarshal %#q into %#v: X=%#v, want %#v", blob, tt.pre, b.X, tt.post)
1122 }
1123 }
1124 }
1125
1126 // JSON null values should be ignored for primitives and string values instead of resulting in an error.
1127 // Issue 2540
1128 func TestUnmarshalNulls(t *testing.T) {
1129 jsonData := []byte(`{
1130 "Bool" : null,
1131 "Int" : null,
1132 "Int8" : null,
1133 "Int16" : null,
1134 "Int32" : null,
1135 "Int64" : null,
1136 "Uint" : null,
1137 "Uint8" : null,
1138 "Uint16" : null,
1139 "Uint32" : null,
1140 "Uint64" : null,
1141 "Float32" : null,
1142 "Float64" : null,
1143 "String" : null}`)
1144
1145 nulls := All{
1146 Bool: true,
1147 Int: 2,
1148 Int8: 3,
1149 Int16: 4,
1150 Int32: 5,
1151 Int64: 6,
1152 Uint: 7,
1153 Uint8: 8,
1154 Uint16: 9,
1155 Uint32: 10,
1156 Uint64: 11,
1157 Float32: 12.1,
1158 Float64: 13.1,
1159 String: "14"}
1160
1161 err := Unmarshal(jsonData, &nulls)
1162 if err != nil {
1163 t.Errorf("Unmarshal of null values failed: %v", err)
1164 }
1165 if !nulls.Bool || nulls.Int != 2 || nulls.Int8 != 3 || nulls.Int16 != 4 || nulls.Int32 != 5 || nulls.Int64 != 6 ||
1166 nulls.Uint != 7 || nulls.Uint8 != 8 || nulls.Uint16 != 9 || nulls.Uint32 != 10 || nulls.Uint64 != 11 ||
1167 nulls.Float32 != 12.1 || nulls.Float64 != 13.1 || nulls.String != "14" {
1168
1169 t.Errorf("Unmarshal of null values affected primitives")
1170 }
1171 }
1172
1173 func TestStringKind(t *testing.T) {
1174 type stringKind string
1175
1176 var m1, m2 map[stringKind]int
1177 m1 = map[stringKind]int{
1178 "foo": 42,
1179 }
1180
1181 data, err := Marshal(m1)
1182 if err != nil {
1183 t.Errorf("Unexpected error marshaling: %v", err)
1184 }
1185
1186 err = Unmarshal(data, &m2)
1187 if err != nil {
1188 t.Errorf("Unexpected error unmarshaling: %v", err)
1189 }
1190
1191 if !reflect.DeepEqual(m1, m2) {
1192 t.Error("Items should be equal after encoding and then decoding")
1193 }
1194 }
1195
1196 // Custom types with []byte as underlying type could not be marshalled
1197 // and then unmarshalled.
1198 // Issue 8962.
1199 func TestByteKind(t *testing.T) {
1200 type byteKind []byte
1201
1202 a := byteKind("hello")
1203
1204 data, err := Marshal(a)
1205 if err != nil {
1206 t.Error(err)
1207 }
1208 var b byteKind
1209 err = Unmarshal(data, &b)
1210 if err != nil {
1211 t.Fatal(err)
1212 }
1213 if !reflect.DeepEqual(a, b) {
1214 t.Errorf("expected %v == %v", a, b)
1215 }
1216 }
1217
1218 // The fix for issue 8962 introduced a regression.
1219 // Issue 12921.
1220 func TestSliceOfCustomByte(t *testing.T) {
1221 type Uint8 uint8
1222
1223 a := []Uint8("hello")
1224
1225 data, err := Marshal(a)
1226 if err != nil {
1227 t.Fatal(err)
1228 }
1229 var b []Uint8
1230 err = Unmarshal(data, &b)
1231 if err != nil {
1232 t.Fatal(err)
1233 }
1234 if !reflect.DeepEqual(a, b) {
1235 t.Fatal("expected %v == %v", a, b)
1236 }
1237 }
1238
1239 var decodeTypeErrorTests = []struct {
1240 dest interface{}
1241 src string
1242 }{
1243 {new(string), `{"user": "name"}`}, // issue 4628.
1244 {new(error), `{}`}, // issue 4222
1245 {new(error), `[]`},
1246 {new(error), `""`},
1247 {new(error), `123`},
1248 {new(error), `true`},
1249 }
1250
1251 func TestUnmarshalTypeError(t *testing.T) {
1252 for _, item := range decodeTypeErrorTests {
1253 err := Unmarshal([]byte(item.src), item.dest)
1254 if _, ok := err.(*UnmarshalTypeError); !ok {
1255 t.Errorf("expected type error for Unmarshal(%q, type %T): got %T",
1256 item.src, item.dest, err)
1257 }
1258 }
1259 }
1260
1261 var unmarshalSyntaxTests = []string{
1262 "tru",
1263 "fals",
1264 "nul",
1265 "123e",
1266 `"hello`,
1267 `[1,2,3`,
1268 `{"key":1`,
1269 `{"key":1,`,
1270 }
1271
1272 func TestUnmarshalSyntax(t *testing.T) {
1273 var x interface{}
1274 for _, src := range unmarshalSyntaxTests {
1275 err := Unmarshal([]byte(src), &x)
1276 if _, ok := err.(*SyntaxError); !ok {
1277 t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err)
1278 }
1279 }
1280 }
1281
1282 // Test handling of unexported fields that should be ignored.
1283 // Issue 4660
1284 type unexportedFields struct {
1285 Name string
1286 m map[string]interface{} `json:"-"`
1287 m2 map[string]interface{} `json:"abcd"`
1288 }
1289
1290 func TestUnmarshalUnexported(t *testing.T) {
1291 input := `{"Name": "Bob", "m": {"x": 123}, "m2": {"y": 456}, "abcd": {"z": 789}}`
1292 want := &unexportedFields{Name: "Bob"}
1293
1294 out := &unexportedFields{}
1295 err := Unmarshal([]byte(input), out)
1296 if err != nil {
1297 t.Errorf("got error %v, expected nil", err)
1298 }
1299 if !reflect.DeepEqual(out, want) {
1300 t.Errorf("got %q, want %q", out, want)
1301 }
1302 }
1303
1304 // Time3339 is a time.Time which encodes to and from JSON
1305 // as an RFC 3339 time in UTC.
1306 type Time3339 time.Time
1307
1308 func (t *Time3339) UnmarshalJSON(b []byte) error {
1309 if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
1310 return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time", b)
1311 }
1312 tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1]))
1313 if err != nil {
1314 return err
1315 }
1316 *t = Time3339(tm)
1317 return nil
1318 }
1319
1320 func TestUnmarshalJSONLiteralError(t *testing.T) {
1321 var t3 Time3339
1322 err := Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3)
1323 if err == nil {
1324 t.Fatalf("expected error; got time %v", time.Time(t3))
1325 }
1326 if !strings.Contains(err.Error(), "range") {
1327 t.Errorf("got err = %v; want out of range error", err)
1328 }
1329 }
1330
1331 // Test that extra object elements in an array do not result in a
1332 // "data changing underfoot" error.
1333 // Issue 3717
1334 func TestSkipArrayObjects(t *testing.T) {
1335 json := `[{}]`
1336 var dest [0]interface{}
1337
1338 err := Unmarshal([]byte(json), &dest)
1339 if err != nil {
1340 t.Errorf("got error %q, want nil", err)
1341 }
1342 }
1343
1344 // Test semantics of pre-filled struct fields and pre-filled map fields.
1345 // Issue 4900.
1346 func TestPrefilled(t *testing.T) {
1347 ptrToMap := func(m map[string]interface{}) *map[string]interface{} { return &m }
1348
1349 // Values here change, cannot reuse table across runs.
1350 var prefillTests = []struct {
1351 in string
1352 ptr interface{}
1353 out interface{}
1354 }{
1355 {
1356 in: `{"X": 1, "Y": 2}`,
1357 ptr: &XYZ{X: float32(3), Y: int16(4), Z: 1.5},
1358 out: &XYZ{X: float64(1), Y: float64(2), Z: 1.5},
1359 },
1360 {
1361 in: `{"X": 1, "Y": 2}`,
1362 ptr: ptrToMap(map[string]interface{}{"X": float32(3), "Y": int16(4), "Z": 1.5}),
1363 out: ptrToMap(map[string]interface{}{"X": float64(1), "Y": float64(2), "Z": 1.5}),
1364 },
1365 }
1366
1367 for _, tt := range prefillTests {
1368 ptrstr := fmt.Sprintf("%v", tt.ptr)
1369 err := Unmarshal([]byte(tt.in), tt.ptr) // tt.ptr edited here
1370 if err != nil {
1371 t.Errorf("Unmarshal: %v", err)
1372 }
1373 if !reflect.DeepEqual(tt.ptr, tt.out) {
1374 t.Errorf("Unmarshal(%#q, %s): have %v, want %v", tt.in, ptrstr, tt.ptr, tt.out)
1375 }
1376 }
1377 }
1378
1379 var invalidUnmarshalTests = []struct {
1380 v interface{}
1381 want string
1382 }{
1383 {nil, "canonicaljson: Unmarshal(nil)"},
1384 {struct{}{}, "canonicaljson: Unmarshal(non-pointer struct {})"},
1385 {(*int)(nil), "canonicaljson: Unmarshal(nil *int)"},
1386 }
1387
1388 func TestInvalidUnmarshal(t *testing.T) {
1389 buf := []byte(`{"a":"1"}`)
1390 for _, tt := range invalidUnmarshalTests {
1391 err := Unmarshal(buf, tt.v)
1392 if err == nil {
1393 t.Errorf("Unmarshal expecting error, got nil")
1394 continue
1395 }
1396 if got := err.Error(); got != tt.want {
1397 t.Errorf("Unmarshal = %q; want %q", got, tt.want)
1398 }
1399 }
1400 }
1401
1402 var invalidUnmarshalTextTests = []struct {
1403 v interface{}
1404 want string
1405 }{
1406 {nil, "canonicaljson: Unmarshal(nil)"},
1407 {struct{}{}, "canonicaljson: Unmarshal(non-pointer struct {})"},
1408 {(*int)(nil), "canonicaljson: Unmarshal(nil *int)"},
1409 {new(net.IP), "canonicaljson: cannot unmarshal string into Go value of type *net.IP"},
1410 }
1411
1412 func TestInvalidUnmarshalText(t *testing.T) {
1413 buf := []byte(`123`)
1414 for _, tt := range invalidUnmarshalTextTests {
1415 err := Unmarshal(buf, tt.v)
1416 if err == nil {
1417 t.Errorf("Unmarshal expecting error, got nil")
1418 continue
1419 }
1420 if got := err.Error(); got != tt.want {
1421 t.Errorf("Unmarshal = %q; want %q", got, tt.want)
1422 }
1423 }
1424 }
1425
1426 // Test that string option is ignored for invalid types.
1427 // Issue 9812.
1428 func TestInvalidStringOption(t *testing.T) {
1429 num := 0
1430 item := struct {
1431 T time.Time `json:",string"`
1432 M map[string]string `json:",string"`
1433 S []string `json:",string"`
1434 A [1]string `json:",string"`
1435 I interface{} `json:",string"`
1436 P *int `json:",string"`
1437 }{M: make(map[string]string), S: make([]string, 0), I: num, P: &num}
1438
1439 data, err := Marshal(item)
1440 if err != nil {
1441 t.Fatalf("Marshal: %v", err)
1442 }
1443
1444 err = Unmarshal(data, &item)
1445 if err != nil {
1446 t.Fatalf("Unmarshal: %v", err)
1447 }
1448 }