github.com/neilotoole/jsoncolor@v0.7.2-0.20231115150201-1637fae69be1/golang_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 jsoncolor 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 //TODO 429 //{in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"}, 430 {in: "null", ptr: new(interface{}), out: nil}, 431 {in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &UnmarshalTypeError{"array", reflect.TypeOf(""), 7, "T", "X"}}, 432 {in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, 433 {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, 434 {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, 435 {in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true}, 436 {in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(SS("")), 0, "W", "S"}}, 437 {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}}, 438 {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true}, 439 {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64}, 440 {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsNumber, useNumber: true}, 441 442 // raw values with whitespace 443 {in: "\n true ", ptr: new(bool), out: true}, 444 {in: "\t 1 ", ptr: new(int), out: 1}, 445 {in: "\r 1.2 ", ptr: new(float64), out: 1.2}, 446 {in: "\t -5 \n", ptr: new(int16), out: int16(-5)}, 447 {in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"}, 448 449 // Z has a "-" tag. 450 {in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}}, 451 {in: `{"Y": 1, "Z": 2}`, ptr: new(T), err: fmt.Errorf("json: unknown field \"Z\""), disallowUnknownFields: true}, 452 453 {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}}, 454 {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, 455 {in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}}, 456 {in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}}, 457 {in: `{"alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true}, 458 459 // syntax errors 460 {in: `{"X": "foo", "Y"}`, err: &testSyntaxError{"invalid character '}' after object key", 17}}, 461 {in: `[1, 2, 3+]`, err: &testSyntaxError{"invalid character '+' after array element", 9}}, 462 {in: `{"X":12x}`, err: &testSyntaxError{"invalid character 'x' after object key:value pair", 8}, useNumber: true}, 463 {in: `[2, 3`, err: &testSyntaxError{msg: "unexpected end of JSON input", Offset: 5}}, 464 465 // raw value errors 466 {in: "\x01 42", err: &testSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 467 {in: " 42 \x01", err: &testSyntaxError{"invalid character '\\x01' after top-level value", 5}}, 468 {in: "\x01 true", err: &testSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 469 {in: " false \x01", err: &testSyntaxError{"invalid character '\\x01' after top-level value", 8}}, 470 {in: "\x01 1.2", err: &testSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 471 {in: " 3.4 \x01", err: &testSyntaxError{"invalid character '\\x01' after top-level value", 6}}, 472 {in: "\x01 \"string\"", err: &testSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}}, 473 {in: " \"string\" \x01", err: &testSyntaxError{"invalid character '\\x01' after top-level value", 11}}, 474 475 // array tests 476 {in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}}, 477 {in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}}, 478 {in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}}, 479 {in: `[1, 2, 3]`, ptr: new(MustNotUnmarshalJSON), err: errors.New("MustNotUnmarshalJSON was used")}, 480 481 // empty array to interface test 482 {in: `[]`, ptr: new([]interface{}), out: []interface{}{}}, 483 {in: `null`, ptr: new([]interface{}), out: []interface{}(nil)}, 484 {in: `{"T":[]}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}}, 485 {in: `{"T":null}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": interface{}(nil)}}, 486 487 // composite tests 488 {in: allValueIndent, ptr: new(All), out: allValue}, 489 {in: allValueCompact, ptr: new(All), out: allValue}, 490 {in: allValueIndent, ptr: new(*All), out: &allValue}, 491 {in: allValueCompact, ptr: new(*All), out: &allValue}, 492 {in: pallValueIndent, ptr: new(All), out: pallValue}, 493 {in: pallValueCompact, ptr: new(All), out: pallValue}, 494 {in: pallValueIndent, ptr: new(*All), out: &pallValue}, 495 {in: pallValueCompact, ptr: new(*All), out: &pallValue}, 496 497 // unmarshal interface test 498 {in: `{"T":false}`, ptr: &um0, out: umtrue}, // use "false" so test will fail if custom unmarshaler is not called 499 {in: `{"T":false}`, ptr: &ump, out: &umtrue}, 500 {in: `[{"T":false}]`, ptr: &umslice, out: umslice}, 501 {in: `[{"T":false}]`, ptr: &umslicep, out: &umslice}, 502 {in: `{"M":{"T":"x:y"}}`, ptr: &umstruct, out: umstruct}, 503 504 // UnmarshalText interface test 505 {in: `"x:y"`, ptr: &um0T, out: umtrueXY}, 506 {in: `"x:y"`, ptr: &umpType, out: &umtrueXY}, 507 {in: `["x:y"]`, ptr: &umsliceXY, out: umsliceXY}, 508 {in: `["x:y"]`, ptr: &umslicepType, out: &umsliceXY}, 509 {in: `{"M":"x:y"}`, ptr: umstructType, out: umstructXY}, 510 511 // integer-keyed map test 512 { 513 in: `{"-1":"a","0":"b","1":"c"}`, 514 ptr: new(map[int]string), 515 out: map[int]string{-1: "a", 0: "b", 1: "c"}, 516 }, 517 { 518 in: `{"0":"a","10":"c","9":"b"}`, 519 ptr: new(map[u8]string), 520 out: map[u8]string{0: "a", 9: "b", 10: "c"}, 521 }, 522 { 523 in: `{"-9223372036854775808":"min","9223372036854775807":"max"}`, 524 ptr: new(map[int64]string), 525 out: map[int64]string{math.MinInt64: "min", math.MaxInt64: "max"}, 526 }, 527 { 528 in: `{"18446744073709551615":"max"}`, 529 ptr: new(map[uint64]string), 530 out: map[uint64]string{math.MaxUint64: "max"}, 531 }, 532 { 533 in: `{"0":false,"10":true}`, 534 ptr: new(map[uintptr]bool), 535 out: map[uintptr]bool{0: false, 10: true}, 536 }, 537 538 // Check that MarshalText and UnmarshalText take precedence 539 // over default integer handling in map keys. 540 { 541 in: `{"u2":4}`, 542 ptr: new(map[u8marshal]int), 543 out: map[u8marshal]int{2: 4}, 544 }, 545 { 546 in: `{"2":4}`, 547 ptr: new(map[u8marshal]int), 548 err: errMissingU8Prefix, 549 }, 550 551 // integer-keyed map errors 552 { 553 in: `{"abc":"abc"}`, 554 ptr: new(map[int]string), 555 err: &UnmarshalTypeError{Value: "number abc", Type: reflect.TypeOf(0), Offset: 2}, 556 }, 557 { 558 in: `{"256":"abc"}`, 559 ptr: new(map[uint8]string), 560 err: &UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2}, 561 }, 562 { 563 in: `{"128":"abc"}`, 564 ptr: new(map[int8]string), 565 err: &UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2}, 566 }, 567 { 568 in: `{"-1":"abc"}`, 569 ptr: new(map[uint8]string), 570 err: &UnmarshalTypeError{Value: "number -1", Type: reflect.TypeOf(uint8(0)), Offset: 2}, 571 }, 572 { 573 in: `{"F":{"a":2,"3":4}}`, 574 ptr: new(map[string]map[int]int), 575 err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(int(0)), Offset: 7}, 576 }, 577 { 578 in: `{"F":{"a":2,"3":4}}`, 579 ptr: new(map[string]map[uint]int), 580 err: &UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(uint(0)), Offset: 7}, 581 }, 582 583 // Map keys can be encoding.TextUnmarshalers. 584 {in: `{"x:y":true}`, ptr: &ummapType, out: ummapXY}, 585 // If multiple values for the same key exists, only the most recent value is used. 586 {in: `{"x:y":false,"x:y":true}`, ptr: &ummapType, out: ummapXY}, 587 588 // Overwriting of data. 589 // This is different from package xml, but it's what we've always done. 590 // Now documented and tested. 591 {in: `[2]`, ptr: sliceAddr([]int{1}), out: []int{2}}, 592 {in: `{"key": 2}`, ptr: mapAddr(map[string]int{"old": 0, "key": 1}), out: map[string]int{"key": 2}}, 593 594 { 595 in: `{ 596 "Level0": 1, 597 "Level1b": 2, 598 "Level1c": 3, 599 "x": 4, 600 "Level1a": 5, 601 "LEVEL1B": 6, 602 "e": { 603 "Level1a": 8, 604 "Level1b": 9, 605 "Level1c": 10, 606 "Level1d": 11, 607 "x": 12 608 }, 609 "Loop1": 13, 610 "Loop2": 14, 611 "X": 15, 612 "Y": 16, 613 "Z": 17, 614 "Q": 18 615 }`, 616 ptr: new(Top), 617 out: Top{ 618 Level0: 1, 619 Embed0: Embed0{ 620 Level1b: 2, 621 Level1c: 3, 622 }, 623 Embed0a: &Embed0a{ 624 Level1a: 5, 625 Level1b: 6, 626 }, 627 Embed0b: &Embed0b{ 628 Level1a: 8, 629 Level1b: 9, 630 Level1c: 10, 631 Level1d: 11, 632 Level1e: 12, 633 }, 634 Loop: Loop{ 635 Loop1: 13, 636 Loop2: 14, 637 }, 638 Embed0p: Embed0p{ 639 Point: image.Point{X: 15, Y: 16}, 640 }, 641 Embed0q: Embed0q{ 642 Point: Point{Z: 17}, 643 }, 644 embed: embed{ 645 Q: 18, 646 }, 647 }, 648 }, 649 { 650 in: `{"hello": 1}`, 651 ptr: new(Ambig), 652 out: Ambig{First: 1}, 653 }, 654 { 655 in: `{"X": 1,"Y":2}`, 656 ptr: new(S5), 657 out: S5{S8: S8{S9: S9{Y: 2}}}, 658 }, 659 { 660 in: `{"X": 1,"Y":2}`, 661 ptr: new(S5), 662 err: fmt.Errorf("json: unknown field \"X\""), 663 disallowUnknownFields: true, 664 }, 665 { 666 in: `{"X": 1,"Y":2}`, 667 ptr: new(S10), 668 out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}}, 669 }, 670 { 671 in: `{"X": 1,"Y":2}`, 672 ptr: new(S10), 673 err: fmt.Errorf("json: unknown field \"X\""), 674 disallowUnknownFields: true, 675 }, 676 677 // invalid UTF-8 is coerced to valid UTF-8. 678 { 679 in: "\"hello\xffworld\"", 680 ptr: new(string), 681 out: "hello\ufffdworld", 682 }, 683 { 684 in: "\"hello\xc2\xc2world\"", 685 ptr: new(string), 686 out: "hello\ufffd\ufffdworld", 687 }, 688 { 689 in: "\"hello\xc2\xffworld\"", 690 ptr: new(string), 691 out: "hello\ufffd\ufffdworld", 692 }, 693 { 694 in: "\"hello\\ud800world\"", 695 ptr: new(string), 696 out: "hello\ufffdworld", 697 }, 698 { 699 in: "\"hello\\ud800\\ud800world\"", 700 ptr: new(string), 701 out: "hello\ufffd\ufffdworld", 702 }, 703 { 704 in: "\"hello\\ud800\\ud800world\"", 705 ptr: new(string), 706 out: "hello\ufffd\ufffdworld", 707 }, 708 { 709 in: "\"hello\xed\xa0\x80\xed\xb0\x80world\"", 710 ptr: new(string), 711 out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld", 712 }, 713 714 // Used to be issue 8305, but time.Time implements encoding.TextUnmarshaler so this works now. 715 { 716 in: `{"2009-11-10T23:00:00Z": "hello world"}`, 717 ptr: &map[time.Time]string{}, 718 out: map[time.Time]string{time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC): "hello world"}, 719 }, 720 721 // issue 8305 722 { 723 in: `{"2009-11-10T23:00:00Z": "hello world"}`, 724 ptr: &map[Point]string{}, 725 err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[Point]string{}), Offset: 1}, 726 }, 727 { 728 in: `{"asdf": "hello world"}`, 729 ptr: &map[unmarshaler]string{}, 730 err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[unmarshaler]string{}), Offset: 1}, 731 }, 732 733 // related to issue 13783. 734 // Go 1.7 changed marshaling a slice of typed byte to use the methods on the byte type, 735 // similar to marshaling a slice of typed int. 736 // These tests check that, assuming the byte type also has valid decoding methods, 737 // either the old base64 string encoding or the new per-element encoding can be 738 // successfully unmarshaled. The custom unmarshalers were accessible in earlier 739 // versions of Go, even though the custom marshaler was not. 740 { 741 in: `"AQID"`, 742 ptr: new([]byteWithMarshalJSON), 743 out: []byteWithMarshalJSON{1, 2, 3}, 744 }, 745 { 746 in: `["Z01","Z02","Z03"]`, 747 ptr: new([]byteWithMarshalJSON), 748 out: []byteWithMarshalJSON{1, 2, 3}, 749 golden: true, 750 }, 751 { 752 in: `"AQID"`, 753 ptr: new([]byteWithMarshalText), 754 out: []byteWithMarshalText{1, 2, 3}, 755 }, 756 { 757 in: `["Z01","Z02","Z03"]`, 758 ptr: new([]byteWithMarshalText), 759 out: []byteWithMarshalText{1, 2, 3}, 760 golden: true, 761 }, 762 { 763 in: `"AQID"`, 764 ptr: new([]byteWithPtrMarshalJSON), 765 out: []byteWithPtrMarshalJSON{1, 2, 3}, 766 }, 767 { 768 in: `["Z01","Z02","Z03"]`, 769 ptr: new([]byteWithPtrMarshalJSON), 770 out: []byteWithPtrMarshalJSON{1, 2, 3}, 771 golden: true, 772 }, 773 { 774 in: `"AQID"`, 775 ptr: new([]byteWithPtrMarshalText), 776 out: []byteWithPtrMarshalText{1, 2, 3}, 777 }, 778 { 779 in: `["Z01","Z02","Z03"]`, 780 ptr: new([]byteWithPtrMarshalText), 781 out: []byteWithPtrMarshalText{1, 2, 3}, 782 golden: true, 783 }, 784 785 // ints work with the marshaler but not the base64 []byte case 786 { 787 in: `["Z01","Z02","Z03"]`, 788 ptr: new([]intWithMarshalJSON), 789 out: []intWithMarshalJSON{1, 2, 3}, 790 golden: true, 791 }, 792 { 793 in: `["Z01","Z02","Z03"]`, 794 ptr: new([]intWithMarshalText), 795 out: []intWithMarshalText{1, 2, 3}, 796 golden: true, 797 }, 798 { 799 in: `["Z01","Z02","Z03"]`, 800 ptr: new([]intWithPtrMarshalJSON), 801 out: []intWithPtrMarshalJSON{1, 2, 3}, 802 golden: true, 803 }, 804 { 805 in: `["Z01","Z02","Z03"]`, 806 ptr: new([]intWithPtrMarshalText), 807 out: []intWithPtrMarshalText{1, 2, 3}, 808 golden: true, 809 }, 810 811 {in: `0.000001`, ptr: new(float64), out: 0.000001, golden: true}, 812 {in: `1e-7`, ptr: new(float64), out: 1e-7, golden: true}, 813 {in: `100000000000000000000`, ptr: new(float64), out: 100000000000000000000.0, golden: true}, 814 {in: `1e+21`, ptr: new(float64), out: 1e21, golden: true}, 815 {in: `-0.000001`, ptr: new(float64), out: -0.000001, golden: true}, 816 {in: `-1e-7`, ptr: new(float64), out: -1e-7, golden: true}, 817 {in: `-100000000000000000000`, ptr: new(float64), out: -100000000000000000000.0, golden: true}, 818 {in: `-1e+21`, ptr: new(float64), out: -1e21, golden: true}, 819 {in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true}, 820 {in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true}, 821 {in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false}, 822 823 { 824 in: `{"V": {"F2": "hello"}}`, 825 ptr: new(VOuter), 826 err: &UnmarshalTypeError{ 827 Value: "string", 828 Struct: "V", 829 Field: "V.F2", 830 Type: reflect.TypeOf(int32(0)), 831 Offset: 20, 832 }, 833 }, 834 { 835 in: `{"V": {"F4": {}, "F2": "hello"}}`, 836 ptr: new(VOuter), 837 err: &UnmarshalTypeError{ 838 Value: "string", 839 Struct: "V", 840 Field: "V.F2", 841 Type: reflect.TypeOf(int32(0)), 842 Offset: 30, 843 }, 844 }, 845 846 // issue 15146. 847 // invalid inputs in wrongStringTests below. 848 {in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true}, 849 {in: `{"B":"false"}`, ptr: new(B), out: B{false}, golden: true}, 850 {in: `{"B": "maybe"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "maybe" into bool`)}, 851 {in: `{"B": "tru"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "tru" into bool`)}, 852 {in: `{"B": "False"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "False" into bool`)}, 853 {in: `{"B": "null"}`, ptr: new(B), out: B{false}}, 854 {in: `{"B": "nul"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "nul" into bool`)}, 855 {in: `{"B": [2, 3]}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal unquoted value into bool`)}, 856 857 // additional tests for disallowUnknownFields 858 { 859 in: `{ 860 "Level0": 1, 861 "Level1b": 2, 862 "Level1c": 3, 863 "x": 4, 864 "Level1a": 5, 865 "LEVEL1B": 6, 866 "e": { 867 "Level1a": 8, 868 "Level1b": 9, 869 "Level1c": 10, 870 "Level1d": 11, 871 "x": 12 872 }, 873 "Loop1": 13, 874 "Loop2": 14, 875 "X": 15, 876 "Y": 16, 877 "Z": 17, 878 "Q": 18, 879 "extra": true 880 }`, 881 ptr: new(Top), 882 err: fmt.Errorf("json: unknown field \"extra\""), 883 disallowUnknownFields: true, 884 }, 885 { 886 in: `{ 887 "Level0": 1, 888 "Level1b": 2, 889 "Level1c": 3, 890 "x": 4, 891 "Level1a": 5, 892 "LEVEL1B": 6, 893 "e": { 894 "Level1a": 8, 895 "Level1b": 9, 896 "Level1c": 10, 897 "Level1d": 11, 898 "x": 12, 899 "extra": null 900 }, 901 "Loop1": 13, 902 "Loop2": 14, 903 "X": 15, 904 "Y": 16, 905 "Z": 17, 906 "Q": 18 907 }`, 908 ptr: new(Top), 909 err: fmt.Errorf("json: unknown field \"extra\""), 910 disallowUnknownFields: true, 911 }, 912 // issue 26444 913 // UnmarshalTypeError without field & struct values 914 { 915 in: `{"data":{"test1": "bob", "test2": 123}}`, 916 ptr: new(mapStringToStringData), 917 err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 37, Struct: "mapStringToStringData", Field: "data"}, 918 }, 919 { 920 in: `{"data":{"test1": 123, "test2": "bob"}}`, 921 ptr: new(mapStringToStringData), 922 err: &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 21, Struct: "mapStringToStringData", Field: "data"}, 923 }, 924 925 // trying to decode JSON arrays or objects via TextUnmarshaler 926 { 927 in: `[1, 2, 3]`, 928 ptr: new(MustNotUnmarshalText), 929 err: &UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1}, 930 }, 931 { 932 in: `{"foo": "bar"}`, 933 ptr: new(MustNotUnmarshalText), 934 err: &UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1}, 935 }, 936 // #22369 937 { 938 in: `{"PP": {"T": {"Y": "bad-type"}}}`, 939 ptr: new(P), 940 err: &UnmarshalTypeError{ 941 Value: "string", 942 Struct: "T", 943 Field: "PP.T.Y", 944 Type: reflect.TypeOf(int(0)), 945 Offset: 29, 946 }, 947 }, 948 { 949 in: `{"Ts": [{"Y": 1}, {"Y": 2}, {"Y": "bad-type"}]}`, 950 ptr: new(PP), 951 err: &UnmarshalTypeError{ 952 Value: "string", 953 Struct: "T", 954 Field: "Ts.Y", 955 Type: reflect.TypeOf(int(0)), 956 Offset: 29, 957 }, 958 }, 959 } 960 961 func TestMarshal(t *testing.T) { 962 b, err := Marshal(allValue) 963 if err != nil { 964 t.Fatalf("Marshal allValue: %v", err) 965 } 966 if string(b) != allValueCompact { 967 t.Errorf("Marshal allValueCompact") 968 diff(t, b, []byte(allValueCompact)) 969 return 970 } 971 972 b, err = Marshal(pallValue) 973 if err != nil { 974 t.Fatalf("Marshal pallValue: %v", err) 975 } 976 if string(b) != pallValueCompact { 977 t.Errorf("Marshal pallValueCompact") 978 diff(t, b, []byte(pallValueCompact)) 979 return 980 } 981 } 982 983 var badUTF8 = []struct { 984 in, out string 985 }{ 986 {"hello\xffworld", `"hello\ufffdworld"`}, 987 {"", `""`}, 988 {"\xff", `"\ufffd"`}, 989 {"\xff\xff", `"\ufffd\ufffd"`}, 990 {"a\xffb", `"a\ufffdb"`}, 991 {"\xe6\x97\xa5\xe6\x9c\xac\xff\xaa\x9e", `"日本\ufffd\ufffd\ufffd"`}, 992 } 993 994 func TestMarshalBadUTF8(t *testing.T) { 995 for _, tt := range badUTF8 { 996 b, err := Marshal(tt.in) 997 if string(b) != tt.out || err != nil { 998 t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", tt.in, b, err, tt.out) 999 } 1000 } 1001 } 1002 1003 func TestMarshalNumberZeroVal(t *testing.T) { 1004 var n Number 1005 out, err := Marshal(n) 1006 if err != nil { 1007 t.Fatal(err) 1008 } 1009 outStr := string(out) 1010 if outStr != "0" { 1011 t.Fatalf("Invalid zero val for Number: %q", outStr) 1012 } 1013 } 1014 1015 func TestMarshalEmbeds(t *testing.T) { 1016 top := &Top{ 1017 Level0: 1, 1018 Embed0: Embed0{ 1019 Level1b: 2, 1020 Level1c: 3, 1021 }, 1022 Embed0a: &Embed0a{ 1023 Level1a: 5, 1024 Level1b: 6, 1025 }, 1026 Embed0b: &Embed0b{ 1027 Level1a: 8, 1028 Level1b: 9, 1029 Level1c: 10, 1030 Level1d: 11, 1031 Level1e: 12, 1032 }, 1033 Loop: Loop{ 1034 Loop1: 13, 1035 Loop2: 14, 1036 }, 1037 Embed0p: Embed0p{ 1038 Point: image.Point{X: 15, Y: 16}, 1039 }, 1040 Embed0q: Embed0q{ 1041 Point: Point{Z: 17}, 1042 }, 1043 embed: embed{ 1044 Q: 18, 1045 }, 1046 } 1047 b, err := Marshal(top) 1048 if err != nil { 1049 t.Fatal(err) 1050 } 1051 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}" 1052 if string(b) != want { 1053 t.Errorf("Wrong marshal result.\n got: %q\nwant: %q", b, want) 1054 } 1055 } 1056 1057 func equalError(a, b error) bool { 1058 if a == nil { 1059 return b == nil 1060 } 1061 if b == nil { 1062 return a == nil 1063 } 1064 return a.Error() == b.Error() 1065 } 1066 1067 func TestUnmarshal(t *testing.T) { 1068 for i, tt := range unmarshalTests { 1069 var scan scanner 1070 in := []byte(tt.in) 1071 if err := checkValid(in, &scan); err != nil { 1072 if !equalError(err, tt.err) { 1073 t.Errorf("#%d: checkValid: %#v", i, err) 1074 continue 1075 } 1076 } 1077 if tt.ptr == nil { 1078 continue 1079 } 1080 1081 // v = new(right-type) 1082 v := reflect.New(reflect.TypeOf(tt.ptr).Elem()) 1083 dec := NewDecoder(bytes.NewReader(in)) 1084 if tt.useNumber { 1085 dec.UseNumber() 1086 } 1087 if tt.disallowUnknownFields { 1088 dec.DisallowUnknownFields() 1089 } 1090 err := dec.Decode(v.Interface()) 1091 assertErrorPresence(t, tt.err, err, "testUnmarshal", i, tt.in) 1092 if err != nil { 1093 continue 1094 } 1095 if !reflect.DeepEqual(v.Elem().Interface(), tt.out) { 1096 t.Errorf("#%d: mismatch\n%v\nhave: %#+v\nwant: %#+v", i, tt.in, 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 assertErrorPresence(t, errors.New(tt.err), err, n) 1265 } 1266 } 1267 1268 func noSpace(c rune) rune { 1269 if isSpace(byte(c)) { // only used for ascii 1270 return -1 1271 } 1272 return c 1273 } 1274 1275 type All struct { 1276 Bool bool 1277 Int int 1278 Int8 int8 1279 Int16 int16 1280 Int32 int32 1281 Int64 int64 1282 Uint uint 1283 Uint8 uint8 1284 Uint16 uint16 1285 Uint32 uint32 1286 Uint64 uint64 1287 Uintptr uintptr 1288 Float32 float32 1289 Float64 float64 1290 1291 Foo string `json:"bar"` 1292 Foo2 string `json:"bar2,dummyopt"` 1293 1294 IntStr int64 `json:",string"` 1295 UintptrStr uintptr `json:",string"` 1296 1297 PBool *bool 1298 PInt *int 1299 PInt8 *int8 1300 PInt16 *int16 1301 PInt32 *int32 1302 PInt64 *int64 1303 PUint *uint 1304 PUint8 *uint8 1305 PUint16 *uint16 1306 PUint32 *uint32 1307 PUint64 *uint64 1308 PUintptr *uintptr 1309 PFloat32 *float32 1310 PFloat64 *float64 1311 1312 String string 1313 PString *string 1314 1315 Map map[string]Small 1316 MapP map[string]*Small 1317 PMap *map[string]Small 1318 PMapP *map[string]*Small 1319 1320 EmptyMap map[string]Small 1321 NilMap map[string]Small 1322 1323 Slice []Small 1324 SliceP []*Small 1325 PSlice *[]Small 1326 PSliceP *[]*Small 1327 1328 EmptySlice []Small 1329 NilSlice []Small 1330 1331 StringSlice []string 1332 ByteSlice []byte 1333 1334 Small Small 1335 PSmall *Small 1336 PPSmall **Small 1337 1338 Interface interface{} 1339 PInterface *interface{} 1340 1341 unexported int 1342 } 1343 1344 type Small struct { 1345 Tag string 1346 } 1347 1348 var allValue = All{ 1349 Bool: true, 1350 Int: 2, 1351 Int8: 3, 1352 Int16: 4, 1353 Int32: 5, 1354 Int64: 6, 1355 Uint: 7, 1356 Uint8: 8, 1357 Uint16: 9, 1358 Uint32: 10, 1359 Uint64: 11, 1360 Uintptr: 12, 1361 Float32: 14.1, 1362 Float64: 15.1, 1363 Foo: "foo", 1364 Foo2: "foo2", 1365 IntStr: 42, 1366 UintptrStr: 44, 1367 String: "16", 1368 Map: map[string]Small{ 1369 "17": {Tag: "tag17"}, 1370 "18": {Tag: "tag18"}, 1371 }, 1372 MapP: map[string]*Small{ 1373 "19": {Tag: "tag19"}, 1374 "20": nil, 1375 }, 1376 EmptyMap: map[string]Small{}, 1377 Slice: []Small{{Tag: "tag20"}, {Tag: "tag21"}}, 1378 SliceP: []*Small{{Tag: "tag22"}, nil, {Tag: "tag23"}}, 1379 EmptySlice: []Small{}, 1380 StringSlice: []string{"str24", "str25", "str26"}, 1381 ByteSlice: []byte{27, 28, 29}, 1382 Small: Small{Tag: "tag30"}, 1383 PSmall: &Small{Tag: "tag31"}, 1384 Interface: 5.2, 1385 } 1386 1387 var pallValue = All{ 1388 PBool: &allValue.Bool, 1389 PInt: &allValue.Int, 1390 PInt8: &allValue.Int8, 1391 PInt16: &allValue.Int16, 1392 PInt32: &allValue.Int32, 1393 PInt64: &allValue.Int64, 1394 PUint: &allValue.Uint, 1395 PUint8: &allValue.Uint8, 1396 PUint16: &allValue.Uint16, 1397 PUint32: &allValue.Uint32, 1398 PUint64: &allValue.Uint64, 1399 PUintptr: &allValue.Uintptr, 1400 PFloat32: &allValue.Float32, 1401 PFloat64: &allValue.Float64, 1402 PString: &allValue.String, 1403 PMap: &allValue.Map, 1404 PMapP: &allValue.MapP, 1405 PSlice: &allValue.Slice, 1406 PSliceP: &allValue.SliceP, 1407 PPSmall: &allValue.PSmall, 1408 PInterface: &allValue.Interface, 1409 } 1410 1411 var allValueIndent = `{ 1412 "Bool": true, 1413 "Int": 2, 1414 "Int8": 3, 1415 "Int16": 4, 1416 "Int32": 5, 1417 "Int64": 6, 1418 "Uint": 7, 1419 "Uint8": 8, 1420 "Uint16": 9, 1421 "Uint32": 10, 1422 "Uint64": 11, 1423 "Uintptr": 12, 1424 "Float32": 14.1, 1425 "Float64": 15.1, 1426 "bar": "foo", 1427 "bar2": "foo2", 1428 "IntStr": "42", 1429 "UintptrStr": "44", 1430 "PBool": null, 1431 "PInt": null, 1432 "PInt8": null, 1433 "PInt16": null, 1434 "PInt32": null, 1435 "PInt64": null, 1436 "PUint": null, 1437 "PUint8": null, 1438 "PUint16": null, 1439 "PUint32": null, 1440 "PUint64": null, 1441 "PUintptr": null, 1442 "PFloat32": null, 1443 "PFloat64": null, 1444 "String": "16", 1445 "PString": null, 1446 "Map": { 1447 "17": { 1448 "Tag": "tag17" 1449 }, 1450 "18": { 1451 "Tag": "tag18" 1452 } 1453 }, 1454 "MapP": { 1455 "19": { 1456 "Tag": "tag19" 1457 }, 1458 "20": null 1459 }, 1460 "PMap": null, 1461 "PMapP": null, 1462 "EmptyMap": {}, 1463 "NilMap": null, 1464 "Slice": [ 1465 { 1466 "Tag": "tag20" 1467 }, 1468 { 1469 "Tag": "tag21" 1470 } 1471 ], 1472 "SliceP": [ 1473 { 1474 "Tag": "tag22" 1475 }, 1476 null, 1477 { 1478 "Tag": "tag23" 1479 } 1480 ], 1481 "PSlice": null, 1482 "PSliceP": null, 1483 "EmptySlice": [], 1484 "NilSlice": null, 1485 "StringSlice": [ 1486 "str24", 1487 "str25", 1488 "str26" 1489 ], 1490 "ByteSlice": "Gxwd", 1491 "Small": { 1492 "Tag": "tag30" 1493 }, 1494 "PSmall": { 1495 "Tag": "tag31" 1496 }, 1497 "PPSmall": null, 1498 "Interface": 5.2, 1499 "PInterface": null 1500 }` 1501 1502 var allValueCompact = strings.Map(noSpace, allValueIndent) 1503 1504 var pallValueIndent = `{ 1505 "Bool": false, 1506 "Int": 0, 1507 "Int8": 0, 1508 "Int16": 0, 1509 "Int32": 0, 1510 "Int64": 0, 1511 "Uint": 0, 1512 "Uint8": 0, 1513 "Uint16": 0, 1514 "Uint32": 0, 1515 "Uint64": 0, 1516 "Uintptr": 0, 1517 "Float32": 0, 1518 "Float64": 0, 1519 "bar": "", 1520 "bar2": "", 1521 "IntStr": "0", 1522 "UintptrStr": "0", 1523 "PBool": true, 1524 "PInt": 2, 1525 "PInt8": 3, 1526 "PInt16": 4, 1527 "PInt32": 5, 1528 "PInt64": 6, 1529 "PUint": 7, 1530 "PUint8": 8, 1531 "PUint16": 9, 1532 "PUint32": 10, 1533 "PUint64": 11, 1534 "PUintptr": 12, 1535 "PFloat32": 14.1, 1536 "PFloat64": 15.1, 1537 "String": "", 1538 "PString": "16", 1539 "Map": null, 1540 "MapP": null, 1541 "PMap": { 1542 "17": { 1543 "Tag": "tag17" 1544 }, 1545 "18": { 1546 "Tag": "tag18" 1547 } 1548 }, 1549 "PMapP": { 1550 "19": { 1551 "Tag": "tag19" 1552 }, 1553 "20": null 1554 }, 1555 "EmptyMap": null, 1556 "NilMap": null, 1557 "Slice": null, 1558 "SliceP": null, 1559 "PSlice": [ 1560 { 1561 "Tag": "tag20" 1562 }, 1563 { 1564 "Tag": "tag21" 1565 } 1566 ], 1567 "PSliceP": [ 1568 { 1569 "Tag": "tag22" 1570 }, 1571 null, 1572 { 1573 "Tag": "tag23" 1574 } 1575 ], 1576 "EmptySlice": null, 1577 "NilSlice": null, 1578 "StringSlice": null, 1579 "ByteSlice": null, 1580 "Small": { 1581 "Tag": "" 1582 }, 1583 "PSmall": null, 1584 "PPSmall": { 1585 "Tag": "tag31" 1586 }, 1587 "Interface": null, 1588 "PInterface": 5.2 1589 }` 1590 1591 var pallValueCompact = strings.Map(noSpace, pallValueIndent) 1592 1593 func TestRefUnmarshal(t *testing.T) { 1594 type S struct { 1595 // Ref is defined in encode_test.go. 1596 R0 Ref 1597 R1 *Ref 1598 R2 RefText 1599 R3 *RefText 1600 } 1601 want := S{ 1602 R0: 12, 1603 R1: new(Ref), 1604 R2: 13, 1605 R3: new(RefText), 1606 } 1607 *want.R1 = 12 1608 *want.R3 = 13 1609 1610 var got S 1611 if err := Unmarshal([]byte(`{"R0":"ref","R1":"ref","R2":"ref","R3":"ref"}`), &got); err != nil { 1612 t.Fatalf("Unmarshal: %v", err) 1613 } 1614 if !reflect.DeepEqual(got, want) { 1615 t.Errorf("got %+v, want %+v", got, want) 1616 } 1617 } 1618 1619 // Test that the empty string doesn't panic decoding when ,string is specified 1620 // Issue 3450 1621 func TestEmptyString(t *testing.T) { 1622 type T2 struct { 1623 Number1 int `json:",string"` 1624 Number2 int `json:",string"` 1625 } 1626 data := `{"Number1":"1", "Number2":""}` 1627 dec := NewDecoder(strings.NewReader(data)) 1628 var t2 T2 1629 err := dec.Decode(&t2) 1630 if err == nil { 1631 t.Fatal("Decode: did not return error") 1632 } 1633 if t2.Number1 != 1 { 1634 t.Fatal("Decode: did not set Number1") 1635 } 1636 } 1637 1638 // Test that a null for ,string is not replaced with the previous quoted string (issue 7046). 1639 // It should also not be an error (issue 2540, issue 8587). 1640 func TestNullString(t *testing.T) { 1641 type T struct { 1642 A int `json:",string"` 1643 B int `json:",string"` 1644 C *int `json:",string"` 1645 } 1646 data := []byte(`{"A": "1", "B": null, "C": null}`) 1647 var s T 1648 s.B = 1 1649 s.C = new(int) 1650 *s.C = 2 1651 err := Unmarshal(data, &s) 1652 if err != nil { 1653 t.Fatalf("Unmarshal: %v", err) 1654 } 1655 if s.B != 1 || s.C != nil { 1656 t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C) 1657 } 1658 } 1659 1660 func intp(x int) *int { 1661 p := new(int) 1662 *p = x 1663 return p 1664 } 1665 1666 func intpp(x *int) **int { 1667 pp := new(*int) 1668 *pp = x 1669 return pp 1670 } 1671 1672 var interfaceSetTests = []struct { 1673 pre interface{} 1674 json string 1675 post interface{} 1676 }{ 1677 {"foo", `"bar"`, "bar"}, 1678 {"foo", `2`, 2.0}, 1679 {"foo", `true`, true}, 1680 {"foo", `null`, nil}, 1681 1682 {nil, `null`, nil}, 1683 {new(int), `null`, nil}, 1684 {(*int)(nil), `null`, nil}, 1685 {new(*int), `null`, new(*int)}, 1686 {(**int)(nil), `null`, nil}, 1687 {intp(1), `null`, nil}, 1688 {intpp(nil), `null`, intpp(nil)}, 1689 {intpp(intp(1)), `null`, intpp(nil)}, 1690 } 1691 1692 func TestInterfaceSet(t *testing.T) { 1693 for _, tt := range interfaceSetTests { 1694 b := struct{ X interface{} }{tt.pre} 1695 blob := `{"X":` + tt.json + `}` 1696 if err := Unmarshal([]byte(blob), &b); err != nil { 1697 t.Errorf("Unmarshal %#q: %v", blob, err) 1698 continue 1699 } 1700 if !reflect.DeepEqual(b.X, tt.post) { 1701 t.Errorf("Unmarshal %#q into %#v: X=%#v, want %#v", blob, tt.pre, b.X, tt.post) 1702 } 1703 } 1704 } 1705 1706 type NullTest struct { 1707 Bool bool 1708 Int int 1709 Int8 int8 1710 Int16 int16 1711 Int32 int32 1712 Int64 int64 1713 Uint uint 1714 Uint8 uint8 1715 Uint16 uint16 1716 Uint32 uint32 1717 Uint64 uint64 1718 Float32 float32 1719 Float64 float64 1720 String string 1721 PBool *bool 1722 Map map[string]string 1723 Slice []string 1724 Interface interface{} 1725 1726 PRaw *RawMessage 1727 PTime *time.Time 1728 PBigInt *big.Int 1729 PText *MustNotUnmarshalText 1730 PBuffer *bytes.Buffer // has methods, just not relevant ones 1731 PStruct *struct{} 1732 1733 Raw RawMessage 1734 Time time.Time 1735 BigInt big.Int 1736 Text MustNotUnmarshalText 1737 Buffer bytes.Buffer 1738 Struct struct{} 1739 } 1740 1741 type NullTestStrings struct { 1742 Bool bool `json:",string"` 1743 Int int `json:",string"` 1744 Int8 int8 `json:",string"` 1745 Int16 int16 `json:",string"` 1746 Int32 int32 `json:",string"` 1747 Int64 int64 `json:",string"` 1748 Uint uint `json:",string"` 1749 Uint8 uint8 `json:",string"` 1750 Uint16 uint16 `json:",string"` 1751 Uint32 uint32 `json:",string"` 1752 Uint64 uint64 `json:",string"` 1753 Float32 float32 `json:",string"` 1754 Float64 float64 `json:",string"` 1755 String string `json:",string"` 1756 PBool *bool `json:",string"` 1757 Map map[string]string `json:",string"` 1758 Slice []string `json:",string"` 1759 Interface interface{} `json:",string"` 1760 1761 PRaw *RawMessage `json:",string"` 1762 PTime *time.Time `json:",string"` 1763 PBigInt *big.Int `json:",string"` 1764 PText *MustNotUnmarshalText `json:",string"` 1765 PBuffer *bytes.Buffer `json:",string"` 1766 PStruct *struct{} `json:",string"` 1767 1768 Raw RawMessage `json:",string"` 1769 Time time.Time `json:",string"` 1770 BigInt big.Int `json:",string"` 1771 Text MustNotUnmarshalText `json:",string"` 1772 Buffer bytes.Buffer `json:",string"` 1773 Struct struct{} `json:",string"` 1774 } 1775 1776 // JSON null values should be ignored for primitives and string values instead of resulting in an error. 1777 // Issue 2540 1778 func TestUnmarshalNulls(t *testing.T) { 1779 // Unmarshal docs: 1780 // The JSON null value unmarshals into an interface, map, pointer, or slice 1781 // by setting that Go value to nil. Because null is often used in JSON to mean 1782 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect 1783 // on the value and produces no error. 1784 1785 jsonData := []byte(`{ 1786 "Bool" : null, 1787 "Int" : null, 1788 "Int8" : null, 1789 "Int16" : null, 1790 "Int32" : null, 1791 "Int64" : null, 1792 "Uint" : null, 1793 "Uint8" : null, 1794 "Uint16" : null, 1795 "Uint32" : null, 1796 "Uint64" : null, 1797 "Float32" : null, 1798 "Float64" : null, 1799 "String" : null, 1800 "PBool": null, 1801 "Map": null, 1802 "Slice": null, 1803 "Interface": null, 1804 "PRaw": null, 1805 "PTime": null, 1806 "PBigInt": null, 1807 "PText": null, 1808 "PBuffer": null, 1809 "PStruct": null, 1810 "Raw": null, 1811 "Time": null, 1812 "BigInt": null, 1813 "Text": null, 1814 "Buffer": null, 1815 "Struct": null 1816 }`) 1817 nulls := NullTest{ 1818 Bool: true, 1819 Int: 2, 1820 Int8: 3, 1821 Int16: 4, 1822 Int32: 5, 1823 Int64: 6, 1824 Uint: 7, 1825 Uint8: 8, 1826 Uint16: 9, 1827 Uint32: 10, 1828 Uint64: 11, 1829 Float32: 12.1, 1830 Float64: 13.1, 1831 String: "14", 1832 PBool: new(bool), 1833 Map: map[string]string{}, 1834 Slice: []string{}, 1835 Interface: new(MustNotUnmarshalJSON), 1836 PRaw: new(RawMessage), 1837 PTime: new(time.Time), 1838 PBigInt: new(big.Int), 1839 PText: new(MustNotUnmarshalText), 1840 PStruct: new(struct{}), 1841 PBuffer: new(bytes.Buffer), 1842 Raw: RawMessage("123"), 1843 Time: time.Unix(123456789, 0), 1844 BigInt: *big.NewInt(123), 1845 } 1846 1847 before := nulls.Time.String() 1848 1849 err := Unmarshal(jsonData, &nulls) 1850 if err != nil { 1851 t.Errorf("Unmarshal of null values failed: %v", err) 1852 } 1853 if !nulls.Bool || nulls.Int != 2 || nulls.Int8 != 3 || nulls.Int16 != 4 || nulls.Int32 != 5 || nulls.Int64 != 6 || 1854 nulls.Uint != 7 || nulls.Uint8 != 8 || nulls.Uint16 != 9 || nulls.Uint32 != 10 || nulls.Uint64 != 11 || 1855 nulls.Float32 != 12.1 || nulls.Float64 != 13.1 || nulls.String != "14" { 1856 t.Errorf("Unmarshal of null values affected primitives") 1857 } 1858 1859 if nulls.PBool != nil { 1860 t.Errorf("Unmarshal of null did not clear nulls.PBool") 1861 } 1862 if nulls.Map != nil { 1863 t.Errorf("Unmarshal of null did not clear nulls.Map") 1864 } 1865 if nulls.Slice != nil { 1866 t.Errorf("Unmarshal of null did not clear nulls.Slice") 1867 } 1868 if nulls.Interface != nil { 1869 t.Errorf("Unmarshal of null did not clear nulls.Interface") 1870 } 1871 if nulls.PRaw != nil { 1872 t.Errorf("Unmarshal of null did not clear nulls.PRaw") 1873 } 1874 if nulls.PTime != nil { 1875 t.Errorf("Unmarshal of null did not clear nulls.PTime") 1876 } 1877 if nulls.PBigInt != nil { 1878 t.Errorf("Unmarshal of null did not clear nulls.PBigInt") 1879 } 1880 if nulls.PText != nil { 1881 t.Errorf("Unmarshal of null did not clear nulls.PText") 1882 } 1883 if nulls.PBuffer != nil { 1884 t.Errorf("Unmarshal of null did not clear nulls.PBuffer") 1885 } 1886 if nulls.PStruct != nil { 1887 t.Errorf("Unmarshal of null did not clear nulls.PStruct") 1888 } 1889 1890 if string(nulls.Raw) != "null" { 1891 t.Errorf("Unmarshal of RawMessage null did not record null: %v", string(nulls.Raw)) 1892 } 1893 if nulls.Time.String() != before { 1894 t.Errorf("Unmarshal of time.Time null set time to %v", nulls.Time.String()) 1895 } 1896 if nulls.BigInt.String() != "123" { 1897 t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String()) 1898 } 1899 } 1900 1901 type MustNotUnmarshalJSON struct{} 1902 1903 func (x MustNotUnmarshalJSON) UnmarshalJSON(data []byte) error { 1904 return errors.New("MustNotUnmarshalJSON was used") 1905 } 1906 1907 type MustNotUnmarshalText struct{} 1908 1909 func (x MustNotUnmarshalText) UnmarshalText(text []byte) error { 1910 return errors.New("MustNotUnmarshalText was used") 1911 } 1912 1913 func TestStringKind(t *testing.T) { 1914 type stringKind string 1915 1916 var m1, m2 map[stringKind]int 1917 m1 = map[stringKind]int{ 1918 "foo": 42, 1919 } 1920 1921 data, err := Marshal(m1) 1922 if err != nil { 1923 t.Errorf("Unexpected error marshaling: %v", err) 1924 } 1925 1926 err = Unmarshal(data, &m2) 1927 if err != nil { 1928 t.Errorf("Unexpected error unmarshaling: %v", err) 1929 } 1930 1931 if !reflect.DeepEqual(m1, m2) { 1932 t.Error("Items should be equal after encoding and then decoding") 1933 } 1934 } 1935 1936 // Custom types with []byte as underlying type could not be marshaled 1937 // and then unmarshaled. 1938 // Issue 8962. 1939 func TestByteKind(t *testing.T) { 1940 type byteKind []byte 1941 1942 a := byteKind("hello") 1943 1944 data, err := Marshal(a) 1945 if err != nil { 1946 t.Error(err) 1947 } 1948 var b byteKind 1949 err = Unmarshal(data, &b) 1950 if err != nil { 1951 t.Fatal(err) 1952 } 1953 if !reflect.DeepEqual(a, b) { 1954 t.Errorf("expected %v == %v", a, b) 1955 } 1956 } 1957 1958 // The fix for issue 8962 introduced a regression. 1959 // Issue 12921. 1960 func TestSliceOfCustomByte(t *testing.T) { 1961 type Uint8 uint8 1962 1963 a := []Uint8("hello") 1964 1965 data, err := Marshal(a) 1966 if err != nil { 1967 t.Fatal(err) 1968 } 1969 var b []Uint8 1970 err = Unmarshal(data, &b) 1971 if err != nil { 1972 t.Fatal(err) 1973 } 1974 if !reflect.DeepEqual(a, b) { 1975 t.Fatalf("expected %v == %v", a, b) 1976 } 1977 } 1978 1979 var decodeTypeErrorTests = []struct { 1980 dest interface{} 1981 src string 1982 }{ 1983 {new(string), `{"user": "name"}`}, // issue 4628. 1984 {new(error), `{}`}, // issue 4222 1985 {new(error), `[]`}, 1986 {new(error), `""`}, 1987 {new(error), `123`}, 1988 {new(error), `true`}, 1989 } 1990 1991 func TestUnmarshalTypeError(t *testing.T) { 1992 for _, item := range decodeTypeErrorTests { 1993 err := Unmarshal([]byte(item.src), item.dest) 1994 if _, ok := err.(*UnmarshalTypeError); !ok { 1995 t.Errorf("expected type error for Unmarshal(%q, type %T): got %T", 1996 item.src, item.dest, err) 1997 } 1998 } 1999 } 2000 2001 var unmarshalSyntaxTests = []string{ 2002 "tru", 2003 "fals", 2004 "nul", 2005 "123e", 2006 `"hello`, 2007 `[1,2,3`, 2008 `{"key":1`, 2009 `{"key":1,`, 2010 } 2011 2012 func TestUnmarshalSyntax(t *testing.T) { 2013 var x interface{} 2014 for _, src := range unmarshalSyntaxTests { 2015 err := Unmarshal([]byte(src), &x) 2016 if _, ok := err.(*SyntaxError); !ok { 2017 t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err) 2018 } 2019 } 2020 } 2021 2022 // Test handling of unexported fields that should be ignored. 2023 // Issue 4660 2024 type unexportedFields struct { 2025 Name string 2026 m map[string]interface{} `json:"-"` 2027 m2 map[string]interface{} `json:"abcd"` 2028 2029 s []int `json:"-"` 2030 } 2031 2032 func TestUnmarshalUnexported(t *testing.T) { 2033 input := `{"Name": "Bob", "m": {"x": 123}, "m2": {"y": 456}, "abcd": {"z": 789}, "s": [2, 3]}` 2034 want := &unexportedFields{Name: "Bob"} 2035 2036 out := &unexportedFields{} 2037 err := Unmarshal([]byte(input), out) 2038 if err != nil { 2039 t.Errorf("got error %v, expected nil", err) 2040 } 2041 if !reflect.DeepEqual(out, want) { 2042 t.Errorf("got %q, want %q", out, want) 2043 } 2044 } 2045 2046 // Time3339 is a time.Time which encodes to and from JSON 2047 // as an RFC 3339 time in UTC. 2048 type Time3339 time.Time 2049 2050 func (t *Time3339) UnmarshalJSON(b []byte) error { 2051 if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' { 2052 return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time", b) 2053 } 2054 tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1])) 2055 if err != nil { 2056 return err 2057 } 2058 *t = Time3339(tm) 2059 return nil 2060 } 2061 2062 func TestUnmarshalJSONLiteralError(t *testing.T) { 2063 var t3 Time3339 2064 err := Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3) 2065 if err == nil { 2066 t.Fatalf("expected error; got time %v", time.Time(t3)) 2067 } 2068 if !strings.Contains(err.Error(), "range") { 2069 t.Errorf("got err = %v; want out of range error", err) 2070 } 2071 } 2072 2073 // Test that extra object elements in an array do not result in a 2074 // "data changing underfoot" error. 2075 // Issue 3717 2076 func TestSkipArrayObjects(t *testing.T) { 2077 json := `[{}]` 2078 var dest [0]interface{} 2079 2080 err := Unmarshal([]byte(json), &dest) 2081 if err != nil { 2082 t.Errorf("got error %q, want nil", err) 2083 } 2084 } 2085 2086 // Test semantics of AppendPre-filled struct fields and AppendPre-filled map fields. 2087 // Issue 4900. 2088 func TestPrefilled(t *testing.T) { 2089 ptrToMap := func(m map[string]interface{}) *map[string]interface{} { return &m } 2090 2091 // Values here change, cannot reuse table across runs. 2092 prefillTests := []struct { 2093 in string 2094 ptr interface{} 2095 out interface{} 2096 }{ 2097 { 2098 in: `{"X": 1, "Y": 2}`, 2099 ptr: &XYZ{X: float32(3), Y: int16(4), Z: 1.5}, 2100 out: &XYZ{X: float64(1), Y: float64(2), Z: 1.5}, 2101 }, 2102 { 2103 in: `{"X": 1, "Y": 2}`, 2104 ptr: ptrToMap(map[string]interface{}{"X": float32(3), "Y": int16(4), "Z": 1.5}), 2105 out: ptrToMap(map[string]interface{}{"X": float64(1), "Y": float64(2), "Z": 1.5}), 2106 }, 2107 } 2108 2109 for _, tt := range prefillTests { 2110 ptrstr := fmt.Sprintf("%v", tt.ptr) 2111 err := Unmarshal([]byte(tt.in), tt.ptr) // tt.ptr edited here 2112 if err != nil { 2113 t.Errorf("Unmarshal: %v", err) 2114 } 2115 if !reflect.DeepEqual(tt.ptr, tt.out) { 2116 t.Errorf("Unmarshal(%#q, %s): have %v, want %v", tt.in, ptrstr, tt.ptr, tt.out) 2117 } 2118 } 2119 } 2120 2121 var invalidUnmarshalTests = []struct { 2122 v interface{} 2123 want string 2124 }{ 2125 {nil, "json: Unmarshal(nil)"}, 2126 {struct{}{}, "json: Unmarshal(non-pointer struct {})"}, 2127 {(*int)(nil), "json: Unmarshal(nil *int)"}, 2128 } 2129 2130 func TestInvalidUnmarshal(t *testing.T) { 2131 buf := []byte(`{"a":"1"}`) 2132 for _, tt := range invalidUnmarshalTests { 2133 err := Unmarshal(buf, tt.v) 2134 if err == nil { 2135 t.Errorf("Unmarshal expecting error, got nil") 2136 continue 2137 } 2138 if got := err.Error(); got != tt.want { 2139 t.Errorf("Unmarshal = %q; want %q", got, tt.want) 2140 } 2141 } 2142 } 2143 2144 var invalidUnmarshalTextTests = []struct { 2145 v interface{} 2146 want string 2147 }{ 2148 {nil, "json: Unmarshal(nil)"}, 2149 {struct{}{}, "json: Unmarshal(non-pointer struct {})"}, 2150 {(*int)(nil), "json: Unmarshal(nil *int)"}, 2151 {new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"}, 2152 } 2153 2154 func TestInvalidUnmarshalText(t *testing.T) { 2155 buf := []byte(`123`) 2156 for _, tt := range invalidUnmarshalTextTests { 2157 err := Unmarshal(buf, tt.v) 2158 if err == nil { 2159 t.Errorf("Unmarshal expecting error, got nil") 2160 continue 2161 } 2162 if got := err.Error(); got != tt.want { 2163 t.Errorf("Unmarshal = %q; want %q", got, tt.want) 2164 } 2165 } 2166 } 2167 2168 // Test that string option is ignored for invalid types. 2169 // Issue 9812. 2170 func TestInvalidStringOption(t *testing.T) { 2171 num := 0 2172 item := struct { 2173 T time.Time `json:",string"` 2174 M map[string]string `json:",string"` 2175 S []string `json:",string"` 2176 A [1]string `json:",string"` 2177 I interface{} `json:",string"` 2178 P *int `json:",string"` 2179 }{M: make(map[string]string), S: make([]string, 0), I: num, P: &num} 2180 2181 data, err := Marshal(item) 2182 if err != nil { 2183 t.Fatalf("Marshal: %v", err) 2184 } 2185 2186 err = Unmarshal(data, &item) 2187 if err != nil { 2188 t.Fatalf("Unmarshal: %v", err) 2189 } 2190 } 2191 2192 // Test unmarshal behavior with regards to embedded unexported structs. 2193 // 2194 // (Issue 21357) If the embedded struct is a pointer and is unallocated, 2195 // this returns an error because unmarshal cannot set the field. 2196 // 2197 // (Issue 24152) If the embedded struct is given an explicit name, 2198 // ensure that the normal unmarshal logic does not panic in reflect. 2199 // 2200 // (Issue 28145) If the embedded struct is given an explicit name and has 2201 // exported methods, don't cause a panic trying to get its value. 2202 func TestUnmarshalEmbeddedUnexported(t *testing.T) { 2203 type ( 2204 embed1 struct{ Q int } 2205 embed2 struct{ Q int } 2206 embed3 struct { 2207 Q int64 `json:",string"` 2208 } 2209 S1 struct { 2210 *embed1 2211 R int 2212 } 2213 S2 struct { 2214 *embed1 2215 Q int 2216 } 2217 S3 struct { 2218 embed1 2219 R int 2220 } 2221 S4 struct { 2222 *embed1 2223 embed2 2224 } 2225 S5 struct { 2226 *embed3 2227 R int 2228 } 2229 S6 struct { 2230 embed1 `json:"embed1"` 2231 } 2232 S7 struct { 2233 embed1 `json:"embed1"` 2234 embed2 2235 } 2236 S8 struct { 2237 embed1 `json:"embed1"` 2238 embed2 `json:"embed2"` 2239 Q int 2240 } 2241 S9 struct { 2242 unexportedWithMethods `json:"embed"` 2243 } 2244 ) 2245 2246 tests := []struct { 2247 in string 2248 ptr interface{} 2249 out interface{} 2250 err error 2251 }{{ 2252 // Error since we cannot set S1.embed1, but still able to set S1.R. 2253 in: `{"R":2,"Q":1}`, 2254 ptr: new(S1), 2255 out: &S1{R: 2}, 2256 err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: jsoncolor.embed1"), 2257 }, { 2258 // The top level Q field takes precedence. 2259 in: `{"Q":1}`, 2260 ptr: new(S2), 2261 out: &S2{Q: 1}, 2262 }, { 2263 // No issue with non-pointer variant. 2264 in: `{"R":2,"Q":1}`, 2265 ptr: new(S3), 2266 out: &S3{embed1: embed1{Q: 1}, R: 2}, 2267 }, { 2268 // No error since both embedded structs have field R, which annihilate each other. 2269 // Thus, no attempt is made at setting S4.embed1. 2270 in: `{"R":2}`, 2271 ptr: new(S4), 2272 out: new(S4), 2273 }, { 2274 // Error since we cannot set S5.embed1, but still able to set S5.R. 2275 in: `{"R":2,"Q":1}`, 2276 ptr: new(S5), 2277 out: &S5{R: 2}, 2278 err: fmt.Errorf("json: cannot set embedded pointer to unexported struct: jsoncolor.embed3"), 2279 }, { 2280 // Issue 24152, ensure decodeState.indirect does not panic. 2281 in: `{"embed1": {"Q": 1}}`, 2282 ptr: new(S6), 2283 out: &S6{embed1{1}}, 2284 }, { 2285 // Issue 24153, check that we can still set forwarded fields even in 2286 // the presence of a name conflict. 2287 // 2288 // This relies on obscure behavior of reflect where it is possible 2289 // to set a forwarded exported field on an unexported embedded struct 2290 // even though there is a name conflict, even when it would have been 2291 // impossible to do so according to Go visibility rules. 2292 // Go forbids this because it is ambiguous whether S7.Q refers to 2293 // S7.embed1.Q or S7.embed2.Q. Since embed1 and embed2 are unexported, 2294 // it should be impossible for an external package to set either Q. 2295 // 2296 // It is probably okay for a future reflect change to break this. 2297 in: `{"embed1": {"Q": 1}, "Q": 2}`, 2298 ptr: new(S7), 2299 out: &S7{embed1{1}, embed2{2}}, 2300 }, { 2301 // Issue 24153, similar to the S7 case. 2302 in: `{"embed1": {"Q": 1}, "embed2": {"Q": 2}, "Q": 3}`, 2303 ptr: new(S8), 2304 out: &S8{embed1{1}, embed2{2}, 3}, 2305 }, { 2306 // Issue 228145, similar to the cases above. 2307 in: `{"embed": {}}`, 2308 ptr: new(S9), 2309 out: &S9{}, 2310 }} 2311 2312 for i, tt := range tests { 2313 err := Unmarshal([]byte(tt.in), tt.ptr) 2314 if !equalError(err, tt.err) { 2315 t.Errorf("#%d: %v, want %v", i, err, tt.err) 2316 } 2317 if !reflect.DeepEqual(tt.ptr, tt.out) { 2318 t.Errorf("#%d: mismatch\ngot: %#+v\nwant: %#+v", i, tt.ptr, tt.out) 2319 } 2320 } 2321 } 2322 2323 type unmarshalPanic struct{} 2324 2325 func (unmarshalPanic) UnmarshalJSON([]byte) error { panic(0xdead) } 2326 2327 func TestUnmarshalPanic(t *testing.T) { 2328 defer func() { 2329 if got := recover(); !reflect.DeepEqual(got, 0xdead) { 2330 t.Errorf("panic() = (%T)(%v), want 0xdead", got, got) 2331 } 2332 }() 2333 Unmarshal([]byte("{}"), &unmarshalPanic{}) 2334 t.Fatalf("Unmarshal should have panicked") 2335 } 2336 2337 // The decoder used to hang if decoding into an interface pointing to its own address. 2338 // See golang.org/issues/31740. 2339 func TestUnmarshalRecursivePointer(t *testing.T) { 2340 var v interface{} 2341 v = &v 2342 data := []byte(`{"a": "b"}`) 2343 2344 if err := Unmarshal(data, v); err != nil { 2345 t.Fatal(err) 2346 } 2347 }