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