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