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