github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/encoding/json/decode_test.go (about) 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 "errors" 11 "fmt" 12 "image" 13 "math" 14 "math/big" 15 "net" 16 "reflect" 17 "strconv" 18 "strings" 19 "testing" 20 "time" 21 ) 22 23 type T struct { 24 X string 25 Y int 26 Z int `json:"-"` 27 } 28 29 type U struct { 30 Alphabet string `json:"alpha"` 31 } 32 33 type V struct { 34 F1 interface{} 35 F2 int32 36 F3 Number 37 F4 *VOuter 38 } 39 40 type VOuter struct { 41 V V 42 } 43 44 type W struct { 45 S SS 46 } 47 48 type SS string 49 50 func (*SS) UnmarshalJSON(data []byte) error { 51 return &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS(""))} 52 } 53 54 // ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and 55 // without UseNumber 56 var ifaceNumAsFloat64 = map[string]interface{}{ 57 "k1": float64(1), 58 "k2": "s", 59 "k3": []interface{}{float64(1), float64(2.0), float64(3e-3)}, 60 "k4": map[string]interface{}{"kk1": "s", "kk2": float64(2)}, 61 } 62 63 var ifaceNumAsNumber = map[string]interface{}{ 64 "k1": Number("1"), 65 "k2": "s", 66 "k3": []interface{}{Number("1"), Number("2.0"), Number("3e-3")}, 67 "k4": map[string]interface{}{"kk1": "s", "kk2": Number("2")}, 68 } 69 70 type tx struct { 71 x int 72 } 73 74 type u8 uint8 75 76 // A type that can unmarshal itself. 77 78 type unmarshaler struct { 79 T bool 80 } 81 82 func (u *unmarshaler) UnmarshalJSON(b []byte) error { 83 *u = unmarshaler{true} // All we need to see that UnmarshalJSON is called. 84 return nil 85 } 86 87 type ustruct struct { 88 M unmarshaler 89 } 90 91 type unmarshalerText struct { 92 A, B string 93 } 94 95 // needed for re-marshaling tests 96 func (u unmarshalerText) MarshalText() ([]byte, error) { 97 return []byte(u.A + ":" + u.B), nil 98 } 99 100 func (u *unmarshalerText) UnmarshalText(b []byte) error { 101 pos := bytes.IndexByte(b, ':') 102 if pos == -1 { 103 return errors.New("missing separator") 104 } 105 u.A, u.B = string(b[:pos]), string(b[pos+1:]) 106 return nil 107 } 108 109 var _ encoding.TextUnmarshaler = (*unmarshalerText)(nil) 110 111 type ustructText struct { 112 M unmarshalerText 113 } 114 115 // u8marshal is an integer type that can marshal/unmarshal itself. 116 type u8marshal uint8 117 118 func (u8 u8marshal) MarshalText() ([]byte, error) { 119 return []byte(fmt.Sprintf("u%d", u8)), nil 120 } 121 122 var errMissingU8Prefix = errors.New("missing 'u' prefix") 123 124 func (u8 *u8marshal) UnmarshalText(b []byte) error { 125 if !bytes.HasPrefix(b, []byte{'u'}) { 126 return errMissingU8Prefix 127 } 128 n, err := strconv.Atoi(string(b[1:])) 129 if err != nil { 130 return err 131 } 132 *u8 = u8marshal(n) 133 return nil 134 } 135 136 var _ encoding.TextUnmarshaler = (*u8marshal)(nil) 137 138 var ( 139 um0, um1 unmarshaler // target2 of unmarshaling 140 ump = &um1 141 umtrue = unmarshaler{true} 142 umslice = []unmarshaler{{true}} 143 umslicep = new([]unmarshaler) 144 umstruct = ustruct{unmarshaler{true}} 145 146 um0T, um1T unmarshalerText // target2 of unmarshaling 147 umpType = &um1T 148 umtrueXY = unmarshalerText{"x", "y"} 149 umsliceXY = []unmarshalerText{{"x", "y"}} 150 umslicepType = new([]unmarshalerText) 151 umstructType = new(ustructText) 152 umstructXY = ustructText{unmarshalerText{"x", "y"}} 153 154 ummapType = map[unmarshalerText]bool{} 155 ummapXY = map[unmarshalerText]bool{{"x", "y"}: true} 156 ) 157 158 // Test data structures for anonymous fields. 159 160 type Point struct { 161 Z int 162 } 163 164 type Top struct { 165 Level0 int 166 Embed0 167 *Embed0a 168 *Embed0b `json:"e,omitempty"` // treated as named 169 Embed0c `json:"-"` // ignored 170 Loop 171 Embed0p // has Point with X, Y, used 172 Embed0q // has Point with Z, used 173 embed // contains exported field 174 } 175 176 type Embed0 struct { 177 Level1a int // overridden by Embed0a's Level1a with json tag 178 Level1b int // used because Embed0a's Level1b is renamed 179 Level1c int // used because Embed0a's Level1c is ignored 180 Level1d int // annihilated by Embed0a's Level1d 181 Level1e int `json:"x"` // annihilated by Embed0a.Level1e 182 } 183 184 type Embed0a struct { 185 Level1a int `json:"Level1a,omitempty"` 186 Level1b int `json:"LEVEL1B,omitempty"` 187 Level1c int `json:"-"` 188 Level1d int // annihilated by Embed0's Level1d 189 Level1f int `json:"x"` // annihilated by Embed0's Level1e 190 } 191 192 type Embed0b Embed0 193 194 type Embed0c Embed0 195 196 type Embed0p struct { 197 image.Point 198 } 199 200 type Embed0q struct { 201 Point 202 } 203 204 type embed struct { 205 Q int 206 } 207 208 type Loop struct { 209 Loop1 int `json:",omitempty"` 210 Loop2 int `json:",omitempty"` 211 *Loop 212 } 213 214 // From reflect test: 215 // The X in S6 and S7 annihilate, but they also block the X in S8.S9. 216 type S5 struct { 217 S6 218 S7 219 S8 220 } 221 222 type S6 struct { 223 X int 224 } 225 226 type S7 S6 227 228 type S8 struct { 229 S9 230 } 231 232 type S9 struct { 233 X int 234 Y int 235 } 236 237 // From reflect test: 238 // The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9. 239 type S10 struct { 240 S11 241 S12 242 S13 243 } 244 245 type S11 struct { 246 S6 247 } 248 249 type S12 struct { 250 S6 251 } 252 253 type S13 struct { 254 S8 255 } 256 257 type Ambig struct { 258 // Given "hello", the first match should win. 259 First int `json:"HELLO"` 260 Second int `json:"Hello"` 261 } 262 263 type XYZ struct { 264 X interface{} 265 Y interface{} 266 Z interface{} 267 } 268 269 type unexportedWithMethods struct{} 270 271 func (unexportedWithMethods) F() {} 272 273 func sliceAddr(x []int) *[]int { return &x } 274 func mapAddr(x map[string]int) *map[string]int { return &x } 275 276 type byteWithMarshalJSON byte 277 278 func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) { 279 return []byte(fmt.Sprintf(`"Z%.2x"`, byte(b))), nil 280 } 281 282 func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error { 283 if len(data) != 5 || data[0] != '"' || data[1] != 'Z' || data[4] != '"' { 284 return fmt.Errorf("bad quoted string") 285 } 286 i, err := strconv.ParseInt(string(data[2:4]), 16, 8) 287 if err != nil { 288 return fmt.Errorf("bad hex") 289 } 290 *b = byteWithMarshalJSON(i) 291 return nil 292 } 293 294 type byteWithPtrMarshalJSON byte 295 296 func (b *byteWithPtrMarshalJSON) MarshalJSON() ([]byte, error) { 297 return byteWithMarshalJSON(*b).MarshalJSON() 298 } 299 300 func (b *byteWithPtrMarshalJSON) UnmarshalJSON(data []byte) error { 301 return (*byteWithMarshalJSON)(b).UnmarshalJSON(data) 302 } 303 304 type byteWithMarshalText byte 305 306 func (b byteWithMarshalText) MarshalText() ([]byte, error) { 307 return []byte(fmt.Sprintf(`Z%.2x`, byte(b))), nil 308 } 309 310 func (b *byteWithMarshalText) UnmarshalText(data []byte) error { 311 if len(data) != 3 || data[0] != 'Z' { 312 return fmt.Errorf("bad quoted string") 313 } 314 i, err := strconv.ParseInt(string(data[1:3]), 16, 8) 315 if err != nil { 316 return fmt.Errorf("bad hex") 317 } 318 *b = byteWithMarshalText(i) 319 return nil 320 } 321 322 type byteWithPtrMarshalText byte 323 324 func (b *byteWithPtrMarshalText) MarshalText() ([]byte, error) { 325 return byteWithMarshalText(*b).MarshalText() 326 } 327 328 func (b *byteWithPtrMarshalText) UnmarshalText(data []byte) error { 329 return (*byteWithMarshalText)(b).UnmarshalText(data) 330 } 331 332 type intWithMarshalJSON int 333 334 func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) { 335 return []byte(fmt.Sprintf(`"Z%.2x"`, int(b))), nil 336 } 337 338 func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error { 339 if len(data) != 5 || data[0] != '"' || data[1] != 'Z' || data[4] != '"' { 340 return fmt.Errorf("bad quoted string") 341 } 342 i, err := strconv.ParseInt(string(data[2:4]), 16, 8) 343 if err != nil { 344 return fmt.Errorf("bad hex") 345 } 346 *b = intWithMarshalJSON(i) 347 return nil 348 } 349 350 type intWithPtrMarshalJSON int 351 352 func (b *intWithPtrMarshalJSON) MarshalJSON() ([]byte, error) { 353 return intWithMarshalJSON(*b).MarshalJSON() 354 } 355 356 func (b *intWithPtrMarshalJSON) UnmarshalJSON(data []byte) error { 357 return (*intWithMarshalJSON)(b).UnmarshalJSON(data) 358 } 359 360 type intWithMarshalText int 361 362 func (b intWithMarshalText) MarshalText() ([]byte, error) { 363 return []byte(fmt.Sprintf(`Z%.2x`, int(b))), nil 364 } 365 366 func (b *intWithMarshalText) UnmarshalText(data []byte) error { 367 if len(data) != 3 || data[0] != 'Z' { 368 return fmt.Errorf("bad quoted string") 369 } 370 i, err := strconv.ParseInt(string(data[1:3]), 16, 8) 371 if err != nil { 372 return fmt.Errorf("bad hex") 373 } 374 *b = intWithMarshalText(i) 375 return nil 376 } 377 378 type intWithPtrMarshalText int 379 380 func (b *intWithPtrMarshalText) MarshalText() ([]byte, error) { 381 return intWithMarshalText(*b).MarshalText() 382 } 383 384 func (b *intWithPtrMarshalText) UnmarshalText(data []byte) error { 385 return (*intWithMarshalText)(b).UnmarshalText(data) 386 } 387 388 type mapStringToStringData struct { 389 Data map[string]string `json:"data"` 390 } 391 392 type unmarshalTest struct { 393 in string 394 ptr interface{} 395 out interface{} 396 err error 397 useNumber bool 398 golden bool 399 disallowUnknownFields bool 400 } 401 402 type B struct { 403 B bool `json:",string"` 404 } 405 406 var unmarshalTests = []unmarshalTest{ 407 // basic types 408 {in: `true`, ptr: new(bool), out: true}, 409 {in: `1`, ptr: new(int), out: 1}, 410 {in: `1.2`, ptr: new(float64), out: 1.2}, 411 {in: `-5`, ptr: new(int16), out: int16(-5)}, 412 {in: `2`, ptr: new(Number), out: Number("2"), useNumber: true}, 413 {in: `2`, ptr: new(Number), out: Number("2")}, 414 {in: `2`, ptr: new(interface{}), out: float64(2.0)}, 415 {in: `2`, ptr: new(interface{}), out: Number("2"), useNumber: true}, 416 {in: `"a\u1234"`, ptr: new(string), out: "a\u1234"}, 417 {in: `"http:\/\/"`, ptr: new(string), out: "http://"}, 418 {in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"}, 419 {in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"}, 420 {in: "null", ptr: new(interface{}), out: nil}, 421 {in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeOf(""), 7, "T", "X"}}, 422 {in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, 423 {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, 424 {in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true}, 425 {in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(SS("")), 0, "W", "S"}}, 426 {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}}, 427 {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true}, 428 {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64}, 429 {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsNumber, useNumber: true}, 430 431 // raw values with whitespace 432 {in: "\n true ", ptr: new(bool), out: true}, 433 {in: "\t 1 ", ptr: new(int), out: 1}, 434 {in: "\r 1.2 ", ptr: new(float64), out: 1.2}, 435 {in: "\t -5 \n", ptr: new(int16), out: int16(-5)}, 436 {in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"}, 437 438 // Z has a "-" tag. 439 {in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}}, 440 {in: `{"Y": 1, "Z": 2}`, ptr: new(T), err: fmt.Errorf("json: unknown field \"Z\""), disallowUnknownFields: true}, 441 442 {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}}, 443 {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, 444 {in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}}, 445 {in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}}, 446 {in: `{"alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, 447 448 // syntax errors 449 {in: `{"X": "foo", "Y"}`, err: &SyntaxError{"invalid character '}' after object key", 17}}, 450 {in: `[1, 2, 3+]`, err: &SyntaxError{"invalid character '+' after array element", 9}}, 451 {in: `{"X":12x}`, err: &SyntaxError{"invalid character 'x' after object key:value pair", 8}, useNumber: true}, 452 {in: `[2, 3`, err: &SyntaxError{msg: "unexpected end of JSON input", Offset: 5}}, 453 454 // raw value errors 455 {in: "\x01 42", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 456 {in: " 42 \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 5}}, 457 {in: "\x01 true", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 458 {in: " false \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 8}}, 459 {in: "\x01 1.2", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 460 {in: " 3.4 \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 6}}, 461 {in: "\x01 \"string\"", err: &SyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 462 {in: " \"string\" \x01", err: &SyntaxError{"invalid character '\\x01' after top-level value", 11}}, 463 464 // array tests 465 {in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}}, 466 {in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}}, 467 {in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}}, 468 {in: `[1, 2, 3]`, ptr: new(MustNotUnmarshalJSON), err: errors.New("MustNotUnmarshalJSON was used")}, 469 470 // empty array to interface test 471 {in: `[]`, ptr: new([]interface{}), out: []interface{}{}}, 472 {in: `null`, ptr: new([]interface{}), out: []interface{}(nil)}, 473 {in: `{"T":[]}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}}, 474 {in: `{"T":null}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": interface{}(nil)}}, 475 476 // composite tests 477 {in: allValueIndent, ptr: new(All), out: allValue}, 478 {in: allValueCompact, ptr: new(All), out: allValue}, 479 {in: allValueIndent, ptr: new(*All), out: &allValue}, 480 {in: allValueCompact, ptr: new(*All), out: &allValue}, 481 {in: pallValueIndent, ptr: new(All), out: pallValue}, 482 {in: pallValueCompact, ptr: new(All), out: pallValue}, 483 {in: pallValueIndent, ptr: new(*All), out: &pallValue}, 484 {in: pallValueCompact, ptr: new(*All), out: &pallValue}, 485 486 // unmarshal interface test 487 {in: `{"T":false}`, ptr: &um0, out: umtrue}, // use "false" so test will fail if custom unmarshaler is not called 488 {in: `{"T":false}`, ptr: &ump, out: &umtrue}, 489 {in: `[{"T":false}]`, ptr: &umslice, out: umslice}, 490 {in: `[{"T":false}]`, ptr: &umslicep, out: &umslice}, 491 {in: `{"M":{"T":"x:y"}}`, ptr: &umstruct, out: umstruct}, 492 493 // UnmarshalText interface test 494 {in: `"x:y"`, ptr: &um0T, out: umtrueXY}, 495 {in: `"x:y"`, ptr: &umpType, out: &umtrueXY}, 496 {in: `["x:y"]`, ptr: &umsliceXY, out: umsliceXY}, 497 {in: `["x:y"]`, ptr: &umslicepType, out: &umsliceXY}, 498 {in: `{"M":"x:y"}`, ptr: umstructType, out: umstructXY}, 499 500 // integer-keyed map test 501 { 502 in: `{"-1":"a","0":"b","1":"c"}`, 503 ptr: new(map[int]string), 504 out: map[int]string{-1: "a", 0: "b", 1: "c"}, 505 }, 506 { 507 in: `{"0":"a","10":"c","9":"b"}`, 508 ptr: new(map[u8]string), 509 out: map[u8]string{0: "a", 9: "b", 10: "c"}, 510 }, 511 { 512 in: `{"-9223372036854775808":"min","9223372036854775807":"max"}`, 513 ptr: new(map[int64]string), 514 out: map[int64]string{math.MinInt64: "min", math.MaxInt64: "max"}, 515 }, 516 { 517 in: `{"18446744073709551615":"max"}`, 518 ptr: new(map[uint64]string), 519 out: map[uint64]string{math.MaxUint64: "max"}, 520 }, 521 { 522 in: `{"0":false,"10":true}`, 523 ptr: new(map[uintptr]bool), 524 out: map[uintptr]bool{0: false, 10: true}, 525 }, 526 527 // Check that MarshalText and UnmarshalText take precedence 528 // over default integer handling in map keys. 529 { 530 in: `{"u2":4}`, 531 ptr: new(map[u8marshal]int), 532 out: map[u8marshal]int{2: 4}, 533 }, 534 { 535 in: `{"2":4}`, 536 ptr: new(map[u8marshal]int), 537 err: errMissingU8Prefix, 538 }, 539 540 // integer-keyed map errors 541 { 542 in: `{"abc":"abc"}`, 543 ptr: new(map[int]string), 544 err: &UnmarshalTypeError{Value: "number abc", Type: reflect.TypeOf(0), Offset: 2}, 545 }, 546 { 547 in: `{"256":"abc"}`, 548 ptr: new(map[uint8]string), 549 err: &UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2}, 550 }, 551 { 552 in: `{"128":"abc"}`, 553 ptr: new(map[int8]string), 554 err: &UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2}, 555 }, 556 { 557 in: `{"-1":"abc"}`, 558 ptr: new(map[uint8]string), 559 err: &UnmarshalTypeError{Value: "number -1", Type: reflect.TypeOf(uint8(0)), Offset: 2}, 560 }, 561 { 562 in: `{"F":{"a":2,"3":4}}`, 563 ptr: new(map[string]map[int]int), 564 err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(int(0)), Offset: 7}, 565 }, 566 { 567 in: `{"F":{"a":2,"3":4}}`, 568 ptr: new(map[string]map[uint]int), 569 err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(uint(0)), Offset: 7}, 570 }, 571 572 // Map keys can be encoding.TextUnmarshalers. 573 {in: `{"x:y":true}`, ptr: &ummapType, out: ummapXY}, 574 // If multiple values for the same key exists, only the most recent value is used. 575 {in: `{"x:y":false,"x:y":true}`, ptr: &ummapType, out: ummapXY}, 576 577 // Overwriting of data. 578 // This is different from package xml, but it's what we've always done. 579 // Now documented and tested. 580 {in: `[2]`, ptr: sliceAddr([]int{1}), out: []int{2}}, 581 {in: `{"key": 2}`, ptr: mapAddr(map[string]int{"old": 0, "key": 1}), out: map[string]int{"key": 2}}, 582 583 { 584 in: `{ 585 "Level0": 1, 586 "Level1b": 2, 587 "Level1c": 3, 588 "x": 4, 589 "Level1a": 5, 590 "LEVEL1B": 6, 591 "e": { 592 "Level1a": 8, 593 "Level1b": 9, 594 "Level1c": 10, 595 "Level1d": 11, 596 "x": 12 597 }, 598 "Loop1": 13, 599 "Loop2": 14, 600 "X": 15, 601 "Y": 16, 602 "Z": 17, 603 "Q": 18 604 }`, 605 ptr: new(Top), 606 out: Top{ 607 Level0: 1, 608 Embed0: Embed0{ 609 Level1b: 2, 610 Level1c: 3, 611 }, 612 Embed0a: &Embed0a{ 613 Level1a: 5, 614 Level1b: 6, 615 }, 616 Embed0b: &Embed0b{ 617 Level1a: 8, 618 Level1b: 9, 619 Level1c: 10, 620 Level1d: 11, 621 Level1e: 12, 622 }, 623 Loop: Loop{ 624 Loop1: 13, 625 Loop2: 14, 626 }, 627 Embed0p: Embed0p{ 628 Point: image.Point{X: 15, Y: 16}, 629 }, 630 Embed0q: Embed0q{ 631 Point: Point{Z: 17}, 632 }, 633 embed: embed{ 634 Q: 18, 635 }, 636 }, 637 }, 638 { 639 in: `{"hello": 1}`, 640 ptr: new(Ambig), 641 out: Ambig{First: 1}, 642 }, 643 644 { 645 in: `{"X": 1,"Y":2}`, 646 ptr: new(S5), 647 out: S5{S8: S8{S9: S9{Y: 2}}}, 648 }, 649 { 650 in: `{"X": 1,"Y":2}`, 651 ptr: new(S5), 652 err: fmt.Errorf("json: unknown field \"X\""), 653 disallowUnknownFields: true, 654 }, 655 { 656 in: `{"X": 1,"Y":2}`, 657 ptr: new(S10), 658 out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}}, 659 }, 660 { 661 in: `{"X": 1,"Y":2}`, 662 ptr: new(S10), 663 err: fmt.Errorf("json: unknown field \"X\""), 664 disallowUnknownFields: true, 665 }, 666 667 // invalid UTF-8 is coerced to valid UTF-8. 668 { 669 in: "\"hello\xffworld\"", 670 ptr: new(string), 671 out: "hello\ufffdworld", 672 }, 673 { 674 in: "\"hello\xc2\xc2world\"", 675 ptr: new(string), 676 out: "hello\ufffd\ufffdworld", 677 }, 678 { 679 in: "\"hello\xc2\xffworld\"", 680 ptr: new(string), 681 out: "hello\ufffd\ufffdworld", 682 }, 683 { 684 in: "\"hello\\ud800world\"", 685 ptr: new(string), 686 out: "hello\ufffdworld", 687 }, 688 { 689 in: "\"hello\\ud800\\ud800world\"", 690 ptr: new(string), 691 out: "hello\ufffd\ufffdworld", 692 }, 693 { 694 in: "\"hello\\ud800\\ud800world\"", 695 ptr: new(string), 696 out: "hello\ufffd\ufffdworld", 697 }, 698 { 699 in: "\"hello\xed\xa0\x80\xed\xb0\x80world\"", 700 ptr: new(string), 701 out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld", 702 }, 703 704 // Used to be issue 8305, but time.Time implements encoding.TextUnmarshaler so this works now. 705 { 706 in: `{"2009-11-10T23:00:00Z": "hello world"}`, 707 ptr: &map[time.Time]string{}, 708 out: map[time.Time]string{time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC): "hello world"}, 709 }, 710 711 // issue 8305 712 { 713 in: `{"2009-11-10T23:00:00Z": "hello world"}`, 714 ptr: &map[Point]string{}, 715 err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[Point]string{}), Offset: 1}, 716 }, 717 { 718 in: `{"asdf": "hello world"}`, 719 ptr: &map[unmarshaler]string{}, 720 err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[unmarshaler]string{}), Offset: 1}, 721 }, 722 723 // related to issue 13783. 724 // Go 1.7 changed marshaling a slice of typed byte to use the methods on the byte type, 725 // similar to marshaling a slice of typed int. 726 // These tests check that, assuming the byte type also has valid decoding methods, 727 // either the old base64 string encoding or the new per-element encoding can be 728 // successfully unmarshaled. The custom unmarshalers were accessible in earlier 729 // versions of Go, even though the custom marshaler was not. 730 { 731 in: `"AQID"`, 732 ptr: new([]byteWithMarshalJSON), 733 out: []byteWithMarshalJSON{1, 2, 3}, 734 }, 735 { 736 in: `["Z01","Z02","Z03"]`, 737 ptr: new([]byteWithMarshalJSON), 738 out: []byteWithMarshalJSON{1, 2, 3}, 739 golden: true, 740 }, 741 { 742 in: `"AQID"`, 743 ptr: new([]byteWithMarshalText), 744 out: []byteWithMarshalText{1, 2, 3}, 745 }, 746 { 747 in: `["Z01","Z02","Z03"]`, 748 ptr: new([]byteWithMarshalText), 749 out: []byteWithMarshalText{1, 2, 3}, 750 golden: true, 751 }, 752 { 753 in: `"AQID"`, 754 ptr: new([]byteWithPtrMarshalJSON), 755 out: []byteWithPtrMarshalJSON{1, 2, 3}, 756 }, 757 { 758 in: `["Z01","Z02","Z03"]`, 759 ptr: new([]byteWithPtrMarshalJSON), 760 out: []byteWithPtrMarshalJSON{1, 2, 3}, 761 golden: true, 762 }, 763 { 764 in: `"AQID"`, 765 ptr: new([]byteWithPtrMarshalText), 766 out: []byteWithPtrMarshalText{1, 2, 3}, 767 }, 768 { 769 in: `["Z01","Z02","Z03"]`, 770 ptr: new([]byteWithPtrMarshalText), 771 out: []byteWithPtrMarshalText{1, 2, 3}, 772 golden: true, 773 }, 774 775 // ints work with the marshaler but not the base64 []byte case 776 { 777 in: `["Z01","Z02","Z03"]`, 778 ptr: new([]intWithMarshalJSON), 779 out: []intWithMarshalJSON{1, 2, 3}, 780 golden: true, 781 }, 782 { 783 in: `["Z01","Z02","Z03"]`, 784 ptr: new([]intWithMarshalText), 785 out: []intWithMarshalText{1, 2, 3}, 786 golden: true, 787 }, 788 { 789 in: `["Z01","Z02","Z03"]`, 790 ptr: new([]intWithPtrMarshalJSON), 791 out: []intWithPtrMarshalJSON{1, 2, 3}, 792 golden: true, 793 }, 794 { 795 in: `["Z01","Z02","Z03"]`, 796 ptr: new([]intWithPtrMarshalText), 797 out: []intWithPtrMarshalText{1, 2, 3}, 798 golden: true, 799 }, 800 801 {in: `0.000001`, ptr: new(float64), out: 0.000001, golden: true}, 802 {in: `1e-7`, ptr: new(float64), out: 1e-7, golden: true}, 803 {in: `100000000000000000000`, ptr: new(float64), out: 100000000000000000000.0, golden: true}, 804 {in: `1e+21`, ptr: new(float64), out: 1e21, golden: true}, 805 {in: `-0.000001`, ptr: new(float64), out: -0.000001, golden: true}, 806 {in: `-1e-7`, ptr: new(float64), out: -1e-7, golden: true}, 807 {in: `-100000000000000000000`, ptr: new(float64), out: -100000000000000000000.0, golden: true}, 808 {in: `-1e+21`, ptr: new(float64), out: -1e21, golden: true}, 809 {in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true}, 810 {in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true}, 811 {in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false}, 812 813 { 814 in: `{"V": {"F2": "hello"}}`, 815 ptr: new(VOuter), 816 err: &UnmarshalTypeError{ 817 Value: "string", 818 Struct: "V", 819 Field: "F2", 820 Type: reflect.TypeOf(int32(0)), 821 Offset: 20, 822 }, 823 }, 824 { 825 in: `{"V": {"F4": {}, "F2": "hello"}}`, 826 ptr: new(VOuter), 827 err: &UnmarshalTypeError{ 828 Value: "string", 829 Struct: "V", 830 Field: "F2", 831 Type: reflect.TypeOf(int32(0)), 832 Offset: 30, 833 }, 834 }, 835 836 // issue 15146. 837 // invalid inputs in wrongStringTests below. 838 {in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true}, 839 {in: `{"B":"false"}`, ptr: new(B), out: B{false}, golden: true}, 840 {in: `{"B": "maybe"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "maybe" into bool`)}, 841 {in: `{"B": "tru"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "tru" into bool`)}, 842 {in: `{"B": "False"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "False" into bool`)}, 843 {in: `{"B": "null"}`, ptr: new(B), out: B{false}}, 844 {in: `{"B": "nul"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "nul" into bool`)}, 845 {in: `{"B": [2, 3]}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal unquoted value into bool`)}, 846 847 // additional tests for disallowUnknownFields 848 { 849 in: `{ 850 "Level0": 1, 851 "Level1b": 2, 852 "Level1c": 3, 853 "x": 4, 854 "Level1a": 5, 855 "LEVEL1B": 6, 856 "e": { 857 "Level1a": 8, 858 "Level1b": 9, 859 "Level1c": 10, 860 "Level1d": 11, 861 "x": 12 862 }, 863 "Loop1": 13, 864 "Loop2": 14, 865 "X": 15, 866 "Y": 16, 867 "Z": 17, 868 "Q": 18, 869 "extra": true 870 }`, 871 ptr: new(Top), 872 err: fmt.Errorf("json: unknown field \"extra\""), 873 disallowUnknownFields: true, 874 }, 875 { 876 in: `{ 877 "Level0": 1, 878 "Level1b": 2, 879 "Level1c": 3, 880 "x": 4, 881 "Level1a": 5, 882 "LEVEL1B": 6, 883 "e": { 884 "Level1a": 8, 885 "Level1b": 9, 886 "Level1c": 10, 887 "Level1d": 11, 888 "x": 12, 889 "extra": null 890 }, 891 "Loop1": 13, 892 "Loop2": 14, 893 "X": 15, 894 "Y": 16, 895 "Z": 17, 896 "Q": 18 897 }`, 898 ptr: new(Top), 899 err: fmt.Errorf("json: unknown field \"extra\""), 900 disallowUnknownFields: true, 901 }, 902 // issue 26444 903 // UnmarshalTypeError without field & struct values 904 { 905 in: `{"data":{"test1": "bob", "test2": 123}}`, 906 ptr: new(mapStringToStringData), 907 err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 37, Struct: "mapStringToStringData", Field: "data"}, 908 }, 909 { 910 in: `{"data":{"test1": 123, "test2": "bob"}}`, 911 ptr: new(mapStringToStringData), 912 err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 21, Struct: "mapStringToStringData", Field: "data"}, 913 }, 914 915 // trying to decode JSON arrays or objects via TextUnmarshaler 916 { 917 in: `[1, 2, 3]`, 918 ptr: new(MustNotUnmarshalText), 919 err: &UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1}, 920 }, 921 { 922 in: `{"foo": "bar"}`, 923 ptr: new(MustNotUnmarshalText), 924 err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1}, 925 }, 926 } 927 928 func TestMarshal(t *testing.T) { 929 b, err := Marshal(allValue) 930 if err != nil { 931 t.Fatalf("Marshal allValue: %v", err) 932 } 933 if string(b) != allValueCompact { 934 t.Errorf("Marshal allValueCompact") 935 diff(t, b, []byte(allValueCompact)) 936 return 937 } 938 939 b, err = Marshal(pallValue) 940 if err != nil { 941 t.Fatalf("Marshal pallValue: %v", err) 942 } 943 if string(b) != pallValueCompact { 944 t.Errorf("Marshal pallValueCompact") 945 diff(t, b, []byte(pallValueCompact)) 946 return 947 } 948 } 949 950 var badUTF8 = []struct { 951 in, out string 952 }{ 953 {"hello\xffworld", `"hello\ufffdworld"`}, 954 {"", `""`}, 955 {"\xff", `"\ufffd"`}, 956 {"\xff\xff", `"\ufffd\ufffd"`}, 957 {"a\xffb", `"a\ufffdb"`}, 958 {"\xe6\x97\xa5\xe6\x9c\xac\xff\xaa\x9e", `"日本\ufffd\ufffd\ufffd"`}, 959 } 960 961 func TestMarshalBadUTF8(t *testing.T) { 962 for _, tt := range badUTF8 { 963 b, err := Marshal(tt.in) 964 if string(b) != tt.out || err != nil { 965 t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", tt.in, b, err, tt.out) 966 } 967 } 968 } 969 970 func TestMarshalNumberZeroVal(t *testing.T) { 971 var n Number 972 out, err := Marshal(n) 973 if err != nil { 974 t.Fatal(err) 975 } 976 outStr := string(out) 977 if outStr != "0" { 978 t.Fatalf("Invalid zero val for Number: %q", outStr) 979 } 980 } 981 982 func TestMarshalEmbeds(t *testing.T) { 983 top := &Top{ 984 Level0: 1, 985 Embed0: Embed0{ 986 Level1b: 2, 987 Level1c: 3, 988 }, 989 Embed0a: &Embed0a{ 990 Level1a: 5, 991 Level1b: 6, 992 }, 993 Embed0b: &Embed0b{ 994 Level1a: 8, 995 Level1b: 9, 996 Level1c: 10, 997 Level1d: 11, 998 Level1e: 12, 999 }, 1000 Loop: Loop{ 1001 Loop1: 13, 1002 Loop2: 14, 1003 }, 1004 Embed0p: Embed0p{ 1005 Point: image.Point{X: 15, Y: 16}, 1006 }, 1007 Embed0q: Embed0q{ 1008 Point: Point{Z: 17}, 1009 }, 1010 embed: embed{ 1011 Q: 18, 1012 }, 1013 } 1014 b, err := Marshal(top) 1015 if err != nil { 1016 t.Fatal(err) 1017 } 1018 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,\"Q\":18}" 1019 if string(b) != want { 1020 t.Errorf("Wrong marshal result.\n got: %q\nwant: %q", b, want) 1021 } 1022 } 1023 1024 func TestUnmarshal(t *testing.T) { 1025 for i, tt := range unmarshalTests { 1026 var scan scanner 1027 in := []byte(tt.in) 1028 if err := checkValid(in, &scan); err != nil { 1029 if !reflect.DeepEqual(err, tt.err) { 1030 t.Errorf("#%d: checkValid: %#v", i, err) 1031 continue 1032 } 1033 } 1034 if tt.ptr == nil { 1035 continue 1036 } 1037 1038 // v = new(right-type) 1039 v := reflect.New(reflect.TypeOf(tt.ptr).Elem()) 1040 dec := NewDecoder(bytes.NewReader(in)) 1041 if tt.useNumber { 1042 dec.UseNumber() 1043 } 1044 if tt.disallowUnknownFields { 1045 dec.DisallowUnknownFields() 1046 } 1047 if err := dec.Decode(v.Interface()); !reflect.DeepEqual(err, tt.err) { 1048 t.Errorf("#%d: %v, want %v", i, err, tt.err) 1049 continue 1050 } else if err != nil { 1051 continue 1052 } 1053 if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { 1054 t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out) 1055 data, _ := Marshal(v.Elem().Interface()) 1056 println(string(data)) 1057 data, _ = Marshal(tt.out) 1058 println(string(data)) 1059 continue 1060 } 1061 1062 // Check round trip also decodes correctly. 1063 if tt.err == nil { 1064 enc, err := Marshal(v.Interface()) 1065 if err != nil { 1066 t.Errorf("#%d: error re-marshaling: %v", i, err) 1067 continue 1068 } 1069 if tt.golden && !bytes.Equal(enc, in) { 1070 t.Errorf("#%d: remarshal mismatch:\nhave: %s\nwant: %s", i, enc, in) 1071 } 1072 vv := reflect.New(reflect.TypeOf(tt.ptr).Elem()) 1073 dec = NewDecoder(bytes.NewReader(enc)) 1074 if tt.useNumber { 1075 dec.UseNumber() 1076 } 1077 if err := dec.Decode(vv.Interface()); err != nil { 1078 t.Errorf("#%d: error re-unmarshaling %#q: %v", i, enc, err) 1079 continue 1080 } 1081 if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) { 1082 t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface()) 1083 t.Errorf(" In: %q", strings.Map(noSpace, string(in))) 1084 t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc))) 1085 continue 1086 } 1087 } 1088 } 1089 } 1090 1091 func TestUnmarshalMarshal(t *testing.T) { 1092 initBig() 1093 var v interface{} 1094 if err := Unmarshal(jsonBig, &v); err != nil { 1095 t.Fatalf("Unmarshal: %v", err) 1096 } 1097 b, err := Marshal(v) 1098 if err != nil { 1099 t.Fatalf("Marshal: %v", err) 1100 } 1101 if !bytes.Equal(jsonBig, b) { 1102 t.Errorf("Marshal jsonBig") 1103 diff(t, b, jsonBig) 1104 return 1105 } 1106 } 1107 1108 var numberTests = []struct { 1109 in string 1110 i int64 1111 intErr string 1112 f float64 1113 floatErr string 1114 }{ 1115 {in: "-1.23e1", intErr: "strconv.ParseInt: parsing \"-1.23e1\": invalid syntax", f: -1.23e1}, 1116 {in: "-12", i: -12, f: -12.0}, 1117 {in: "1e1000", intErr: "strconv.ParseInt: parsing \"1e1000\": invalid syntax", floatErr: "strconv.ParseFloat: parsing \"1e1000\": value out of range"}, 1118 } 1119 1120 // Independent of Decode, basic coverage of the accessors in Number 1121 func TestNumberAccessors(t *testing.T) { 1122 for _, tt := range numberTests { 1123 n := Number(tt.in) 1124 if s := n.String(); s != tt.in { 1125 t.Errorf("Number(%q).String() is %q", tt.in, s) 1126 } 1127 if i, err := n.Int64(); err == nil && tt.intErr == "" && i != tt.i { 1128 t.Errorf("Number(%q).Int64() is %d", tt.in, i) 1129 } else if (err == nil && tt.intErr != "") || (err != nil && err.Error() != tt.intErr) { 1130 t.Errorf("Number(%q).Int64() wanted error %q but got: %v", tt.in, tt.intErr, err) 1131 } 1132 if f, err := n.Float64(); err == nil && tt.floatErr == "" && f != tt.f { 1133 t.Errorf("Number(%q).Float64() is %g", tt.in, f) 1134 } else if (err == nil && tt.floatErr != "") || (err != nil && err.Error() != tt.floatErr) { 1135 t.Errorf("Number(%q).Float64() wanted error %q but got: %v", tt.in, tt.floatErr, err) 1136 } 1137 } 1138 } 1139 1140 func TestLargeByteSlice(t *testing.T) { 1141 s0 := make([]byte, 2000) 1142 for i := range s0 { 1143 s0[i] = byte(i) 1144 } 1145 b, err := Marshal(s0) 1146 if err != nil { 1147 t.Fatalf("Marshal: %v", err) 1148 } 1149 var s1 []byte 1150 if err := Unmarshal(b, &s1); err != nil { 1151 t.Fatalf("Unmarshal: %v", err) 1152 } 1153 if !bytes.Equal(s0, s1) { 1154 t.Errorf("Marshal large byte slice") 1155 diff(t, s0, s1) 1156 } 1157 } 1158 1159 type Xint struct { 1160 X int 1161 } 1162 1163 func TestUnmarshalInterface(t *testing.T) { 1164 var xint Xint 1165 var i interface{} = &xint 1166 if err := Unmarshal([]byte(`{"X":1}`), &i); err != nil { 1167 t.Fatalf("Unmarshal: %v", err) 1168 } 1169 if xint.X != 1 { 1170 t.Fatalf("Did not write to xint") 1171 } 1172 } 1173 1174 func TestUnmarshalPtrPtr(t *testing.T) { 1175 var xint Xint 1176 pxint := &xint 1177 if err := Unmarshal([]byte(`{"X":1}`), &pxint); err != nil { 1178 t.Fatalf("Unmarshal: %v", err) 1179 } 1180 if xint.X != 1 { 1181 t.Fatalf("Did not write to xint") 1182 } 1183 } 1184 1185 func TestEscape(t *testing.T) { 1186 const input = `"foobar"<html>` + " [\u2028 \u2029]" 1187 const expected = `"\"foobar\"\u003chtml\u003e [\u2028 \u2029]"` 1188 b, err := Marshal(input) 1189 if err != nil { 1190 t.Fatalf("Marshal error: %v", err) 1191 } 1192 if s := string(b); s != expected { 1193 t.Errorf("Encoding of [%s]:\n got [%s]\nwant [%s]", input, s, expected) 1194 } 1195 } 1196 1197 // WrongString is a struct that's misusing the ,string modifier. 1198 type WrongString struct { 1199 Message string `json:"result,string"` 1200 } 1201 1202 type wrongStringTest struct { 1203 in, err string 1204 } 1205 1206 var wrongStringTests = []wrongStringTest{ 1207 {`{"result":"x"}`, `json: invalid use of ,string struct tag, trying to unmarshal "x" into string`}, 1208 {`{"result":"foo"}`, `json: invalid use of ,string struct tag, trying to unmarshal "foo" into string`}, 1209 {`{"result":"123"}`, `json: invalid use of ,string struct tag, trying to unmarshal "123" into string`}, 1210 {`{"result":123}`, `json: invalid use of ,string struct tag, trying to unmarshal unquoted value into string`}, 1211 } 1212 1213 // If people misuse the ,string modifier, the error message should be 1214 // helpful, telling the user that they're doing it wrong. 1215 func TestErrorMessageFromMisusedString(t *testing.T) { 1216 for n, tt := range wrongStringTests { 1217 r := strings.NewReader(tt.in) 1218 var s WrongString 1219 err := NewDecoder(r).Decode(&s) 1220 got := fmt.Sprintf("%v", err) 1221 if got != tt.err { 1222 t.Errorf("%d. got err = %q, want %q", n, got, tt.err) 1223 } 1224 } 1225 } 1226 1227 func noSpace(c rune) rune { 1228 if isSpace(byte(c)) { //only used for ascii 1229 return -1 1230 } 1231 return c 1232 } 1233 1234 type All struct { 1235 Bool bool 1236 Int int 1237 Int8 int8 1238 Int16 int16 1239 Int32 int32 1240 Int64 int64 1241 Uint uint 1242 Uint8 uint8 1243 Uint16 uint16 1244 Uint32 uint32 1245 Uint64 uint64 1246 Uintptr uintptr 1247 Float32 float32 1248 Float64 float64 1249 1250 Foo string `json:"bar"` 1251 Foo2 string `json:"bar2,dummyopt"` 1252 1253 IntStr int64 `json:",string"` 1254 UintptrStr uintptr `json:",string"` 1255 1256 PBool *bool 1257 PInt *int 1258 PInt8 *int8 1259 PInt16 *int16 1260 PInt32 *int32 1261 PInt64 *int64 1262 PUint *uint 1263 PUint8 *uint8 1264 PUint16 *uint16 1265 PUint32 *uint32 1266 PUint64 *uint64 1267 PUintptr *uintptr 1268 PFloat32 *float32 1269 PFloat64 *float64 1270 1271 String string 1272 PString *string 1273 1274 Map map[string]Small 1275 MapP map[string]*Small 1276 PMap *map[string]Small 1277 PMapP *map[string]*Small 1278 1279 EmptyMap map[string]Small 1280 NilMap map[string]Small 1281 1282 Slice []Small 1283 SliceP []*Small 1284 PSlice *[]Small 1285 PSliceP *[]*Small 1286 1287 EmptySlice []Small 1288 NilSlice []Small 1289 1290 StringSlice []string 1291 ByteSlice []byte 1292 1293 Small Small 1294 PSmall *Small 1295 PPSmall **Small 1296 1297 Interface interface{} 1298 PInterface *interface{} 1299 1300 unexported int 1301 } 1302 1303 type Small struct { 1304 Tag string 1305 } 1306 1307 var allValue = All{ 1308 Bool: true, 1309 Int: 2, 1310 Int8: 3, 1311 Int16: 4, 1312 Int32: 5, 1313 Int64: 6, 1314 Uint: 7, 1315 Uint8: 8, 1316 Uint16: 9, 1317 Uint32: 10, 1318 Uint64: 11, 1319 Uintptr: 12, 1320 Float32: 14.1, 1321 Float64: 15.1, 1322 Foo: "foo", 1323 Foo2: "foo2", 1324 IntStr: 42, 1325 UintptrStr: 44, 1326 String: "16", 1327 Map: map[string]Small{ 1328 "17": {Tag: "tag17"}, 1329 "18": {Tag: "tag18"}, 1330 }, 1331 MapP: map[string]*Small{ 1332 "19": {Tag: "tag19"}, 1333 "20": nil, 1334 }, 1335 EmptyMap: map[string]Small{}, 1336 Slice: []Small{{Tag: "tag20"}, {Tag: "tag21"}}, 1337 SliceP: []*Small{{Tag: "tag22"}, nil, {Tag: "tag23"}}, 1338 EmptySlice: []Small{}, 1339 StringSlice: []string{"str24", "str25", "str26"}, 1340 ByteSlice: []byte{27, 28, 29}, 1341 Small: Small{Tag: "tag30"}, 1342 PSmall: &Small{Tag: "tag31"}, 1343 Interface: 5.2, 1344 } 1345 1346 var pallValue = All{ 1347 PBool: &allValue.Bool, 1348 PInt: &allValue.Int, 1349 PInt8: &allValue.Int8, 1350 PInt16: &allValue.Int16, 1351 PInt32: &allValue.Int32, 1352 PInt64: &allValue.Int64, 1353 PUint: &allValue.Uint, 1354 PUint8: &allValue.Uint8, 1355 PUint16: &allValue.Uint16, 1356 PUint32: &allValue.Uint32, 1357 PUint64: &allValue.Uint64, 1358 PUintptr: &allValue.Uintptr, 1359 PFloat32: &allValue.Float32, 1360 PFloat64: &allValue.Float64, 1361 PString: &allValue.String, 1362 PMap: &allValue.Map, 1363 PMapP: &allValue.MapP, 1364 PSlice: &allValue.Slice, 1365 PSliceP: &allValue.SliceP, 1366 PPSmall: &allValue.PSmall, 1367 PInterface: &allValue.Interface, 1368 } 1369 1370 var allValueIndent = `{ 1371 "Bool": true, 1372 "Int": 2, 1373 "Int8": 3, 1374 "Int16": 4, 1375 "Int32": 5, 1376 "Int64": 6, 1377 "Uint": 7, 1378 "Uint8": 8, 1379 "Uint16": 9, 1380 "Uint32": 10, 1381 "Uint64": 11, 1382 "Uintptr": 12, 1383 "Float32": 14.1, 1384 "Float64": 15.1, 1385 "bar": "foo", 1386 "bar2": "foo2", 1387 "IntStr": "42", 1388 "UintptrStr": "44", 1389 "PBool": null, 1390 "PInt": null, 1391 "PInt8": null, 1392 "PInt16": null, 1393 "PInt32": null, 1394 "PInt64": null, 1395 "PUint": null, 1396 "PUint8": null, 1397 "PUint16": null, 1398 "PUint32": null, 1399 "PUint64": null, 1400 "PUintptr": null, 1401 "PFloat32": null, 1402 "PFloat64": null, 1403 "String": "16", 1404 "PString": null, 1405 "Map": { 1406 "17": { 1407 "Tag": "tag17" 1408 }, 1409 "18": { 1410 "Tag": "tag18" 1411 } 1412 }, 1413 "MapP": { 1414 "19": { 1415 "Tag": "tag19" 1416 }, 1417 "20": null 1418 }, 1419 "PMap": null, 1420 "PMapP": null, 1421 "EmptyMap": {}, 1422 "NilMap": null, 1423 "Slice": [ 1424 { 1425 "Tag": "tag20" 1426 }, 1427 { 1428 "Tag": "tag21" 1429 } 1430 ], 1431 "SliceP": [ 1432 { 1433 "Tag": "tag22" 1434 }, 1435 null, 1436 { 1437 "Tag": "tag23" 1438 } 1439 ], 1440 "PSlice": null, 1441 "PSliceP": null, 1442 "EmptySlice": [], 1443 "NilSlice": null, 1444 "StringSlice": [ 1445 "str24", 1446 "str25", 1447 "str26" 1448 ], 1449 "ByteSlice": "Gxwd", 1450 "Small": { 1451 "Tag": "tag30" 1452 }, 1453 "PSmall": { 1454 "Tag": "tag31" 1455 }, 1456 "PPSmall": null, 1457 "Interface": 5.2, 1458 "PInterface": null 1459 }` 1460 1461 var allValueCompact = strings.Map(noSpace, allValueIndent) 1462 1463 var pallValueIndent = `{ 1464 "Bool": false, 1465 "Int": 0, 1466 "Int8": 0, 1467 "Int16": 0, 1468 "Int32": 0, 1469 "Int64": 0, 1470 "Uint": 0, 1471 "Uint8": 0, 1472 "Uint16": 0, 1473 "Uint32": 0, 1474 "Uint64": 0, 1475 "Uintptr": 0, 1476 "Float32": 0, 1477 "Float64": 0, 1478 "bar": "", 1479 "bar2": "", 1480 "IntStr": "0", 1481 "UintptrStr": "0", 1482 "PBool": true, 1483 "PInt": 2, 1484 "PInt8": 3, 1485 "PInt16": 4, 1486 "PInt32": 5, 1487 "PInt64": 6, 1488 "PUint": 7, 1489 "PUint8": 8, 1490 "PUint16": 9, 1491 "PUint32": 10, 1492 "PUint64": 11, 1493 "PUintptr": 12, 1494 "PFloat32": 14.1, 1495 "PFloat64": 15.1, 1496 "String": "", 1497 "PString": "16", 1498 "Map": null, 1499 "MapP": null, 1500 "PMap": { 1501 "17": { 1502 "Tag": "tag17" 1503 }, 1504 "18": { 1505 "Tag": "tag18" 1506 } 1507 }, 1508 "PMapP": { 1509 "19": { 1510 "Tag": "tag19" 1511 }, 1512 "20": null 1513 }, 1514 "EmptyMap": null, 1515 "NilMap": null, 1516 "Slice": null, 1517 "SliceP": null, 1518 "PSlice": [ 1519 { 1520 "Tag": "tag20" 1521 }, 1522 { 1523 "Tag": "tag21" 1524 } 1525 ], 1526 "PSliceP": [ 1527 { 1528 "Tag": "tag22" 1529 }, 1530 null, 1531 { 1532 "Tag": "tag23" 1533 } 1534 ], 1535 "EmptySlice": null, 1536 "NilSlice": null, 1537 "StringSlice": null, 1538 "ByteSlice": null, 1539 "Small": { 1540 "Tag": "" 1541 }, 1542 "PSmall": null, 1543 "PPSmall": { 1544 "Tag": "tag31" 1545 }, 1546 "Interface": null, 1547 "PInterface": 5.2 1548 }` 1549 1550 var pallValueCompact = strings.Map(noSpace, pallValueIndent) 1551 1552 func TestRefUnmarshal(t *testing.T) { 1553 type S struct { 1554 // Ref is defined in encode_test.go. 1555 R0 Ref 1556 R1 *Ref 1557 R2 RefText 1558 R3 *RefText 1559 } 1560 want := S{ 1561 R0: 12, 1562 R1: new(Ref), 1563 R2: 13, 1564 R3: new(RefText), 1565 } 1566 *want.R1 = 12 1567 *want.R3 = 13 1568 1569 var got S 1570 if err := Unmarshal([]byte(`{"R0":"ref","R1":"ref","R2":"ref","R3":"ref"}`), &got); err != nil { 1571 t.Fatalf("Unmarshal: %v", err) 1572 } 1573 if !reflect.DeepEqual(got, want) { 1574 t.Errorf("got %+v, want %+v", got, want) 1575 } 1576 } 1577 1578 // Test that the empty string doesn't panic decoding when ,string is specified 1579 // Issue 3450 1580 func TestEmptyString(t *testing.T) { 1581 type T2 struct { 1582 Number1 int `json:",string"` 1583 Number2 int `json:",string"` 1584 } 1585 data := `{"Number1":"1", "Number2":""}` 1586 dec := NewDecoder(strings.NewReader(data)) 1587 var t2 T2 1588 err := dec.Decode(&t2) 1589 if err == nil { 1590 t.Fatal("Decode: did not return error") 1591 } 1592 if t2.Number1 != 1 { 1593 t.Fatal("Decode: did not set Number1") 1594 } 1595 } 1596 1597 // Test that a null for ,string is not replaced with the previous quoted string (issue 7046). 1598 // It should also not be an error (issue 2540, issue 8587). 1599 func TestNullString(t *testing.T) { 1600 type T struct { 1601 A int `json:",string"` 1602 B int `json:",string"` 1603 C *int `json:",string"` 1604 } 1605 data := []byte(`{"A": "1", "B": null, "C": null}`) 1606 var s T 1607 s.B = 1 1608 s.C = new(int) 1609 *s.C = 2 1610 err := Unmarshal(data, &s) 1611 if err != nil { 1612 t.Fatalf("Unmarshal: %v", err) 1613 } 1614 if s.B != 1 || s.C != nil { 1615 t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C) 1616 } 1617 } 1618 1619 func intp(x int) *int { 1620 p := new(int) 1621 *p = x 1622 return p 1623 } 1624 1625 func intpp(x *int) **int { 1626 pp := new(*int) 1627 *pp = x 1628 return pp 1629 } 1630 1631 var interfaceSetTests = []struct { 1632 pre interface{} 1633 json string 1634 post interface{} 1635 }{ 1636 {"foo", `"bar"`, "bar"}, 1637 {"foo", `2`, 2.0}, 1638 {"foo", `true`, true}, 1639 {"foo", `null`, nil}, 1640 1641 {nil, `null`, nil}, 1642 {new(int), `null`, nil}, 1643 {(*int)(nil), `null`, nil}, 1644 {new(*int), `null`, new(*int)}, 1645 {(**int)(nil), `null`, nil}, 1646 {intp(1), `null`, nil}, 1647 {intpp(nil), `null`, intpp(nil)}, 1648 {intpp(intp(1)), `null`, intpp(nil)}, 1649 } 1650 1651 func TestInterfaceSet(t *testing.T) { 1652 for _, tt := range interfaceSetTests { 1653 b := struct{ X interface{} }{tt.pre} 1654 blob := `{"X":` + tt.json + `}` 1655 if err := Unmarshal([]byte(blob), &b); err != nil { 1656 t.Errorf("Unmarshal %#q: %v", blob, err) 1657 continue 1658 } 1659 if !reflect.DeepEqual(b.X, tt.post) { 1660 t.Errorf("Unmarshal %#q into %#v: X=%#v, want %#v", blob, tt.pre, b.X, tt.post) 1661 } 1662 } 1663 } 1664 1665 type NullTest struct { 1666 Bool bool 1667 Int int 1668 Int8 int8 1669 Int16 int16 1670 Int32 int32 1671 Int64 int64 1672 Uint uint 1673 Uint8 uint8 1674 Uint16 uint16 1675 Uint32 uint32 1676 Uint64 uint64 1677 Float32 float32 1678 Float64 float64 1679 String string 1680 PBool *bool 1681 Map map[string]string 1682 Slice []string 1683 Interface interface{} 1684 1685 PRaw *RawMessage 1686 PTime *time.Time 1687 PBigInt *big.Int 1688 PText *MustNotUnmarshalText 1689 PBuffer *bytes.Buffer // has methods, just not relevant ones 1690 PStruct *struct{} 1691 1692 Raw RawMessage 1693 Time time.Time 1694 BigInt big.Int 1695 Text MustNotUnmarshalText 1696 Buffer bytes.Buffer 1697 Struct struct{} 1698 } 1699 1700 type NullTestStrings struct { 1701 Bool bool `json:",string"` 1702 Int int `json:",string"` 1703 Int8 int8 `json:",string"` 1704 Int16 int16 `json:",string"` 1705 Int32 int32 `json:",string"` 1706 Int64 int64 `json:",string"` 1707 Uint uint `json:",string"` 1708 Uint8 uint8 `json:",string"` 1709 Uint16 uint16 `json:",string"` 1710 Uint32 uint32 `json:",string"` 1711 Uint64 uint64 `json:",string"` 1712 Float32 float32 `json:",string"` 1713 Float64 float64 `json:",string"` 1714 String string `json:",string"` 1715 PBool *bool `json:",string"` 1716 Map map[string]string `json:",string"` 1717 Slice []string `json:",string"` 1718 Interface interface{} `json:",string"` 1719 1720 PRaw *RawMessage `json:",string"` 1721 PTime *time.Time `json:",string"` 1722 PBigInt *big.Int `json:",string"` 1723 PText *MustNotUnmarshalText `json:",string"` 1724 PBuffer *bytes.Buffer `json:",string"` 1725 PStruct *struct{} `json:",string"` 1726 1727 Raw RawMessage `json:",string"` 1728 Time time.Time `json:",string"` 1729 BigInt big.Int `json:",string"` 1730 Text MustNotUnmarshalText `json:",string"` 1731 Buffer bytes.Buffer `json:",string"` 1732 Struct struct{} `json:",string"` 1733 } 1734 1735 // JSON null values should be ignored for primitives and string values instead of resulting in an error. 1736 // Issue 2540 1737 func TestUnmarshalNulls(t *testing.T) { 1738 // Unmarshal docs: 1739 // The JSON null value unmarshals into an interface, map, pointer, or slice 1740 // by setting that Go value to nil. Because null is often used in JSON to mean 1741 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect 1742 // on the value and produces no error. 1743 1744 jsonData := []byte(`{ 1745 "Bool" : null, 1746 "Int" : null, 1747 "Int8" : null, 1748 "Int16" : null, 1749 "Int32" : null, 1750 "Int64" : null, 1751 "Uint" : null, 1752 "Uint8" : null, 1753 "Uint16" : null, 1754 "Uint32" : null, 1755 "Uint64" : null, 1756 "Float32" : null, 1757 "Float64" : null, 1758 "String" : null, 1759 "PBool": null, 1760 "Map": null, 1761 "Slice": null, 1762 "Interface": null, 1763 "PRaw": null, 1764 "PTime": null, 1765 "PBigInt": null, 1766 "PText": null, 1767 "PBuffer": null, 1768 "PStruct": null, 1769 "Raw": null, 1770 "Time": null, 1771 "BigInt": null, 1772 "Text": null, 1773 "Buffer": null, 1774 "Struct": null 1775 }`) 1776 nulls := NullTest{ 1777 Bool: true, 1778 Int: 2, 1779 Int8: 3, 1780 Int16: 4, 1781 Int32: 5, 1782 Int64: 6, 1783 Uint: 7, 1784 Uint8: 8, 1785 Uint16: 9, 1786 Uint32: 10, 1787 Uint64: 11, 1788 Float32: 12.1, 1789 Float64: 13.1, 1790 String: "14", 1791 PBool: new(bool), 1792 Map: map[string]string{}, 1793 Slice: []string{}, 1794 Interface: new(MustNotUnmarshalJSON), 1795 PRaw: new(RawMessage), 1796 PTime: new(time.Time), 1797 PBigInt: new(big.Int), 1798 PText: new(MustNotUnmarshalText), 1799 PStruct: new(struct{}), 1800 PBuffer: new(bytes.Buffer), 1801 Raw: RawMessage("123"), 1802 Time: time.Unix(123456789, 0), 1803 BigInt: *big.NewInt(123), 1804 } 1805 1806 before := nulls.Time.String() 1807 1808 err := Unmarshal(jsonData, &nulls) 1809 if err != nil { 1810 t.Errorf("Unmarshal of null values failed: %v", err) 1811 } 1812 if !nulls.Bool || nulls.Int != 2 || nulls.Int8 != 3 || nulls.Int16 != 4 || nulls.Int32 != 5 || nulls.Int64 != 6 || 1813 nulls.Uint != 7 || nulls.Uint8 != 8 || nulls.Uint16 != 9 || nulls.Uint32 != 10 || nulls.Uint64 != 11 || 1814 nulls.Float32 != 12.1 || nulls.Float64 != 13.1 || nulls.String != "14" { 1815 t.Errorf("Unmarshal of null values affected primitives") 1816 } 1817 1818 if nulls.PBool != nil { 1819 t.Errorf("Unmarshal of null did not clear nulls.PBool") 1820 } 1821 if nulls.Map != nil { 1822 t.Errorf("Unmarshal of null did not clear nulls.Map") 1823 } 1824 if nulls.Slice != nil { 1825 t.Errorf("Unmarshal of null did not clear nulls.Slice") 1826 } 1827 if nulls.Interface != nil { 1828 t.Errorf("Unmarshal of null did not clear nulls.Interface") 1829 } 1830 if nulls.PRaw != nil { 1831 t.Errorf("Unmarshal of null did not clear nulls.PRaw") 1832 } 1833 if nulls.PTime != nil { 1834 t.Errorf("Unmarshal of null did not clear nulls.PTime") 1835 } 1836 if nulls.PBigInt != nil { 1837 t.Errorf("Unmarshal of null did not clear nulls.PBigInt") 1838 } 1839 if nulls.PText != nil { 1840 t.Errorf("Unmarshal of null did not clear nulls.PText") 1841 } 1842 if nulls.PBuffer != nil { 1843 t.Errorf("Unmarshal of null did not clear nulls.PBuffer") 1844 } 1845 if nulls.PStruct != nil { 1846 t.Errorf("Unmarshal of null did not clear nulls.PStruct") 1847 } 1848 1849 if string(nulls.Raw) != "null" { 1850 t.Errorf("Unmarshal of RawMessage null did not record null: %v", string(nulls.Raw)) 1851 } 1852 if nulls.Time.String() != before { 1853 t.Errorf("Unmarshal of time.Time null set time to %v", nulls.Time.String()) 1854 } 1855 if nulls.BigInt.String() != "123" { 1856 t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String()) 1857 } 1858 } 1859 1860 type MustNotUnmarshalJSON struct{} 1861 1862 func (x MustNotUnmarshalJSON) UnmarshalJSON(data []byte) error { 1863 return errors.New("MustNotUnmarshalJSON was used") 1864 } 1865 1866 type MustNotUnmarshalText struct{} 1867 1868 func (x MustNotUnmarshalText) UnmarshalText(text []byte) error { 1869 return errors.New("MustNotUnmarshalText was used") 1870 } 1871 1872 func TestStringKind(t *testing.T) { 1873 type stringKind string 1874 1875 var m1, m2 map[stringKind]int 1876 m1 = map[stringKind]int{ 1877 "foo": 42, 1878 } 1879 1880 data, err := Marshal(m1) 1881 if err != nil { 1882 t.Errorf("Unexpected error marshaling: %v", err) 1883 } 1884 1885 err = Unmarshal(data, &m2) 1886 if err != nil { 1887 t.Errorf("Unexpected error unmarshaling: %v", err) 1888 } 1889 1890 if !reflect.DeepEqual(m1, m2) { 1891 t.Error("Items should be equal after encoding and then decoding") 1892 } 1893 } 1894 1895 // Custom types with []byte as underlying type could not be marshaled 1896 // and then unmarshaled. 1897 // Issue 8962. 1898 func TestByteKind(t *testing.T) { 1899 type byteKind []byte 1900 1901 a := byteKind("hello") 1902 1903 data, err := Marshal(a) 1904 if err != nil { 1905 t.Error(err) 1906 } 1907 var b byteKind 1908 err = Unmarshal(data, &b) 1909 if err != nil { 1910 t.Fatal(err) 1911 } 1912 if !reflect.DeepEqual(a, b) { 1913 t.Errorf("expected %v == %v", a, b) 1914 } 1915 } 1916 1917 // The fix for issue 8962 introduced a regression. 1918 // Issue 12921. 1919 func TestSliceOfCustomByte(t *testing.T) { 1920 type Uint8 uint8 1921 1922 a := []Uint8("hello") 1923 1924 data, err := Marshal(a) 1925 if err != nil { 1926 t.Fatal(err) 1927 } 1928 var b []Uint8 1929 err = Unmarshal(data, &b) 1930 if err != nil { 1931 t.Fatal(err) 1932 } 1933 if !reflect.DeepEqual(a, b) { 1934 t.Fatalf("expected %v == %v", a, b) 1935 } 1936 } 1937 1938 var decodeTypeErrorTests = []struct { 1939 dest interface{} 1940 src string 1941 }{ 1942 {new(string), `{"user": "name"}`}, // issue 4628. 1943 {new(error), `{}`}, // issue 4222 1944 {new(error), `[]`}, 1945 {new(error), `""`}, 1946 {new(error), `123`}, 1947 {new(error), `true`}, 1948 } 1949 1950 func TestUnmarshalTypeError(t *testing.T) { 1951 for _, item := range decodeTypeErrorTests { 1952 err := Unmarshal([]byte(item.src), item.dest) 1953 if _, ok := err.(*UnmarshalTypeError); !ok { 1954 t.Errorf("expected type error for Unmarshal(%q, type %T): got %T", 1955 item.src, item.dest, err) 1956 } 1957 } 1958 } 1959 1960 var unmarshalSyntaxTests = []string{ 1961 "tru", 1962 "fals", 1963 "nul", 1964 "123e", 1965 `"hello`, 1966 `[1,2,3`, 1967 `{"key":1`, 1968 `{"key":1,`, 1969 } 1970 1971 func TestUnmarshalSyntax(t *testing.T) { 1972 var x interface{} 1973 for _, src := range unmarshalSyntaxTests { 1974 err := Unmarshal([]byte(src), &x) 1975 if _, ok := err.(*SyntaxError); !ok { 1976 t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err) 1977 } 1978 } 1979 } 1980 1981 // Test handling of unexported fields that should be ignored. 1982 // Issue 4660 1983 type unexportedFields struct { 1984 Name string 1985 m map[string]interface{} `json:"-"` 1986 m2 map[string]interface{} `json:"abcd"` 1987 1988 s []int `json:"-"` 1989 } 1990 1991 func TestUnmarshalUnexported(t *testing.T) { 1992 input := `{"Name": "Bob", "m": {"x": 123}, "m2": {"y": 456}, "abcd": {"z": 789}, "s": [2, 3]}` 1993 want := &unexportedFields{Name: "Bob"} 1994 1995 out := &unexportedFields{} 1996 err := Unmarshal([]byte(input), out) 1997 if err != nil { 1998 t.Errorf("got error %v, expected nil", err) 1999 } 2000 if !reflect.DeepEqual(out, want) { 2001 t.Errorf("got %q, want %q", out, want) 2002 } 2003 } 2004 2005 // Time3339 is a time.Time which encodes to and from JSON 2006 // as an RFC 3339 time in UTC. 2007 type Time3339 time.Time 2008 2009 func (t *Time3339) UnmarshalJSON(b []byte) error { 2010 if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' { 2011 return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time", b) 2012 } 2013 tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1])) 2014 if err != nil { 2015 return err 2016 } 2017 *t = Time3339(tm) 2018 return nil 2019 } 2020 2021 func TestUnmarshalJSONLiteralError(t *testing.T) { 2022 var t3 Time3339 2023 err := Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3) 2024 if err == nil { 2025 t.Fatalf("expected error; got time %v", time.Time(t3)) 2026 } 2027 if !strings.Contains(err.Error(), "range") { 2028 t.Errorf("got err = %v; want out of range error", err) 2029 } 2030 } 2031 2032 // Test that extra object elements in an array do not result in a 2033 // "data changing underfoot" error. 2034 // Issue 3717 2035 func TestSkipArrayObjects(t *testing.T) { 2036 json := `[{}]` 2037 var dest [0]interface{} 2038 2039 err := Unmarshal([]byte(json), &dest) 2040 if err != nil { 2041 t.Errorf("got error %q, want nil", err) 2042 } 2043 } 2044 2045 // Test semantics of pre-filled struct fields and pre-filled map fields. 2046 // Issue 4900. 2047 func TestPrefilled(t *testing.T) { 2048 ptrToMap := func(m map[string]interface{}) *map[string]interface{} { return &m } 2049 2050 // Values here change, cannot reuse table across runs. 2051 var prefillTests = []struct { 2052 in string 2053 ptr interface{} 2054 out interface{} 2055 }{ 2056 { 2057 in: `{"X": 1, "Y": 2}`, 2058 ptr: &XYZ{X: float32(3), Y: int16(4), Z: 1.5}, 2059 out: &XYZ{X: float64(1), Y: float64(2), Z: 1.5}, 2060 }, 2061 { 2062 in: `{"X": 1, "Y": 2}`, 2063 ptr: ptrToMap(map[string]interface{}{"X": float32(3), "Y": int16(4), "Z": 1.5}), 2064 out: ptrToMap(map[string]interface{}{"X": float64(1), "Y": float64(2), "Z": 1.5}), 2065 }, 2066 } 2067 2068 for _, tt := range prefillTests { 2069 ptrstr := fmt.Sprintf("%v", tt.ptr) 2070 err := Unmarshal([]byte(tt.in), tt.ptr) // tt.ptr edited here 2071 if err != nil { 2072 t.Errorf("Unmarshal: %v", err) 2073 } 2074 if !reflect.DeepEqual(tt.ptr, tt.out) { 2075 t.Errorf("Unmarshal(%#q, %s): have %v, want %v", tt.in, ptrstr, tt.ptr, tt.out) 2076 } 2077 } 2078 } 2079 2080 var invalidUnmarshalTests = []struct { 2081 v interface{} 2082 want string 2083 }{ 2084 {nil, "json: Unmarshal(nil)"}, 2085 {struct{}{}, "json: Unmarshal(non-pointer struct {})"}, 2086 {(*int)(nil), "json: Unmarshal(nil *int)"}, 2087 } 2088 2089 func TestInvalidUnmarshal(t *testing.T) { 2090 buf := []byte(`{"a":"1"}`) 2091 for _, tt := range invalidUnmarshalTests { 2092 err := Unmarshal(buf, tt.v) 2093 if err == nil { 2094 t.Errorf("Unmarshal expecting error, got nil") 2095 continue 2096 } 2097 if got := err.Error(); got != tt.want { 2098 t.Errorf("Unmarshal = %q; want %q", got, tt.want) 2099 } 2100 } 2101 } 2102 2103 var invalidUnmarshalTextTests = []struct { 2104 v interface{} 2105 want string 2106 }{ 2107 {nil, "json: Unmarshal(nil)"}, 2108 {struct{}{}, "json: Unmarshal(non-pointer struct {})"}, 2109 {(*int)(nil), "json: Unmarshal(nil *int)"}, 2110 {new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"}, 2111 } 2112 2113 func TestInvalidUnmarshalText(t *testing.T) { 2114 buf := []byte(`123`) 2115 for _, tt := range invalidUnmarshalTextTests { 2116 err := Unmarshal(buf, tt.v) 2117 if err == nil { 2118 t.Errorf("Unmarshal expecting error, got nil") 2119 continue 2120 } 2121 if got := err.Error(); got != tt.want { 2122 t.Errorf("Unmarshal = %q; want %q", got, tt.want) 2123 } 2124 } 2125 } 2126 2127 // Test that string option is ignored for invalid types. 2128 // Issue 9812. 2129 func TestInvalidStringOption(t *testing.T) { 2130 num := 0 2131 item := struct { 2132 T time.Time `json:",string"` 2133 M map[string]string `json:",string"` 2134 S []string `json:",string"` 2135 A [1]string `json:",string"` 2136 I interface{} `json:",string"` 2137 P *int `json:",string"` 2138 }{M: make(map[string]string), S: make([]string, 0), I: num, P: &num} 2139 2140 data, err := Marshal(item) 2141 if err != nil { 2142 t.Fatalf("Marshal: %v", err) 2143 } 2144 2145 err = Unmarshal(data, &item) 2146 if err != nil { 2147 t.Fatalf("Unmarshal: %v", err) 2148 } 2149 } 2150 2151 // Test unmarshal behavior with regards to embedded unexported structs. 2152 // 2153 // (Issue 21357) If the embedded struct is a pointer and is unallocated, 2154 // this returns an error because unmarshal cannot set the field. 2155 // 2156 // (Issue 24152) If the embedded struct is given an explicit name, 2157 // ensure that the normal unmarshal logic does not panic in reflect. 2158 // 2159 // (Issue 28145) If the embedded struct is given an explicit name and has 2160 // exported methods, don't cause a panic trying to get its value. 2161 func TestUnmarshalEmbeddedUnexported(t *testing.T) { 2162 type ( 2163 embed1 struct{ Q int } 2164 embed2 struct{ Q int } 2165 embed3 struct { 2166 Q int64 `json:",string"` 2167 } 2168 S1 struct { 2169 *embed1 2170 R int 2171 } 2172 S2 struct { 2173 *embed1 2174 Q int 2175 } 2176 S3 struct { 2177 embed1 2178 R int 2179 } 2180 S4 struct { 2181 *embed1 2182 embed2 2183 } 2184 S5 struct { 2185 *embed3 2186 R int 2187 } 2188 S6 struct { 2189 embed1 `json:"embed1"` 2190 } 2191 S7 struct { 2192 embed1 `json:"embed1"` 2193 embed2 2194 } 2195 S8 struct { 2196 embed1 `json:"embed1"` 2197 embed2 `json:"embed2"` 2198 Q int 2199 } 2200 S9 struct { 2201 unexportedWithMethods `json:"embed"` 2202 } 2203 ) 2204 2205 tests := []struct { 2206 in string 2207 ptr interface{} 2208 out interface{} 2209 err error 2210 }{{ 2211 // Error since we cannot set S1.embed1, but still able to set S1.R. 2212 in: `{"R":2,"Q":1}`, 2213 ptr: new(S1), 2214 out: &S1{R: 2}, 2215 err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json.embed1"), 2216 }, { 2217 // The top level Q field takes precedence. 2218 in: `{"Q":1}`, 2219 ptr: new(S2), 2220 out: &S2{Q: 1}, 2221 }, { 2222 // No issue with non-pointer variant. 2223 in: `{"R":2,"Q":1}`, 2224 ptr: new(S3), 2225 out: &S3{embed1: embed1{Q: 1}, R: 2}, 2226 }, { 2227 // No error since both embedded structs have field R, which annihilate each other. 2228 // Thus, no attempt is made at setting S4.embed1. 2229 in: `{"R":2}`, 2230 ptr: new(S4), 2231 out: new(S4), 2232 }, { 2233 // Error since we cannot set S5.embed1, but still able to set S5.R. 2234 in: `{"R":2,"Q":1}`, 2235 ptr: new(S5), 2236 out: &S5{R: 2}, 2237 err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: json.embed3"), 2238 }, { 2239 // Issue 24152, ensure decodeState.indirect does not panic. 2240 in: `{"embed1": {"Q": 1}}`, 2241 ptr: new(S6), 2242 out: &S6{embed1{1}}, 2243 }, { 2244 // Issue 24153, check that we can still set forwarded fields even in 2245 // the presence of a name conflict. 2246 // 2247 // This relies on obscure behavior of reflect where it is possible 2248 // to set a forwarded exported field on an unexported embedded struct 2249 // even though there is a name conflict, even when it would have been 2250 // impossible to do so according to Go visibility rules. 2251 // Go forbids this because it is ambiguous whether S7.Q refers to 2252 // S7.embed1.Q or S7.embed2.Q. Since embed1 and embed2 are unexported, 2253 // it should be impossible for an external package to set either Q. 2254 // 2255 // It is probably okay for a future reflect change to break this. 2256 in: `{"embed1": {"Q": 1}, "Q": 2}`, 2257 ptr: new(S7), 2258 out: &S7{embed1{1}, embed2{2}}, 2259 }, { 2260 // Issue 24153, similar to the S7 case. 2261 in: `{"embed1": {"Q": 1}, "embed2": {"Q": 2}, "Q": 3}`, 2262 ptr: new(S8), 2263 out: &S8{embed1{1}, embed2{2}, 3}, 2264 }, { 2265 // Issue 228145, similar to the cases above. 2266 in: `{"embed": {}}`, 2267 ptr: new(S9), 2268 out: &S9{}, 2269 }} 2270 2271 for i, tt := range tests { 2272 err := Unmarshal([]byte(tt.in), tt.ptr) 2273 if !reflect.DeepEqual(err, tt.err) { 2274 t.Errorf("#%d: %v, want %v", i, err, tt.err) 2275 } 2276 if !reflect.DeepEqual(tt.ptr, tt.out) { 2277 t.Errorf("#%d: mismatch\ngot: %#+v\nwant: %#+v", i, tt.ptr, tt.out) 2278 } 2279 } 2280 } 2281 2282 type unmarshalPanic struct{} 2283 2284 func (unmarshalPanic) UnmarshalJSON([]byte) error { panic(0xdead) } 2285 2286 func TestUnmarshalPanic(t *testing.T) { 2287 defer func() { 2288 if got := recover(); !reflect.DeepEqual(got, 0xdead) { 2289 t.Errorf("panic() = (%T)(%v), want 0xdead", got, got) 2290 } 2291 }() 2292 Unmarshal([]byte("{}"), &unmarshalPanic{}) 2293 t.Fatalf("Unmarshal should have panicked") 2294 }