github.com/segmentio/encoding@v0.4.0/json/decode.go (about) 1 package json 2 3 import ( 4 "bytes" 5 "encoding" 6 "encoding/json" 7 "fmt" 8 "math" 9 "math/big" 10 "reflect" 11 "strconv" 12 "time" 13 "unsafe" 14 15 "github.com/segmentio/asm/base64" 16 "github.com/segmentio/asm/keyset" 17 "github.com/segmentio/encoding/iso8601" 18 ) 19 20 func (d decoder) anyFlagsSet(flags ParseFlags) bool { 21 return d.flags&flags != 0 22 } 23 24 func (d decoder) decodeNull(b []byte, p unsafe.Pointer) ([]byte, error) { 25 if hasNullPrefix(b) { 26 return b[4:], nil 27 } 28 return d.inputError(b, nullType) 29 } 30 31 func (d decoder) decodeBool(b []byte, p unsafe.Pointer) ([]byte, error) { 32 switch { 33 case hasTruePrefix(b): 34 *(*bool)(p) = true 35 return b[4:], nil 36 37 case hasFalsePrefix(b): 38 *(*bool)(p) = false 39 return b[5:], nil 40 41 case hasNullPrefix(b): 42 return b[4:], nil 43 44 default: 45 return d.inputError(b, boolType) 46 } 47 } 48 49 func (d decoder) decodeInt(b []byte, p unsafe.Pointer) ([]byte, error) { 50 if hasNullPrefix(b) { 51 return b[4:], nil 52 } 53 54 v, r, err := d.parseInt(b, intType) 55 if err != nil { 56 return r, err 57 } 58 59 *(*int)(p) = int(v) 60 return r, nil 61 } 62 63 func (d decoder) decodeInt8(b []byte, p unsafe.Pointer) ([]byte, error) { 64 if hasNullPrefix(b) { 65 return b[4:], nil 66 } 67 68 v, r, err := d.parseInt(b, int8Type) 69 if err != nil { 70 return r, err 71 } 72 73 if v < math.MinInt8 || v > math.MaxInt8 { 74 return r, unmarshalOverflow(b[:len(b)-len(r)], int8Type) 75 } 76 77 *(*int8)(p) = int8(v) 78 return r, nil 79 } 80 81 func (d decoder) decodeInt16(b []byte, p unsafe.Pointer) ([]byte, error) { 82 if hasNullPrefix(b) { 83 return b[4:], nil 84 } 85 86 v, r, err := d.parseInt(b, int16Type) 87 if err != nil { 88 return r, err 89 } 90 91 if v < math.MinInt16 || v > math.MaxInt16 { 92 return r, unmarshalOverflow(b[:len(b)-len(r)], int16Type) 93 } 94 95 *(*int16)(p) = int16(v) 96 return r, nil 97 } 98 99 func (d decoder) decodeInt32(b []byte, p unsafe.Pointer) ([]byte, error) { 100 if hasNullPrefix(b) { 101 return b[4:], nil 102 } 103 104 v, r, err := d.parseInt(b, int32Type) 105 if err != nil { 106 return r, err 107 } 108 109 if v < math.MinInt32 || v > math.MaxInt32 { 110 return r, unmarshalOverflow(b[:len(b)-len(r)], int32Type) 111 } 112 113 *(*int32)(p) = int32(v) 114 return r, nil 115 } 116 117 func (d decoder) decodeInt64(b []byte, p unsafe.Pointer) ([]byte, error) { 118 if hasNullPrefix(b) { 119 return b[4:], nil 120 } 121 122 v, r, err := d.parseInt(b, int64Type) 123 if err != nil { 124 return r, err 125 } 126 127 *(*int64)(p) = v 128 return r, nil 129 } 130 131 func (d decoder) decodeUint(b []byte, p unsafe.Pointer) ([]byte, error) { 132 if hasNullPrefix(b) { 133 return b[4:], nil 134 } 135 136 v, r, err := d.parseUint(b, uintType) 137 if err != nil { 138 return r, err 139 } 140 141 *(*uint)(p) = uint(v) 142 return r, nil 143 } 144 145 func (d decoder) decodeUintptr(b []byte, p unsafe.Pointer) ([]byte, error) { 146 if hasNullPrefix(b) { 147 return b[4:], nil 148 } 149 150 v, r, err := d.parseUint(b, uintptrType) 151 if err != nil { 152 return r, err 153 } 154 155 *(*uintptr)(p) = uintptr(v) 156 return r, nil 157 } 158 159 func (d decoder) decodeUint8(b []byte, p unsafe.Pointer) ([]byte, error) { 160 if hasNullPrefix(b) { 161 return b[4:], nil 162 } 163 164 v, r, err := d.parseUint(b, uint8Type) 165 if err != nil { 166 return r, err 167 } 168 169 if v > math.MaxUint8 { 170 return r, unmarshalOverflow(b[:len(b)-len(r)], uint8Type) 171 } 172 173 *(*uint8)(p) = uint8(v) 174 return r, nil 175 } 176 177 func (d decoder) decodeUint16(b []byte, p unsafe.Pointer) ([]byte, error) { 178 if hasNullPrefix(b) { 179 return b[4:], nil 180 } 181 182 v, r, err := d.parseUint(b, uint16Type) 183 if err != nil { 184 return r, err 185 } 186 187 if v > math.MaxUint16 { 188 return r, unmarshalOverflow(b[:len(b)-len(r)], uint16Type) 189 } 190 191 *(*uint16)(p) = uint16(v) 192 return r, nil 193 } 194 195 func (d decoder) decodeUint32(b []byte, p unsafe.Pointer) ([]byte, error) { 196 if hasNullPrefix(b) { 197 return b[4:], nil 198 } 199 200 v, r, err := d.parseUint(b, uint32Type) 201 if err != nil { 202 return r, err 203 } 204 205 if v > math.MaxUint32 { 206 return r, unmarshalOverflow(b[:len(b)-len(r)], uint32Type) 207 } 208 209 *(*uint32)(p) = uint32(v) 210 return r, nil 211 } 212 213 func (d decoder) decodeUint64(b []byte, p unsafe.Pointer) ([]byte, error) { 214 if hasNullPrefix(b) { 215 return b[4:], nil 216 } 217 218 v, r, err := d.parseUint(b, uint64Type) 219 if err != nil { 220 return r, err 221 } 222 223 *(*uint64)(p) = v 224 return r, nil 225 } 226 227 func (d decoder) decodeFloat32(b []byte, p unsafe.Pointer) ([]byte, error) { 228 if hasNullPrefix(b) { 229 return b[4:], nil 230 } 231 232 v, r, _, err := d.parseNumber(b) 233 if err != nil { 234 return d.inputError(b, float32Type) 235 } 236 237 f, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&v)), 32) 238 if err != nil { 239 return d.inputError(b, float32Type) 240 } 241 242 *(*float32)(p) = float32(f) 243 return r, nil 244 } 245 246 func (d decoder) decodeFloat64(b []byte, p unsafe.Pointer) ([]byte, error) { 247 if hasNullPrefix(b) { 248 return b[4:], nil 249 } 250 251 v, r, _, err := d.parseNumber(b) 252 if err != nil { 253 return d.inputError(b, float64Type) 254 } 255 256 f, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&v)), 64) 257 if err != nil { 258 return d.inputError(b, float64Type) 259 } 260 261 *(*float64)(p) = f 262 return r, nil 263 } 264 265 func (d decoder) decodeNumber(b []byte, p unsafe.Pointer) ([]byte, error) { 266 if hasNullPrefix(b) { 267 return b[4:], nil 268 } 269 270 v, r, _, err := d.parseNumber(b) 271 if err != nil { 272 return d.inputError(b, numberType) 273 } 274 275 if (d.flags & DontCopyNumber) != 0 { 276 *(*Number)(p) = *(*Number)(unsafe.Pointer(&v)) 277 } else { 278 *(*Number)(p) = Number(v) 279 } 280 281 return r, nil 282 } 283 284 func (d decoder) decodeString(b []byte, p unsafe.Pointer) ([]byte, error) { 285 if hasNullPrefix(b) { 286 return b[4:], nil 287 } 288 289 s, r, new, err := d.parseStringUnquote(b, nil) 290 if err != nil { 291 if len(b) == 0 || b[0] != '"' { 292 return d.inputError(b, stringType) 293 } 294 return r, err 295 } 296 297 if new || (d.flags&DontCopyString) != 0 { 298 *(*string)(p) = *(*string)(unsafe.Pointer(&s)) 299 } else { 300 *(*string)(p) = string(s) 301 } 302 303 return r, nil 304 } 305 306 func (d decoder) decodeFromString(b []byte, p unsafe.Pointer, decode decodeFunc) ([]byte, error) { 307 if hasNullPrefix(b) { 308 return decode(d, b, p) 309 } 310 311 v, b, _, err := d.parseStringUnquote(b, nil) 312 if err != nil { 313 return d.inputError(v, stringType) 314 } 315 316 if v, err = decode(d, v, p); err != nil { 317 return b, err 318 } 319 320 if v = skipSpaces(v); len(v) != 0 { 321 return b, syntaxError(v, "unexpected trailing tokens after string value") 322 } 323 324 return b, nil 325 } 326 327 func (d decoder) decodeFromStringToInt(b []byte, p unsafe.Pointer, t reflect.Type, decode decodeFunc) ([]byte, error) { 328 if hasNullPrefix(b) { 329 return decode(d, b, p) 330 } 331 332 if len(b) > 0 && b[0] != '"' { 333 v, r, k, err := d.parseNumber(b) 334 if err == nil { 335 // The encoding/json package will return a *json.UnmarshalTypeError if 336 // the input was a floating point number representation, even tho a 337 // string is expected here. 338 if k == Float { 339 _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&v)), 64) 340 if err != nil { 341 return r, unmarshalTypeError(v, t) 342 } 343 } 344 } 345 return r, fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into int") 346 } 347 348 if len(b) > 1 && b[0] == '"' && b[1] == '"' { 349 return b, fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal \"\" into int") 350 } 351 352 v, b, _, err := d.parseStringUnquote(b, nil) 353 if err != nil { 354 return d.inputError(v, t) 355 } 356 357 if hasLeadingZeroes(v) { 358 // In this context the encoding/json package accepts leading zeroes because 359 // it is not constrained by the JSON syntax, remove them so the parsing 360 // functions don't return syntax errors. 361 u := make([]byte, 0, len(v)) 362 i := 0 363 364 if i < len(v) && v[i] == '-' || v[i] == '+' { 365 u = append(u, v[i]) 366 i++ 367 } 368 369 for (i+1) < len(v) && v[i] == '0' && '0' <= v[i+1] && v[i+1] <= '9' { 370 i++ 371 } 372 373 v = append(u, v[i:]...) 374 } 375 376 if r, err := decode(d, v, p); err != nil { 377 if _, isSyntaxError := err.(*SyntaxError); isSyntaxError { 378 if hasPrefix(v, "-") { 379 // The standard library interprets sequences of '-' characters 380 // as numbers but still returns type errors in this case... 381 return b, unmarshalTypeError(v, t) 382 } 383 return b, fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into int", prefix(v)) 384 } 385 // When the input value was a valid number representation we retain the 386 // error returned by the decoder. 387 if _, _, _, err := d.parseNumber(v); err != nil { 388 // When the input value valid JSON we mirror the behavior of the 389 // encoding/json package and return a generic error. 390 if _, _, _, err := d.parseValue(v); err == nil { 391 return b, fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into int", prefix(v)) 392 } 393 } 394 return b, err 395 } else if len(r) != 0 { 396 return r, unmarshalTypeError(v, t) 397 } 398 399 return b, nil 400 } 401 402 func (d decoder) decodeBytes(b []byte, p unsafe.Pointer) ([]byte, error) { 403 if hasNullPrefix(b) { 404 *(*[]byte)(p) = nil 405 return b[4:], nil 406 } 407 408 if len(b) < 2 { 409 return d.inputError(b, bytesType) 410 } 411 412 if b[0] != '"' { 413 // Go 1.7- behavior: bytes slices may be decoded from array of integers. 414 if len(b) > 0 && b[0] == '[' { 415 return d.decodeSlice(b, p, 1, bytesType, decoder.decodeUint8) 416 } 417 return d.inputError(b, bytesType) 418 } 419 420 // The input string contains escaped sequences, we need to parse it before 421 // decoding it to match the encoding/json package behvaior. 422 src, r, _, err := d.parseStringUnquote(b, nil) 423 if err != nil { 424 return d.inputError(b, bytesType) 425 } 426 427 dst := make([]byte, base64.StdEncoding.DecodedLen(len(src))) 428 429 n, err := base64.StdEncoding.Decode(dst, src) 430 if err != nil { 431 return r, err 432 } 433 434 *(*[]byte)(p) = dst[:n] 435 return r, nil 436 } 437 438 func (d decoder) decodeDuration(b []byte, p unsafe.Pointer) ([]byte, error) { 439 if hasNullPrefix(b) { 440 return b[4:], nil 441 } 442 443 // in order to inter-operate with the stdlib, we must be able to interpret 444 // durations passed as integer values. there's some discussion about being 445 // flexible on how durations are formatted, but for the time being, it's 446 // been punted to go2 at the earliest: https://github.com/golang/go/issues/4712 447 if len(b) > 0 && b[0] != '"' { 448 v, r, err := d.parseInt(b, durationType) 449 if err != nil { 450 return d.inputError(b, int32Type) 451 } 452 453 if v < math.MinInt64 || v > math.MaxInt64 { 454 return r, unmarshalOverflow(b[:len(b)-len(r)], int32Type) 455 } 456 457 *(*time.Duration)(p) = time.Duration(v) 458 return r, nil 459 } 460 461 if len(b) < 2 || b[0] != '"' { 462 return d.inputError(b, durationType) 463 } 464 465 i := bytes.IndexByte(b[1:], '"') + 1 466 if i <= 0 { 467 return d.inputError(b, durationType) 468 } 469 470 s := b[1:i] // trim quotes 471 472 v, err := time.ParseDuration(*(*string)(unsafe.Pointer(&s))) 473 if err != nil { 474 return d.inputError(b, durationType) 475 } 476 477 *(*time.Duration)(p) = v 478 return b[i+1:], nil 479 } 480 481 func (d decoder) decodeTime(b []byte, p unsafe.Pointer) ([]byte, error) { 482 if hasNullPrefix(b) { 483 return b[4:], nil 484 } 485 486 if len(b) < 2 || b[0] != '"' { 487 return d.inputError(b, timeType) 488 } 489 490 i := bytes.IndexByte(b[1:], '"') + 1 491 if i <= 0 { 492 return d.inputError(b, timeType) 493 } 494 495 s := b[1:i] // trim quotes 496 497 v, err := iso8601.Parse(*(*string)(unsafe.Pointer(&s))) 498 if err != nil { 499 return d.inputError(b, timeType) 500 } 501 502 *(*time.Time)(p) = v 503 return b[i+1:], nil 504 } 505 506 func (d decoder) decodeArray(b []byte, p unsafe.Pointer, n int, size uintptr, t reflect.Type, decode decodeFunc) ([]byte, error) { 507 if hasNullPrefix(b) { 508 return b[4:], nil 509 } 510 511 if len(b) < 2 || b[0] != '[' { 512 return d.inputError(b, t) 513 } 514 b = b[1:] 515 516 var err error 517 for i := 0; i < n; i++ { 518 b = skipSpaces(b) 519 520 if i != 0 { 521 if len(b) == 0 { 522 return b, syntaxError(b, "unexpected EOF after array element") 523 } 524 switch b[0] { 525 case ',': 526 b = skipSpaces(b[1:]) 527 case ']': 528 return b[1:], nil 529 default: 530 return b, syntaxError(b, "expected ',' after array element but found '%c'", b[0]) 531 } 532 } 533 534 b, err = decode(d, b, unsafe.Pointer(uintptr(p)+(uintptr(i)*size))) 535 if err != nil { 536 if e, ok := err.(*UnmarshalTypeError); ok { 537 e.Struct = t.String() + e.Struct 538 e.Field = d.prependField(strconv.Itoa(i), e.Field) 539 } 540 return b, err 541 } 542 } 543 544 // The encoding/json package ignores extra elements found when decoding into 545 // array types (which have a fixed size). 546 for { 547 b = skipSpaces(b) 548 549 if len(b) == 0 { 550 return b, syntaxError(b, "missing closing ']' in array value") 551 } 552 553 switch b[0] { 554 case ',': 555 b = skipSpaces(b[1:]) 556 case ']': 557 return b[1:], nil 558 } 559 560 _, b, _, err = d.parseValue(b) 561 if err != nil { 562 return b, err 563 } 564 } 565 } 566 567 var ( 568 // This is a placeholder used to consturct non-nil empty slices. 569 empty struct{} 570 ) 571 572 func (d decoder) decodeSlice(b []byte, p unsafe.Pointer, size uintptr, t reflect.Type, decode decodeFunc) ([]byte, error) { 573 if hasNullPrefix(b) { 574 *(*slice)(p) = slice{} 575 return b[4:], nil 576 } 577 578 if len(b) < 2 { 579 return d.inputError(b, t) 580 } 581 582 if b[0] != '[' { 583 // Go 1.7- behavior: fallback to decoding as a []byte if the element 584 // type is byte; allow conversions from JSON strings even tho the 585 // underlying type implemented unmarshaler interfaces. 586 if t.Elem().Kind() == reflect.Uint8 { 587 return d.decodeBytes(b, p) 588 } 589 return d.inputError(b, t) 590 } 591 592 input := b 593 b = b[1:] 594 595 s := (*slice)(p) 596 s.len = 0 597 598 var err error 599 for { 600 b = skipSpaces(b) 601 602 if len(b) != 0 && b[0] == ']' { 603 if s.data == nil { 604 s.data = unsafe.Pointer(&empty) 605 } 606 return b[1:], nil 607 } 608 609 if s.len != 0 { 610 if len(b) == 0 { 611 return b, syntaxError(b, "unexpected EOF after array element") 612 } 613 if b[0] != ',' { 614 return b, syntaxError(b, "expected ',' after array element but found '%c'", b[0]) 615 } 616 b = skipSpaces(b[1:]) 617 } 618 619 if s.len == s.cap { 620 c := s.cap 621 622 if c == 0 { 623 c = 10 624 } else { 625 c *= 2 626 } 627 628 *s = extendSlice(t, s, c) 629 } 630 631 b, err = decode(d, b, unsafe.Pointer(uintptr(s.data)+(uintptr(s.len)*size))) 632 if err != nil { 633 if _, r, _, err := d.parseValue(input); err != nil { 634 return r, err 635 } else { 636 b = r 637 } 638 if e, ok := err.(*UnmarshalTypeError); ok { 639 e.Struct = t.String() + e.Struct 640 e.Field = d.prependField(strconv.Itoa(s.len), e.Field) 641 } 642 return b, err 643 } 644 645 s.len++ 646 } 647 } 648 649 func (d decoder) decodeMap(b []byte, p unsafe.Pointer, t, kt, vt reflect.Type, kz, vz reflect.Value, decodeKey, decodeValue decodeFunc) ([]byte, error) { 650 if hasNullPrefix(b) { 651 *(*unsafe.Pointer)(p) = nil 652 return b[4:], nil 653 } 654 655 if len(b) < 2 || b[0] != '{' { 656 return d.inputError(b, t) 657 } 658 i := 0 659 m := reflect.NewAt(t, p).Elem() 660 661 k := reflect.New(kt).Elem() 662 v := reflect.New(vt).Elem() 663 664 kptr := (*iface)(unsafe.Pointer(&k)).ptr 665 vptr := (*iface)(unsafe.Pointer(&v)).ptr 666 input := b 667 668 if m.IsNil() { 669 m = reflect.MakeMap(t) 670 } 671 672 var err error 673 b = b[1:] 674 for { 675 k.Set(kz) 676 v.Set(vz) 677 b = skipSpaces(b) 678 679 if len(b) != 0 && b[0] == '}' { 680 *(*unsafe.Pointer)(p) = unsafe.Pointer(m.Pointer()) 681 return b[1:], nil 682 } 683 684 if i != 0 { 685 if len(b) == 0 { 686 return b, syntaxError(b, "unexpected end of JSON input after object field value") 687 } 688 if b[0] != ',' { 689 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 690 } 691 b = skipSpaces(b[1:]) 692 } 693 694 if hasNullPrefix(b) { 695 return b, syntaxError(b, "cannot decode object key string from 'null' value") 696 } 697 698 if b, err = decodeKey(d, b, kptr); err != nil { 699 return objectKeyError(b, err) 700 } 701 b = skipSpaces(b) 702 703 if len(b) == 0 { 704 return b, syntaxError(b, "unexpected end of JSON input after object field key") 705 } 706 if b[0] != ':' { 707 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 708 } 709 b = skipSpaces(b[1:]) 710 711 if b, err = decodeValue(d, b, vptr); err != nil { 712 if _, r, _, err := d.parseValue(input); err != nil { 713 return r, err 714 } else { 715 b = r 716 } 717 if e, ok := err.(*UnmarshalTypeError); ok { 718 e.Struct = "map[" + kt.String() + "]" + vt.String() + "{" + e.Struct + "}" 719 e.Field = d.prependField(fmt.Sprint(k.Interface()), e.Field) 720 } 721 return b, err 722 } 723 724 m.SetMapIndex(k, v) 725 i++ 726 } 727 } 728 729 func (d decoder) decodeMapStringInterface(b []byte, p unsafe.Pointer) ([]byte, error) { 730 if hasNullPrefix(b) { 731 *(*unsafe.Pointer)(p) = nil 732 return b[4:], nil 733 } 734 735 if len(b) < 2 || b[0] != '{' { 736 return d.inputError(b, mapStringInterfaceType) 737 } 738 739 i := 0 740 m := *(*map[string]interface{})(p) 741 742 if m == nil { 743 m = make(map[string]interface{}, 64) 744 } 745 746 var ( 747 input = b 748 key string 749 val any 750 err error 751 ) 752 753 b = b[1:] 754 for { 755 key = "" 756 val = nil 757 758 b = skipSpaces(b) 759 760 if len(b) != 0 && b[0] == '}' { 761 *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(unsafe.Pointer(&m)) 762 return b[1:], nil 763 } 764 765 if i != 0 { 766 if len(b) == 0 { 767 return b, syntaxError(b, "unexpected end of JSON input after object field value") 768 } 769 if b[0] != ',' { 770 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 771 } 772 b = skipSpaces(b[1:]) 773 } 774 775 if hasNullPrefix(b) { 776 return b, syntaxError(b, "cannot decode object key string from 'null' value") 777 } 778 779 b, err = d.decodeString(b, unsafe.Pointer(&key)) 780 if err != nil { 781 return objectKeyError(b, err) 782 } 783 b = skipSpaces(b) 784 785 if len(b) == 0 { 786 return b, syntaxError(b, "unexpected end of JSON input after object field key") 787 } 788 if b[0] != ':' { 789 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 790 } 791 b = skipSpaces(b[1:]) 792 793 b, err = d.decodeInterface(b, unsafe.Pointer(&val)) 794 if err != nil { 795 if _, r, _, err := d.parseValue(input); err != nil { 796 return r, err 797 } else { 798 b = r 799 } 800 if e, ok := err.(*UnmarshalTypeError); ok { 801 e.Struct = mapStringInterfaceType.String() + e.Struct 802 e.Field = d.prependField(key, e.Field) 803 } 804 return b, err 805 } 806 807 m[key] = val 808 i++ 809 } 810 } 811 812 func (d decoder) decodeMapStringRawMessage(b []byte, p unsafe.Pointer) ([]byte, error) { 813 if hasNullPrefix(b) { 814 *(*unsafe.Pointer)(p) = nil 815 return b[4:], nil 816 } 817 818 if len(b) < 2 || b[0] != '{' { 819 return d.inputError(b, mapStringRawMessageType) 820 } 821 822 i := 0 823 m := *(*map[string]RawMessage)(p) 824 825 if m == nil { 826 m = make(map[string]RawMessage, 64) 827 } 828 829 var err error 830 var key string 831 var val RawMessage 832 var input = b 833 834 b = b[1:] 835 for { 836 key = "" 837 val = nil 838 839 b = skipSpaces(b) 840 841 if len(b) != 0 && b[0] == '}' { 842 *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(unsafe.Pointer(&m)) 843 return b[1:], nil 844 } 845 846 if i != 0 { 847 if len(b) == 0 { 848 return b, syntaxError(b, "unexpected end of JSON input after object field value") 849 } 850 if b[0] != ',' { 851 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 852 } 853 b = skipSpaces(b[1:]) 854 } 855 856 if hasNullPrefix(b) { 857 return b, syntaxError(b, "cannot decode object key string from 'null' value") 858 } 859 860 b, err = d.decodeString(b, unsafe.Pointer(&key)) 861 if err != nil { 862 return objectKeyError(b, err) 863 } 864 b = skipSpaces(b) 865 866 if len(b) == 0 { 867 return b, syntaxError(b, "unexpected end of JSON input after object field key") 868 } 869 if b[0] != ':' { 870 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 871 } 872 b = skipSpaces(b[1:]) 873 874 b, err = d.decodeRawMessage(b, unsafe.Pointer(&val)) 875 if err != nil { 876 if _, r, _, err := d.parseValue(input); err != nil { 877 return r, err 878 } else { 879 b = r 880 } 881 if e, ok := err.(*UnmarshalTypeError); ok { 882 e.Struct = mapStringRawMessageType.String() + e.Struct 883 e.Field = d.prependField(key, e.Field) 884 } 885 return b, err 886 } 887 888 m[key] = val 889 i++ 890 } 891 } 892 893 func (d decoder) decodeMapStringString(b []byte, p unsafe.Pointer) ([]byte, error) { 894 if hasNullPrefix(b) { 895 *(*unsafe.Pointer)(p) = nil 896 return b[4:], nil 897 } 898 899 if len(b) < 2 || b[0] != '{' { 900 return d.inputError(b, mapStringStringType) 901 } 902 903 i := 0 904 m := *(*map[string]string)(p) 905 906 if m == nil { 907 m = make(map[string]string, 64) 908 } 909 910 var err error 911 var key string 912 var val string 913 var input = b 914 915 b = b[1:] 916 for { 917 key = "" 918 val = "" 919 920 b = skipSpaces(b) 921 922 if len(b) != 0 && b[0] == '}' { 923 *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(unsafe.Pointer(&m)) 924 return b[1:], nil 925 } 926 927 if i != 0 { 928 if len(b) == 0 { 929 return b, syntaxError(b, "unexpected end of JSON input after object field value") 930 } 931 if b[0] != ',' { 932 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 933 } 934 b = skipSpaces(b[1:]) 935 } 936 937 if hasNullPrefix(b) { 938 return b, syntaxError(b, "cannot decode object key string from 'null' value") 939 } 940 941 b, err = d.decodeString(b, unsafe.Pointer(&key)) 942 if err != nil { 943 return objectKeyError(b, err) 944 } 945 b = skipSpaces(b) 946 947 if len(b) == 0 { 948 return b, syntaxError(b, "unexpected end of JSON input after object field key") 949 } 950 if b[0] != ':' { 951 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 952 } 953 b = skipSpaces(b[1:]) 954 955 b, err = d.decodeString(b, unsafe.Pointer(&val)) 956 if err != nil { 957 if _, r, _, err := d.parseValue(input); err != nil { 958 return r, err 959 } else { 960 b = r 961 } 962 if e, ok := err.(*UnmarshalTypeError); ok { 963 e.Struct = mapStringStringType.String() + e.Struct 964 e.Field = d.prependField(key, e.Field) 965 } 966 return b, err 967 } 968 969 m[key] = val 970 i++ 971 } 972 } 973 974 func (d decoder) decodeMapStringStringSlice(b []byte, p unsafe.Pointer) ([]byte, error) { 975 if hasNullPrefix(b) { 976 *(*unsafe.Pointer)(p) = nil 977 return b[4:], nil 978 } 979 980 if len(b) < 2 || b[0] != '{' { 981 return d.inputError(b, mapStringStringSliceType) 982 } 983 984 i := 0 985 m := *(*map[string][]string)(p) 986 987 if m == nil { 988 m = make(map[string][]string, 64) 989 } 990 991 var err error 992 var key string 993 var buf []string 994 var input = b 995 var stringSize = unsafe.Sizeof("") 996 997 b = b[1:] 998 for { 999 key = "" 1000 buf = buf[:0] 1001 1002 b = skipSpaces(b) 1003 1004 if len(b) != 0 && b[0] == '}' { 1005 *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(unsafe.Pointer(&m)) 1006 return b[1:], nil 1007 } 1008 1009 if i != 0 { 1010 if len(b) == 0 { 1011 return b, syntaxError(b, "unexpected end of JSON input after object field value") 1012 } 1013 if b[0] != ',' { 1014 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 1015 } 1016 b = skipSpaces(b[1:]) 1017 } 1018 1019 if hasNullPrefix(b) { 1020 return b, syntaxError(b, "cannot decode object key string from 'null' value") 1021 } 1022 1023 b, err = d.decodeString(b, unsafe.Pointer(&key)) 1024 if err != nil { 1025 return objectKeyError(b, err) 1026 } 1027 b = skipSpaces(b) 1028 1029 if len(b) == 0 { 1030 return b, syntaxError(b, "unexpected end of JSON input after object field key") 1031 } 1032 if b[0] != ':' { 1033 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 1034 } 1035 b = skipSpaces(b[1:]) 1036 1037 b, err = d.decodeSlice(b, unsafe.Pointer(&buf), stringSize, sliceStringType, decoder.decodeString) 1038 if err != nil { 1039 if _, r, _, err := d.parseValue(input); err != nil { 1040 return r, err 1041 } else { 1042 b = r 1043 } 1044 if e, ok := err.(*UnmarshalTypeError); ok { 1045 e.Struct = mapStringStringType.String() + e.Struct 1046 e.Field = d.prependField(key, e.Field) 1047 } 1048 return b, err 1049 } 1050 1051 val := make([]string, len(buf)) 1052 copy(val, buf) 1053 1054 m[key] = val 1055 i++ 1056 } 1057 } 1058 1059 func (d decoder) decodeMapStringBool(b []byte, p unsafe.Pointer) ([]byte, error) { 1060 if hasNullPrefix(b) { 1061 *(*unsafe.Pointer)(p) = nil 1062 return b[4:], nil 1063 } 1064 1065 if len(b) < 2 || b[0] != '{' { 1066 return d.inputError(b, mapStringBoolType) 1067 } 1068 1069 i := 0 1070 m := *(*map[string]bool)(p) 1071 1072 if m == nil { 1073 m = make(map[string]bool, 64) 1074 } 1075 1076 var err error 1077 var key string 1078 var val bool 1079 var input = b 1080 1081 b = b[1:] 1082 for { 1083 key = "" 1084 val = false 1085 1086 b = skipSpaces(b) 1087 1088 if len(b) != 0 && b[0] == '}' { 1089 *(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(unsafe.Pointer(&m)) 1090 return b[1:], nil 1091 } 1092 1093 if i != 0 { 1094 if len(b) == 0 { 1095 return b, syntaxError(b, "unexpected end of JSON input after object field value") 1096 } 1097 if b[0] != ',' { 1098 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 1099 } 1100 b = skipSpaces(b[1:]) 1101 } 1102 1103 if hasNullPrefix(b) { 1104 return b, syntaxError(b, "cannot decode object key string from 'null' value") 1105 } 1106 1107 b, err = d.decodeString(b, unsafe.Pointer(&key)) 1108 if err != nil { 1109 return objectKeyError(b, err) 1110 } 1111 b = skipSpaces(b) 1112 1113 if len(b) == 0 { 1114 return b, syntaxError(b, "unexpected end of JSON input after object field key") 1115 } 1116 if b[0] != ':' { 1117 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 1118 } 1119 b = skipSpaces(b[1:]) 1120 1121 b, err = d.decodeBool(b, unsafe.Pointer(&val)) 1122 if err != nil { 1123 if _, r, _, err := d.parseValue(input); err != nil { 1124 return r, err 1125 } else { 1126 b = r 1127 } 1128 if e, ok := err.(*UnmarshalTypeError); ok { 1129 e.Struct = mapStringStringType.String() + e.Struct 1130 e.Field = d.prependField(key, e.Field) 1131 } 1132 return b, err 1133 } 1134 1135 m[key] = val 1136 i++ 1137 } 1138 } 1139 1140 func (d decoder) decodeStruct(b []byte, p unsafe.Pointer, st *structType) ([]byte, error) { 1141 if hasNullPrefix(b) { 1142 return b[4:], nil 1143 } 1144 1145 if len(b) < 2 || b[0] != '{' { 1146 return d.inputError(b, st.typ) 1147 } 1148 1149 var err error 1150 var k []byte 1151 var i int 1152 1153 // memory buffer used to convert short field names to lowercase 1154 var buf [64]byte 1155 var key []byte 1156 var input = b 1157 1158 b = b[1:] 1159 for { 1160 b = skipSpaces(b) 1161 1162 if len(b) != 0 && b[0] == '}' { 1163 return b[1:], nil 1164 } 1165 1166 if i != 0 { 1167 if len(b) == 0 { 1168 return b, syntaxError(b, "unexpected end of JSON input after object field value") 1169 } 1170 if b[0] != ',' { 1171 return b, syntaxError(b, "expected ',' after object field value but found '%c'", b[0]) 1172 } 1173 b = skipSpaces(b[1:]) 1174 } 1175 i++ 1176 1177 if hasNullPrefix(b) { 1178 return b, syntaxError(b, "cannot decode object key string from 'null' value") 1179 } 1180 1181 k, b, _, err = d.parseStringUnquote(b, nil) 1182 if err != nil { 1183 return objectKeyError(b, err) 1184 } 1185 b = skipSpaces(b) 1186 1187 if len(b) == 0 { 1188 return b, syntaxError(b, "unexpected end of JSON input after object field key") 1189 } 1190 if b[0] != ':' { 1191 return b, syntaxError(b, "expected ':' after object field key but found '%c'", b[0]) 1192 } 1193 b = skipSpaces(b[1:]) 1194 1195 var f *structField 1196 if len(st.keyset) != 0 { 1197 if n := keyset.Lookup(st.keyset, k); n < len(st.fields) { 1198 f = &st.fields[n] 1199 } 1200 } else { 1201 f = st.fieldsIndex[string(k)] 1202 } 1203 1204 if f == nil && (d.flags&DontMatchCaseInsensitiveStructFields) == 0 { 1205 key = appendToLower(buf[:0], k) 1206 f = st.ficaseIndex[string(key)] 1207 } 1208 1209 if f == nil { 1210 if (d.flags & DisallowUnknownFields) != 0 { 1211 return b, fmt.Errorf("json: unknown field %q", k) 1212 } 1213 if _, b, _, err = d.parseValue(b); err != nil { 1214 return b, err 1215 } 1216 continue 1217 } 1218 1219 if b, err = f.codec.decode(d, b, unsafe.Pointer(uintptr(p)+f.offset)); err != nil { 1220 if _, r, _, err := d.parseValue(input); err != nil { 1221 return r, err 1222 } else { 1223 b = r 1224 } 1225 if e, ok := err.(*UnmarshalTypeError); ok { 1226 e.Struct = st.typ.String() + e.Struct 1227 e.Field = d.prependField(string(k), e.Field) 1228 } 1229 return b, err 1230 } 1231 } 1232 } 1233 1234 func (d decoder) decodeEmbeddedStructPointer(b []byte, p unsafe.Pointer, t reflect.Type, unexported bool, offset uintptr, decode decodeFunc) ([]byte, error) { 1235 v := *(*unsafe.Pointer)(p) 1236 1237 if v == nil { 1238 if unexported { 1239 return nil, fmt.Errorf("json: cannot set embedded pointer to unexported struct: %s", t) 1240 } 1241 v = unsafe.Pointer(reflect.New(t).Pointer()) 1242 *(*unsafe.Pointer)(p) = v 1243 } 1244 1245 return decode(d, b, unsafe.Pointer(uintptr(v)+offset)) 1246 } 1247 1248 func (d decoder) decodePointer(b []byte, p unsafe.Pointer, t reflect.Type, decode decodeFunc) ([]byte, error) { 1249 if hasNullPrefix(b) { 1250 pp := *(*unsafe.Pointer)(p) 1251 if pp != nil && t.Kind() == reflect.Ptr { 1252 return decode(d, b, pp) 1253 } 1254 *(*unsafe.Pointer)(p) = nil 1255 return b[4:], nil 1256 } 1257 1258 v := *(*unsafe.Pointer)(p) 1259 if v == nil { 1260 v = unsafe.Pointer(reflect.New(t).Pointer()) 1261 *(*unsafe.Pointer)(p) = v 1262 } 1263 1264 return decode(d, b, v) 1265 } 1266 1267 func (d decoder) decodeInterface(b []byte, p unsafe.Pointer) ([]byte, error) { 1268 val := *(*interface{})(p) 1269 *(*interface{})(p) = nil 1270 1271 if t := reflect.TypeOf(val); t != nil && t.Kind() == reflect.Ptr { 1272 if v := reflect.ValueOf(val); v.IsNil() || t.Elem().Kind() != reflect.Ptr { 1273 // If the destination is nil the only value that is OK to decode is 1274 // `null`, and the encoding/json package always nils the destination 1275 // interface value in this case. 1276 if hasNullPrefix(b) { 1277 *(*interface{})(p) = nil 1278 return b[4:], nil 1279 } 1280 } 1281 1282 b, err := Parse(b, val, d.flags) 1283 if err == nil { 1284 *(*interface{})(p) = val 1285 } 1286 1287 return b, err 1288 } 1289 1290 v, b, k, err := d.parseValue(b) 1291 if err != nil { 1292 return b, err 1293 } 1294 1295 switch k.Class() { 1296 case Object: 1297 v, err = decodeInto[map[string]any](&val, v, d, decoder.decodeMapStringInterface) 1298 1299 case Array: 1300 size := alignedSize(interfaceType) 1301 fn := constructSliceDecodeFunc(size, sliceInterfaceType, decoder.decodeInterface) 1302 v, err = decodeInto[[]any](&val, v, d, fn) 1303 1304 case String: 1305 v, err = decodeInto[string](&val, v, d, decoder.decodeString) 1306 1307 case Null: 1308 v, val = nil, nil 1309 1310 case Bool: 1311 v, val = nil, k == True 1312 1313 case Num: 1314 v, err = d.decodeDynamicNumber(v, unsafe.Pointer(&val)) 1315 1316 default: 1317 return b, syntaxError(v, "expected token but found '%c'", v[0]) 1318 } 1319 1320 if err != nil { 1321 return b, err 1322 } 1323 1324 if v = skipSpaces(v); len(v) != 0 { 1325 return b, syntaxError(v, "unexpected trailing trailing tokens after json value") 1326 } 1327 1328 *(*interface{})(p) = val 1329 return b, nil 1330 } 1331 1332 func (d decoder) decodeDynamicNumber(b []byte, p unsafe.Pointer) ([]byte, error) { 1333 kind := Float 1334 var err error 1335 1336 // Only pre-parse for numeric kind if a conditional decode 1337 // has been requested. 1338 if d.anyFlagsSet(UseBigInt | UseInt64 | UseUint64) { 1339 _, _, kind, err = d.parseNumber(b) 1340 if err != nil { 1341 return b, err 1342 } 1343 } 1344 1345 var rem []byte 1346 anyPtr := (*any)(p) 1347 1348 // Mutually exclusive integer handling cases. 1349 switch { 1350 // If requested, attempt decode of positive integers as uint64. 1351 case kind == Uint && d.anyFlagsSet(UseUint64): 1352 rem, err = decodeInto[uint64](anyPtr, b, d, decoder.decodeUint64) 1353 if err == nil { 1354 return rem, err 1355 } 1356 1357 // If uint64 decode was not requested but int64 decode was requested, 1358 // then attempt decode of positive integers as int64. 1359 case kind == Uint && d.anyFlagsSet(UseInt64): 1360 fallthrough 1361 1362 // If int64 decode was requested, 1363 // attempt decode of negative integers as int64. 1364 case kind == Int && d.anyFlagsSet(UseInt64): 1365 rem, err = decodeInto[int64](anyPtr, b, d, decoder.decodeInt64) 1366 if err == nil { 1367 return rem, err 1368 } 1369 } 1370 1371 // Fallback numeric handling cases: 1372 // these cannot be combined into the above switch, 1373 // since these cases also handle overflow 1374 // from the above cases, if decode was already attempted. 1375 switch { 1376 // If *big.Int decode was requested, handle that case for any integer. 1377 case kind == Uint && d.anyFlagsSet(UseBigInt): 1378 fallthrough 1379 case kind == Int && d.anyFlagsSet(UseBigInt): 1380 rem, err = decodeInto[*big.Int](anyPtr, b, d, bigIntDecoder) 1381 1382 // If json.Number decode was requested, handle that for any number. 1383 case d.anyFlagsSet(UseNumber): 1384 rem, err = decodeInto[Number](anyPtr, b, d, decoder.decodeNumber) 1385 1386 // Fall back to float64 decode when no special decoding has been requested. 1387 default: 1388 rem, err = decodeInto[float64](anyPtr, b, d, decoder.decodeFloat64) 1389 } 1390 1391 return rem, err 1392 } 1393 1394 func (d decoder) decodeMaybeEmptyInterface(b []byte, p unsafe.Pointer, t reflect.Type) ([]byte, error) { 1395 if hasNullPrefix(b) { 1396 *(*interface{})(p) = nil 1397 return b[4:], nil 1398 } 1399 1400 if x := reflect.NewAt(t, p).Elem(); !x.IsNil() { 1401 if e := x.Elem(); e.Kind() == reflect.Ptr { 1402 return Parse(b, e.Interface(), d.flags) 1403 } 1404 } else if t.NumMethod() == 0 { // empty interface 1405 return Parse(b, (*interface{})(p), d.flags) 1406 } 1407 1408 return d.decodeUnmarshalTypeError(b, p, t) 1409 } 1410 1411 func (d decoder) decodeUnmarshalTypeError(b []byte, p unsafe.Pointer, t reflect.Type) ([]byte, error) { 1412 v, b, _, err := d.parseValue(b) 1413 if err != nil { 1414 return b, err 1415 } 1416 return b, &UnmarshalTypeError{ 1417 Value: string(v), 1418 Type: t, 1419 } 1420 } 1421 1422 func (d decoder) decodeRawMessage(b []byte, p unsafe.Pointer) ([]byte, error) { 1423 v, r, _, err := d.parseValue(b) 1424 if err != nil { 1425 return d.inputError(b, rawMessageType) 1426 } 1427 1428 if (d.flags & DontCopyRawMessage) == 0 { 1429 v = append(make([]byte, 0, len(v)), v...) 1430 } 1431 1432 *(*RawMessage)(p) = json.RawMessage(v) 1433 return r, err 1434 } 1435 1436 func (d decoder) decodeJSONUnmarshaler(b []byte, p unsafe.Pointer, t reflect.Type, pointer bool) ([]byte, error) { 1437 v, b, _, err := d.parseValue(b) 1438 if err != nil { 1439 return b, err 1440 } 1441 1442 u := reflect.NewAt(t, p) 1443 if !pointer { 1444 u = u.Elem() 1445 t = t.Elem() 1446 } 1447 if u.IsNil() { 1448 u.Set(reflect.New(t)) 1449 } 1450 1451 return b, u.Interface().(Unmarshaler).UnmarshalJSON(v) 1452 } 1453 1454 func (d decoder) decodeTextUnmarshaler(b []byte, p unsafe.Pointer, t reflect.Type, pointer bool) ([]byte, error) { 1455 var value string 1456 1457 v, b, k, err := d.parseValue(b) 1458 if err != nil { 1459 return b, err 1460 } 1461 if len(v) == 0 { 1462 return d.inputError(v, t) 1463 } 1464 1465 switch k.Class() { 1466 case Null: 1467 return b, err 1468 1469 case String: 1470 s, _, _, err := d.parseStringUnquote(v, nil) 1471 if err != nil { 1472 return b, err 1473 } 1474 u := reflect.NewAt(t, p) 1475 if !pointer { 1476 u = u.Elem() 1477 t = t.Elem() 1478 } 1479 if u.IsNil() { 1480 u.Set(reflect.New(t)) 1481 } 1482 return b, u.Interface().(encoding.TextUnmarshaler).UnmarshalText(s) 1483 1484 case Bool: 1485 if k == True { 1486 value = "true" 1487 } else { 1488 value = "false" 1489 } 1490 1491 case Num: 1492 value = "number" 1493 1494 case Object: 1495 value = "object" 1496 1497 case Array: 1498 value = "array" 1499 } 1500 1501 return b, &UnmarshalTypeError{Value: value, Type: reflect.PtrTo(t)} 1502 } 1503 1504 func (d decoder) prependField(key, field string) string { 1505 if field != "" { 1506 return key + "." + field 1507 } 1508 return key 1509 } 1510 1511 func (d decoder) inputError(b []byte, t reflect.Type) ([]byte, error) { 1512 if len(b) == 0 { 1513 return nil, unexpectedEOF(b) 1514 } 1515 _, r, _, err := d.parseValue(b) 1516 if err != nil { 1517 return r, err 1518 } 1519 return skipSpaces(r), unmarshalTypeError(b, t) 1520 } 1521 1522 func decodeInto[T any](dest *any, b []byte, d decoder, fn decodeFunc) ([]byte, error) { 1523 var v T 1524 rem, err := fn(d, b, unsafe.Pointer(&v)) 1525 if err == nil { 1526 *dest = v 1527 } 1528 1529 return rem, err 1530 }