github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/encoding/json/decode.go (about) 1 // Vendored from Go 1.24.0-pre-release 2 // To find alterations, check package shims, and comments beginning in SHIM(). 3 // 4 // Copyright 2010 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 // Represents JSON data structure using native Go types: booleans, floats, 9 // strings, arrays, and maps. 10 11 package json 12 13 import ( 14 "encoding" 15 "encoding/base64" 16 "fmt" 17 "github.com/anthropics/anthropic-sdk-go/internal/encoding/json/shims" 18 "reflect" 19 "strconv" 20 "strings" 21 "unicode" 22 "unicode/utf16" 23 "unicode/utf8" 24 _ "unsafe" // for linkname 25 ) 26 27 // Unmarshal parses the JSON-encoded data and stores the result 28 // in the value pointed to by v. If v is nil or not a pointer, 29 // Unmarshal returns an [InvalidUnmarshalError]. 30 // 31 // Unmarshal uses the inverse of the encodings that 32 // [Marshal] uses, allocating maps, slices, and pointers as necessary, 33 // with the following additional rules: 34 // 35 // To unmarshal JSON into a pointer, Unmarshal first handles the case of 36 // the JSON being the JSON literal null. In that case, Unmarshal sets 37 // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into 38 // the value pointed at by the pointer. If the pointer is nil, Unmarshal 39 // allocates a new value for it to point to. 40 // 41 // To unmarshal JSON into a value implementing [Unmarshaler], 42 // Unmarshal calls that value's [Unmarshaler.UnmarshalJSON] method, including 43 // when the input is a JSON null. 44 // Otherwise, if the value implements [encoding.TextUnmarshaler] 45 // and the input is a JSON quoted string, Unmarshal calls 46 // [encoding.TextUnmarshaler.UnmarshalText] with the unquoted form of the string. 47 // 48 // To unmarshal JSON into a struct, Unmarshal matches incoming object 49 // keys to the keys used by [Marshal] (either the struct field name or its tag), 50 // preferring an exact match but also accepting a case-insensitive match. By 51 // default, object keys which don't have a corresponding struct field are 52 // ignored (see [Decoder.DisallowUnknownFields] for an alternative). 53 // 54 // To unmarshal JSON into an interface value, 55 // Unmarshal stores one of these in the interface value: 56 // 57 // - bool, for JSON booleans 58 // - float64, for JSON numbers 59 // - string, for JSON strings 60 // - []any, for JSON arrays 61 // - map[string]any, for JSON objects 62 // - nil for JSON null 63 // 64 // To unmarshal a JSON array into a slice, Unmarshal resets the slice length 65 // to zero and then appends each element to the slice. 66 // As a special case, to unmarshal an empty JSON array into a slice, 67 // Unmarshal replaces the slice with a new empty slice. 68 // 69 // To unmarshal a JSON array into a Go array, Unmarshal decodes 70 // JSON array elements into corresponding Go array elements. 71 // If the Go array is smaller than the JSON array, 72 // the additional JSON array elements are discarded. 73 // If the JSON array is smaller than the Go array, 74 // the additional Go array elements are set to zero values. 75 // 76 // To unmarshal a JSON object into a map, Unmarshal first establishes a map to 77 // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal 78 // reuses the existing map, keeping existing entries. Unmarshal then stores 79 // key-value pairs from the JSON object into the map. The map's key type must 80 // either be any string type, an integer, or implement [encoding.TextUnmarshaler]. 81 // 82 // If the JSON-encoded data contain a syntax error, Unmarshal returns a [SyntaxError]. 83 // 84 // If a JSON value is not appropriate for a given target type, 85 // or if a JSON number overflows the target type, Unmarshal 86 // skips that field and completes the unmarshaling as best it can. 87 // If no more serious errors are encountered, Unmarshal returns 88 // an [UnmarshalTypeError] describing the earliest such error. In any 89 // case, it's not guaranteed that all the remaining fields following 90 // the problematic one will be unmarshaled into the target object. 91 // 92 // The JSON null value unmarshals into an interface, map, pointer, or slice 93 // by setting that Go value to nil. Because null is often used in JSON to mean 94 // “not present,” unmarshaling a JSON null into any other Go type has no effect 95 // on the value and produces no error. 96 // 97 // When unmarshaling quoted strings, invalid UTF-8 or 98 // invalid UTF-16 surrogate pairs are not treated as an error. 99 // Instead, they are replaced by the Unicode replacement 100 // character U+FFFD. 101 func Unmarshal(data []byte, v any) error { 102 // Check for well-formedness. 103 // Avoids filling out half a data structure 104 // before discovering a JSON syntax error. 105 var d decodeState 106 err := checkValid(data, &d.scan) 107 if err != nil { 108 return err 109 } 110 111 d.init(data) 112 return d.unmarshal(v) 113 } 114 115 // Unmarshaler is the interface implemented by types 116 // that can unmarshal a JSON description of themselves. 117 // The input can be assumed to be a valid encoding of 118 // a JSON value. UnmarshalJSON must copy the JSON data 119 // if it wishes to retain the data after returning. 120 // 121 // By convention, to approximate the behavior of [Unmarshal] itself, 122 // Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op. 123 type Unmarshaler interface { 124 UnmarshalJSON([]byte) error 125 } 126 127 // An UnmarshalTypeError describes a JSON value that was 128 // not appropriate for a value of a specific Go type. 129 type UnmarshalTypeError struct { 130 Value string // description of JSON value - "bool", "array", "number -5" 131 Type reflect.Type // type of Go value it could not be assigned to 132 Offset int64 // error occurred after reading Offset bytes 133 Struct string // name of the struct type containing the field 134 Field string // the full path from root node to the field, include embedded struct 135 } 136 137 func (e *UnmarshalTypeError) Error() string { 138 if e.Struct != "" || e.Field != "" { 139 return "json: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String() 140 } 141 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() 142 } 143 144 // An UnmarshalFieldError describes a JSON object key that 145 // led to an unexported (and therefore unwritable) struct field. 146 // 147 // Deprecated: No longer used; kept for compatibility. 148 type UnmarshalFieldError struct { 149 Key string 150 Type reflect.Type 151 Field reflect.StructField 152 } 153 154 func (e *UnmarshalFieldError) Error() string { 155 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() 156 } 157 158 // An InvalidUnmarshalError describes an invalid argument passed to [Unmarshal]. 159 // (The argument to [Unmarshal] must be a non-nil pointer.) 160 type InvalidUnmarshalError struct { 161 Type reflect.Type 162 } 163 164 func (e *InvalidUnmarshalError) Error() string { 165 if e.Type == nil { 166 return "json: Unmarshal(nil)" 167 } 168 169 if e.Type.Kind() != reflect.Pointer { 170 return "json: Unmarshal(non-pointer " + e.Type.String() + ")" 171 } 172 return "json: Unmarshal(nil " + e.Type.String() + ")" 173 } 174 175 func (d *decodeState) unmarshal(v any) error { 176 rv := reflect.ValueOf(v) 177 if rv.Kind() != reflect.Pointer || rv.IsNil() { 178 return &InvalidUnmarshalError{reflect.TypeOf(v)} 179 } 180 181 d.scan.reset() 182 d.scanWhile(scanSkipSpace) 183 // We decode rv not rv.Elem because the Unmarshaler interface 184 // test must be applied at the top level of the value. 185 err := d.value(rv) 186 if err != nil { 187 return d.addErrorContext(err) 188 } 189 return d.savedError 190 } 191 192 // A Number represents a JSON number literal. 193 type Number string 194 195 // String returns the literal text of the number. 196 func (n Number) String() string { return string(n) } 197 198 // Float64 returns the number as a float64. 199 func (n Number) Float64() (float64, error) { 200 return strconv.ParseFloat(string(n), 64) 201 } 202 203 // Int64 returns the number as an int64. 204 func (n Number) Int64() (int64, error) { 205 return strconv.ParseInt(string(n), 10, 64) 206 } 207 208 // An errorContext provides context for type errors during decoding. 209 type errorContext struct { 210 Struct reflect.Type 211 FieldStack []string 212 } 213 214 // decodeState represents the state while decoding a JSON value. 215 type decodeState struct { 216 data []byte 217 off int // next read offset in data 218 opcode int // last read result 219 scan scanner 220 errorContext *errorContext 221 savedError error 222 useNumber bool 223 disallowUnknownFields bool 224 } 225 226 // readIndex returns the position of the last byte read. 227 func (d *decodeState) readIndex() int { 228 return d.off - 1 229 } 230 231 // phasePanicMsg is used as a panic message when we end up with something that 232 // shouldn't happen. It can indicate a bug in the JSON decoder, or that 233 // something is editing the data slice while the decoder executes. 234 const phasePanicMsg = "JSON decoder out of sync - data changing underfoot?" 235 236 func (d *decodeState) init(data []byte) *decodeState { 237 d.data = data 238 d.off = 0 239 d.savedError = nil 240 if d.errorContext != nil { 241 d.errorContext.Struct = nil 242 // Reuse the allocated space for the FieldStack slice. 243 d.errorContext.FieldStack = d.errorContext.FieldStack[:0] 244 } 245 return d 246 } 247 248 // saveError saves the first err it is called with, 249 // for reporting at the end of the unmarshal. 250 func (d *decodeState) saveError(err error) { 251 if d.savedError == nil { 252 d.savedError = d.addErrorContext(err) 253 } 254 } 255 256 // addErrorContext returns a new error enhanced with information from d.errorContext 257 func (d *decodeState) addErrorContext(err error) error { 258 if d.errorContext != nil && (d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0) { 259 switch err := err.(type) { 260 case *UnmarshalTypeError: 261 err.Struct = d.errorContext.Struct.Name() 262 fieldStack := d.errorContext.FieldStack 263 if err.Field != "" { 264 fieldStack = append(fieldStack, err.Field) 265 } 266 err.Field = strings.Join(fieldStack, ".") 267 } 268 } 269 return err 270 } 271 272 // skip scans to the end of what was started. 273 func (d *decodeState) skip() { 274 s, data, i := &d.scan, d.data, d.off 275 depth := len(s.parseState) 276 for { 277 op := s.step(s, data[i]) 278 i++ 279 if len(s.parseState) < depth { 280 d.off = i 281 d.opcode = op 282 return 283 } 284 } 285 } 286 287 // scanNext processes the byte at d.data[d.off]. 288 func (d *decodeState) scanNext() { 289 if d.off < len(d.data) { 290 d.opcode = d.scan.step(&d.scan, d.data[d.off]) 291 d.off++ 292 } else { 293 d.opcode = d.scan.eof() 294 d.off = len(d.data) + 1 // mark processed EOF with len+1 295 } 296 } 297 298 // scanWhile processes bytes in d.data[d.off:] until it 299 // receives a scan code not equal to op. 300 func (d *decodeState) scanWhile(op int) { 301 s, data, i := &d.scan, d.data, d.off 302 for i < len(data) { 303 newOp := s.step(s, data[i]) 304 i++ 305 if newOp != op { 306 d.opcode = newOp 307 d.off = i 308 return 309 } 310 } 311 312 d.off = len(data) + 1 // mark processed EOF with len+1 313 d.opcode = d.scan.eof() 314 } 315 316 // rescanLiteral is similar to scanWhile(scanContinue), but it specialises the 317 // common case where we're decoding a literal. The decoder scans the input 318 // twice, once for syntax errors and to check the length of the value, and the 319 // second to perform the decoding. 320 // 321 // Only in the second step do we use decodeState to tokenize literals, so we 322 // know there aren't any syntax errors. We can take advantage of that knowledge, 323 // and scan a literal's bytes much more quickly. 324 func (d *decodeState) rescanLiteral() { 325 data, i := d.data, d.off 326 Switch: 327 switch data[i-1] { 328 case '"': // string 329 for ; i < len(data); i++ { 330 switch data[i] { 331 case '\\': 332 i++ // escaped char 333 case '"': 334 i++ // tokenize the closing quote too 335 break Switch 336 } 337 } 338 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-': // number 339 for ; i < len(data); i++ { 340 switch data[i] { 341 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 342 '.', 'e', 'E', '+', '-': 343 default: 344 break Switch 345 } 346 } 347 case 't': // true 348 i += len("rue") 349 case 'f': // false 350 i += len("alse") 351 case 'n': // null 352 i += len("ull") 353 } 354 if i < len(data) { 355 d.opcode = stateEndValue(&d.scan, data[i]) 356 } else { 357 d.opcode = scanEnd 358 } 359 d.off = i + 1 360 } 361 362 // value consumes a JSON value from d.data[d.off-1:], decoding into v, and 363 // reads the following byte ahead. If v is invalid, the value is discarded. 364 // The first byte of the value has been read already. 365 func (d *decodeState) value(v reflect.Value) error { 366 switch d.opcode { 367 default: 368 panic(phasePanicMsg) 369 370 case scanBeginArray: 371 if v.IsValid() { 372 if err := d.array(v); err != nil { 373 return err 374 } 375 } else { 376 d.skip() 377 } 378 d.scanNext() 379 380 case scanBeginObject: 381 if v.IsValid() { 382 if err := d.object(v); err != nil { 383 return err 384 } 385 } else { 386 d.skip() 387 } 388 d.scanNext() 389 390 case scanBeginLiteral: 391 // All bytes inside literal return scanContinue op code. 392 start := d.readIndex() 393 d.rescanLiteral() 394 395 if v.IsValid() { 396 if err := d.literalStore(d.data[start:d.readIndex()], v, false); err != nil { 397 return err 398 } 399 } 400 } 401 return nil 402 } 403 404 type unquotedValue struct{} 405 406 // valueQuoted is like value but decodes a 407 // quoted string literal or literal null into an interface value. 408 // If it finds anything other than a quoted string literal or null, 409 // valueQuoted returns unquotedValue{}. 410 func (d *decodeState) valueQuoted() any { 411 switch d.opcode { 412 default: 413 panic(phasePanicMsg) 414 415 case scanBeginArray, scanBeginObject: 416 d.skip() 417 d.scanNext() 418 419 case scanBeginLiteral: 420 v := d.literalInterface() 421 switch v.(type) { 422 case nil, string: 423 return v 424 } 425 } 426 return unquotedValue{} 427 } 428 429 // indirect walks down v allocating pointers as needed, 430 // until it gets to a non-pointer. 431 // If it encounters an Unmarshaler, indirect stops and returns that. 432 // If decodingNull is true, indirect stops at the first settable pointer so it 433 // can be set to nil. 434 func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) { 435 // Issue #24153 indicates that it is generally not a guaranteed property 436 // that you may round-trip a reflect.Value by calling Value.Addr().Elem() 437 // and expect the value to still be settable for values derived from 438 // unexported embedded struct fields. 439 // 440 // The logic below effectively does this when it first addresses the value 441 // (to satisfy possible pointer methods) and continues to dereference 442 // subsequent pointers as necessary. 443 // 444 // After the first round-trip, we set v back to the original value to 445 // preserve the original RW flags contained in reflect.Value. 446 v0 := v 447 haveAddr := false 448 449 // If v is a named type and is addressable, 450 // start with its address, so that if the type has pointer methods, 451 // we find them. 452 if v.Kind() != reflect.Pointer && v.Type().Name() != "" && v.CanAddr() { 453 haveAddr = true 454 v = v.Addr() 455 } 456 for { 457 // Load value from interface, but only if the result will be 458 // usefully addressable. 459 if v.Kind() == reflect.Interface && !v.IsNil() { 460 e := v.Elem() 461 if e.Kind() == reflect.Pointer && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Pointer) { 462 haveAddr = false 463 v = e 464 continue 465 } 466 } 467 468 if v.Kind() != reflect.Pointer { 469 break 470 } 471 472 if decodingNull && v.CanSet() { 473 break 474 } 475 476 // Prevent infinite loop if v is an interface pointing to its own address: 477 // var v any 478 // v = &v 479 if v.Elem().Kind() == reflect.Interface && v.Elem().Elem().Equal(v) { 480 v = v.Elem() 481 break 482 } 483 if v.IsNil() { 484 v.Set(reflect.New(v.Type().Elem())) 485 } 486 if v.Type().NumMethod() > 0 && v.CanInterface() { 487 if u, ok := v.Interface().(Unmarshaler); ok { 488 return u, nil, reflect.Value{} 489 } 490 if !decodingNull { 491 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok { 492 return nil, u, reflect.Value{} 493 } 494 } 495 } 496 497 if haveAddr { 498 v = v0 // restore original value after round-trip Value.Addr().Elem() 499 haveAddr = false 500 } else { 501 v = v.Elem() 502 } 503 } 504 return nil, nil, v 505 } 506 507 // array consumes an array from d.data[d.off-1:], decoding into v. 508 // The first byte of the array ('[') has been read already. 509 func (d *decodeState) array(v reflect.Value) error { 510 // Check for unmarshaler. 511 u, ut, pv := indirect(v, false) 512 if u != nil { 513 start := d.readIndex() 514 d.skip() 515 return u.UnmarshalJSON(d.data[start:d.off]) 516 } 517 if ut != nil { 518 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)}) 519 d.skip() 520 return nil 521 } 522 v = pv 523 524 // Check type of target. 525 switch v.Kind() { 526 case reflect.Interface: 527 if v.NumMethod() == 0 { 528 // Decoding into nil interface? Switch to non-reflect code. 529 ai := d.arrayInterface() 530 v.Set(reflect.ValueOf(ai)) 531 return nil 532 } 533 // Otherwise it's invalid. 534 fallthrough 535 default: 536 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)}) 537 d.skip() 538 return nil 539 case reflect.Array, reflect.Slice: 540 break 541 } 542 543 i := 0 544 for { 545 // Look ahead for ] - can only happen on first iteration. 546 d.scanWhile(scanSkipSpace) 547 if d.opcode == scanEndArray { 548 break 549 } 550 551 // Expand slice length, growing the slice if necessary. 552 if v.Kind() == reflect.Slice { 553 if i >= v.Cap() { 554 v.Grow(1) 555 } 556 if i >= v.Len() { 557 v.SetLen(i + 1) 558 } 559 } 560 561 if i < v.Len() { 562 // Decode into element. 563 if err := d.value(v.Index(i)); err != nil { 564 return err 565 } 566 } else { 567 // Ran out of fixed array: skip. 568 if err := d.value(reflect.Value{}); err != nil { 569 return err 570 } 571 } 572 i++ 573 574 // Next token must be , or ]. 575 if d.opcode == scanSkipSpace { 576 d.scanWhile(scanSkipSpace) 577 } 578 if d.opcode == scanEndArray { 579 break 580 } 581 if d.opcode != scanArrayValue { 582 panic(phasePanicMsg) 583 } 584 } 585 586 if i < v.Len() { 587 if v.Kind() == reflect.Array { 588 for ; i < v.Len(); i++ { 589 v.Index(i).SetZero() // zero remainder of array 590 } 591 } else { 592 v.SetLen(i) // truncate the slice 593 } 594 } 595 if i == 0 && v.Kind() == reflect.Slice { 596 v.Set(reflect.MakeSlice(v.Type(), 0, 0)) 597 } 598 return nil 599 } 600 601 var nullLiteral = []byte("null") 602 603 // SHIM(reflect): reflect.TypeFor[T]() reflect.T 604 var textUnmarshalerType = shims.TypeFor[encoding.TextUnmarshaler]() 605 606 // object consumes an object from d.data[d.off-1:], decoding into v. 607 // The first byte ('{') of the object has been read already. 608 func (d *decodeState) object(v reflect.Value) error { 609 // Check for unmarshaler. 610 u, ut, pv := indirect(v, false) 611 if u != nil { 612 start := d.readIndex() 613 d.skip() 614 return u.UnmarshalJSON(d.data[start:d.off]) 615 } 616 if ut != nil { 617 d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)}) 618 d.skip() 619 return nil 620 } 621 v = pv 622 t := v.Type() 623 624 // Decoding into nil interface? Switch to non-reflect code. 625 if v.Kind() == reflect.Interface && v.NumMethod() == 0 { 626 oi := d.objectInterface() 627 v.Set(reflect.ValueOf(oi)) 628 return nil 629 } 630 631 var fields structFields 632 633 // Check type of target: 634 // struct or 635 // map[T1]T2 where T1 is string, an integer type, 636 // or an encoding.TextUnmarshaler 637 switch v.Kind() { 638 case reflect.Map: 639 // Map key must either have string kind, have an integer kind, 640 // or be an encoding.TextUnmarshaler. 641 switch t.Key().Kind() { 642 case reflect.String, 643 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 644 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 645 default: 646 if !reflect.PointerTo(t.Key()).Implements(textUnmarshalerType) { 647 d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)}) 648 d.skip() 649 return nil 650 } 651 } 652 if v.IsNil() { 653 v.Set(reflect.MakeMap(t)) 654 } 655 case reflect.Struct: 656 fields = cachedTypeFields(t) 657 // ok 658 default: 659 d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)}) 660 d.skip() 661 return nil 662 } 663 664 var mapElem reflect.Value 665 var origErrorContext errorContext 666 if d.errorContext != nil { 667 origErrorContext = *d.errorContext 668 } 669 670 for { 671 // Read opening " of string key or closing }. 672 d.scanWhile(scanSkipSpace) 673 if d.opcode == scanEndObject { 674 // closing } - can only happen on first iteration. 675 break 676 } 677 if d.opcode != scanBeginLiteral { 678 panic(phasePanicMsg) 679 } 680 681 // Read key. 682 start := d.readIndex() 683 d.rescanLiteral() 684 item := d.data[start:d.readIndex()] 685 key, ok := unquoteBytes(item) 686 if !ok { 687 panic(phasePanicMsg) 688 } 689 690 // Figure out field corresponding to key. 691 var subv reflect.Value 692 destring := false // whether the value is wrapped in a string to be decoded first 693 694 if v.Kind() == reflect.Map { 695 elemType := t.Elem() 696 if !mapElem.IsValid() { 697 mapElem = reflect.New(elemType).Elem() 698 } else { 699 mapElem.SetZero() 700 } 701 subv = mapElem 702 } else { 703 f := fields.byExactName[string(key)] 704 if f == nil { 705 f = fields.byFoldedName[string(foldName(key))] 706 } 707 if f != nil { 708 subv = v 709 destring = f.quoted 710 if d.errorContext == nil { 711 d.errorContext = new(errorContext) 712 } 713 for i, ind := range f.index { 714 if subv.Kind() == reflect.Pointer { 715 if subv.IsNil() { 716 // If a struct embeds a pointer to an unexported type, 717 // it is not possible to set a newly allocated value 718 // since the field is unexported. 719 // 720 // See https://golang.org/issue/21357 721 if !subv.CanSet() { 722 d.saveError(fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", subv.Type().Elem())) 723 // Invalidate subv to ensure d.value(subv) skips over 724 // the JSON value without assigning it to subv. 725 subv = reflect.Value{} 726 destring = false 727 break 728 } 729 subv.Set(reflect.New(subv.Type().Elem())) 730 } 731 subv = subv.Elem() 732 } 733 if i < len(f.index)-1 { 734 d.errorContext.FieldStack = append( 735 d.errorContext.FieldStack, 736 subv.Type().Field(ind).Name, 737 ) 738 } 739 subv = subv.Field(ind) 740 } 741 d.errorContext.Struct = t 742 d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name) 743 } else if d.disallowUnknownFields { 744 d.saveError(fmt.Errorf("json: unknown field %q", key)) 745 } 746 } 747 748 // Read : before value. 749 if d.opcode == scanSkipSpace { 750 d.scanWhile(scanSkipSpace) 751 } 752 if d.opcode != scanObjectKey { 753 panic(phasePanicMsg) 754 } 755 d.scanWhile(scanSkipSpace) 756 757 if destring { 758 switch qv := d.valueQuoted().(type) { 759 case nil: 760 if err := d.literalStore(nullLiteral, subv, false); err != nil { 761 return err 762 } 763 case string: 764 if err := d.literalStore([]byte(qv), subv, true); err != nil { 765 return err 766 } 767 default: 768 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) 769 } 770 } else { 771 if err := d.value(subv); err != nil { 772 return err 773 } 774 } 775 776 // Write value back to map; 777 // if using struct, subv points into struct already. 778 if v.Kind() == reflect.Map { 779 kt := t.Key() 780 var kv reflect.Value 781 if reflect.PointerTo(kt).Implements(textUnmarshalerType) { 782 kv = reflect.New(kt) 783 if err := d.literalStore(item, kv, true); err != nil { 784 return err 785 } 786 kv = kv.Elem() 787 } else { 788 switch kt.Kind() { 789 case reflect.String: 790 kv = reflect.New(kt).Elem() 791 kv.SetString(string(key)) 792 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 793 s := string(key) 794 n, err := strconv.ParseInt(s, 10, 64) 795 // SHIM(reflect): reflect.Type.OverflowInt(int64) bool 796 okt := shims.OverflowableType{Type: kt} 797 if err != nil || okt.OverflowInt(n) { 798 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)}) 799 break 800 } 801 kv = reflect.New(kt).Elem() 802 kv.SetInt(n) 803 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 804 s := string(key) 805 n, err := strconv.ParseUint(s, 10, 64) 806 // SHIM(reflect): reflect.Type.OverflowUint(uint64) bool 807 okt := shims.OverflowableType{Type: kt} 808 if err != nil || okt.OverflowUint(n) { 809 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)}) 810 break 811 } 812 kv = reflect.New(kt).Elem() 813 kv.SetUint(n) 814 default: 815 panic("json: Unexpected key type") // should never occur 816 } 817 } 818 if kv.IsValid() { 819 v.SetMapIndex(kv, subv) 820 } 821 } 822 823 // Next token must be , or }. 824 if d.opcode == scanSkipSpace { 825 d.scanWhile(scanSkipSpace) 826 } 827 if d.errorContext != nil { 828 // Reset errorContext to its original state. 829 // Keep the same underlying array for FieldStack, to reuse the 830 // space and avoid unnecessary allocs. 831 d.errorContext.FieldStack = d.errorContext.FieldStack[:len(origErrorContext.FieldStack)] 832 d.errorContext.Struct = origErrorContext.Struct 833 } 834 if d.opcode == scanEndObject { 835 break 836 } 837 if d.opcode != scanObjectValue { 838 panic(phasePanicMsg) 839 } 840 } 841 return nil 842 } 843 844 // convertNumber converts the number literal s to a float64 or a Number 845 // depending on the setting of d.useNumber. 846 func (d *decodeState) convertNumber(s string) (any, error) { 847 if d.useNumber { 848 return Number(s), nil 849 } 850 f, err := strconv.ParseFloat(s, 64) 851 if err != nil { 852 // SHIM(reflect): reflect.TypeFor[T]() reflect.Type 853 return nil, &UnmarshalTypeError{Value: "number " + s, Type: shims.TypeFor[float64](), Offset: int64(d.off)} 854 } 855 return f, nil 856 } 857 858 // SHIM(reflect): TypeFor[T]() reflect.Type 859 var numberType = shims.TypeFor[Number]() 860 861 // literalStore decodes a literal stored in item into v. 862 // 863 // fromQuoted indicates whether this literal came from unwrapping a 864 // string from the ",string" struct tag option. this is used only to 865 // produce more helpful error messages. 866 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) error { 867 // Check for unmarshaler. 868 if len(item) == 0 { 869 // Empty string given. 870 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 871 return nil 872 } 873 isNull := item[0] == 'n' // null 874 u, ut, pv := indirect(v, isNull) 875 if u != nil { 876 return u.UnmarshalJSON(item) 877 } 878 if ut != nil { 879 if item[0] != '"' { 880 if fromQuoted { 881 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 882 return nil 883 } 884 val := "number" 885 switch item[0] { 886 case 'n': 887 val = "null" 888 case 't', 'f': 889 val = "bool" 890 } 891 d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())}) 892 return nil 893 } 894 s, ok := unquoteBytes(item) 895 if !ok { 896 if fromQuoted { 897 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 898 } 899 panic(phasePanicMsg) 900 } 901 return ut.UnmarshalText(s) 902 } 903 904 v = pv 905 906 switch c := item[0]; c { 907 case 'n': // null 908 // The main parser checks that only true and false can reach here, 909 // but if this was a quoted string input, it could be anything. 910 if fromQuoted && string(item) != "null" { 911 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 912 break 913 } 914 switch v.Kind() { 915 case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice: 916 v.SetZero() 917 // otherwise, ignore null for primitives/string 918 } 919 case 't', 'f': // true, false 920 value := item[0] == 't' 921 // The main parser checks that only true and false can reach here, 922 // but if this was a quoted string input, it could be anything. 923 if fromQuoted && string(item) != "true" && string(item) != "false" { 924 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 925 break 926 } 927 switch v.Kind() { 928 default: 929 if fromQuoted { 930 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 931 } else { 932 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())}) 933 } 934 case reflect.Bool: 935 v.SetBool(value) 936 case reflect.Interface: 937 if v.NumMethod() == 0 { 938 v.Set(reflect.ValueOf(value)) 939 } else { 940 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())}) 941 } 942 } 943 944 case '"': // string 945 s, ok := unquoteBytes(item) 946 if !ok { 947 if fromQuoted { 948 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 949 } 950 panic(phasePanicMsg) 951 } 952 switch v.Kind() { 953 default: 954 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())}) 955 case reflect.Slice: 956 if v.Type().Elem().Kind() != reflect.Uint8 { 957 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())}) 958 break 959 } 960 b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) 961 n, err := base64.StdEncoding.Decode(b, s) 962 if err != nil { 963 d.saveError(err) 964 break 965 } 966 v.SetBytes(b[:n]) 967 case reflect.String: 968 t := string(s) 969 if v.Type() == numberType && !isValidNumber(t) { 970 return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item) 971 } 972 v.SetString(t) 973 case reflect.Interface: 974 if v.NumMethod() == 0 { 975 v.Set(reflect.ValueOf(string(s))) 976 } else { 977 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())}) 978 } 979 } 980 981 default: // number 982 if c != '-' && (c < '0' || c > '9') { 983 if fromQuoted { 984 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 985 } 986 panic(phasePanicMsg) 987 } 988 switch v.Kind() { 989 default: 990 if v.Kind() == reflect.String && v.Type() == numberType { 991 // s must be a valid number, because it's 992 // already been tokenized. 993 v.SetString(string(item)) 994 break 995 } 996 if fromQuoted { 997 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 998 } 999 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())}) 1000 case reflect.Interface: 1001 n, err := d.convertNumber(string(item)) 1002 if err != nil { 1003 d.saveError(err) 1004 break 1005 } 1006 if v.NumMethod() != 0 { 1007 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())}) 1008 break 1009 } 1010 v.Set(reflect.ValueOf(n)) 1011 1012 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1013 n, err := strconv.ParseInt(string(item), 10, 64) 1014 if err != nil || v.OverflowInt(n) { 1015 d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())}) 1016 break 1017 } 1018 v.SetInt(n) 1019 1020 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 1021 n, err := strconv.ParseUint(string(item), 10, 64) 1022 if err != nil || v.OverflowUint(n) { 1023 d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())}) 1024 break 1025 } 1026 v.SetUint(n) 1027 1028 case reflect.Float32, reflect.Float64: 1029 n, err := strconv.ParseFloat(string(item), v.Type().Bits()) 1030 if err != nil || v.OverflowFloat(n) { 1031 d.saveError(&UnmarshalTypeError{Value: "number " + string(item), Type: v.Type(), Offset: int64(d.readIndex())}) 1032 break 1033 } 1034 v.SetFloat(n) 1035 } 1036 } 1037 return nil 1038 } 1039 1040 // The xxxInterface routines build up a value to be stored 1041 // in an empty interface. They are not strictly necessary, 1042 // but they avoid the weight of reflection in this common case. 1043 1044 // valueInterface is like value but returns any. 1045 func (d *decodeState) valueInterface() (val any) { 1046 switch d.opcode { 1047 default: 1048 panic(phasePanicMsg) 1049 case scanBeginArray: 1050 val = d.arrayInterface() 1051 d.scanNext() 1052 case scanBeginObject: 1053 val = d.objectInterface() 1054 d.scanNext() 1055 case scanBeginLiteral: 1056 val = d.literalInterface() 1057 } 1058 return 1059 } 1060 1061 // arrayInterface is like array but returns []any. 1062 func (d *decodeState) arrayInterface() []any { 1063 var v = make([]any, 0) 1064 for { 1065 // Look ahead for ] - can only happen on first iteration. 1066 d.scanWhile(scanSkipSpace) 1067 if d.opcode == scanEndArray { 1068 break 1069 } 1070 1071 v = append(v, d.valueInterface()) 1072 1073 // Next token must be , or ]. 1074 if d.opcode == scanSkipSpace { 1075 d.scanWhile(scanSkipSpace) 1076 } 1077 if d.opcode == scanEndArray { 1078 break 1079 } 1080 if d.opcode != scanArrayValue { 1081 panic(phasePanicMsg) 1082 } 1083 } 1084 return v 1085 } 1086 1087 // objectInterface is like object but returns map[string]any. 1088 func (d *decodeState) objectInterface() map[string]any { 1089 m := make(map[string]any) 1090 for { 1091 // Read opening " of string key or closing }. 1092 d.scanWhile(scanSkipSpace) 1093 if d.opcode == scanEndObject { 1094 // closing } - can only happen on first iteration. 1095 break 1096 } 1097 if d.opcode != scanBeginLiteral { 1098 panic(phasePanicMsg) 1099 } 1100 1101 // Read string key. 1102 start := d.readIndex() 1103 d.rescanLiteral() 1104 item := d.data[start:d.readIndex()] 1105 key, ok := unquote(item) 1106 if !ok { 1107 panic(phasePanicMsg) 1108 } 1109 1110 // Read : before value. 1111 if d.opcode == scanSkipSpace { 1112 d.scanWhile(scanSkipSpace) 1113 } 1114 if d.opcode != scanObjectKey { 1115 panic(phasePanicMsg) 1116 } 1117 d.scanWhile(scanSkipSpace) 1118 1119 // Read value. 1120 m[key] = d.valueInterface() 1121 1122 // Next token must be , or }. 1123 if d.opcode == scanSkipSpace { 1124 d.scanWhile(scanSkipSpace) 1125 } 1126 if d.opcode == scanEndObject { 1127 break 1128 } 1129 if d.opcode != scanObjectValue { 1130 panic(phasePanicMsg) 1131 } 1132 } 1133 return m 1134 } 1135 1136 // literalInterface consumes and returns a literal from d.data[d.off-1:] and 1137 // it reads the following byte ahead. The first byte of the literal has been 1138 // read already (that's how the caller knows it's a literal). 1139 func (d *decodeState) literalInterface() any { 1140 // All bytes inside literal return scanContinue op code. 1141 start := d.readIndex() 1142 d.rescanLiteral() 1143 1144 item := d.data[start:d.readIndex()] 1145 1146 switch c := item[0]; c { 1147 case 'n': // null 1148 return nil 1149 1150 case 't', 'f': // true, false 1151 return c == 't' 1152 1153 case '"': // string 1154 s, ok := unquote(item) 1155 if !ok { 1156 panic(phasePanicMsg) 1157 } 1158 return s 1159 1160 default: // number 1161 if c != '-' && (c < '0' || c > '9') { 1162 panic(phasePanicMsg) 1163 } 1164 n, err := d.convertNumber(string(item)) 1165 if err != nil { 1166 d.saveError(err) 1167 } 1168 return n 1169 } 1170 } 1171 1172 // getu4 decodes \uXXXX from the beginning of s, returning the hex value, 1173 // or it returns -1. 1174 func getu4(s []byte) rune { 1175 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { 1176 return -1 1177 } 1178 var r rune 1179 for _, c := range s[2:6] { 1180 switch { 1181 case '0' <= c && c <= '9': 1182 c = c - '0' 1183 case 'a' <= c && c <= 'f': 1184 c = c - 'a' + 10 1185 case 'A' <= c && c <= 'F': 1186 c = c - 'A' + 10 1187 default: 1188 return -1 1189 } 1190 r = r*16 + rune(c) 1191 } 1192 return r 1193 } 1194 1195 // unquote converts a quoted JSON string literal s into an actual string t. 1196 // The rules are different than for Go, so cannot use strconv.Unquote. 1197 func unquote(s []byte) (t string, ok bool) { 1198 s, ok = unquoteBytes(s) 1199 t = string(s) 1200 return 1201 } 1202 1203 // unquoteBytes should be an internal detail, 1204 // but widely used packages access it using linkname. 1205 // Notable members of the hall of shame include: 1206 // - github.com/bytedance/sonic 1207 // 1208 // Do not remove or change the type signature. 1209 // See go.dev/issue/67401. 1210 // 1211 //go:linkname unquoteBytes 1212 func unquoteBytes(s []byte) (t []byte, ok bool) { 1213 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 1214 return 1215 } 1216 s = s[1 : len(s)-1] 1217 1218 // Check for unusual characters. If there are none, 1219 // then no unquoting is needed, so return a slice of the 1220 // original bytes. 1221 r := 0 1222 for r < len(s) { 1223 c := s[r] 1224 if c == '\\' || c == '"' || c < ' ' { 1225 break 1226 } 1227 if c < utf8.RuneSelf { 1228 r++ 1229 continue 1230 } 1231 rr, size := utf8.DecodeRune(s[r:]) 1232 if rr == utf8.RuneError && size == 1 { 1233 break 1234 } 1235 r += size 1236 } 1237 if r == len(s) { 1238 return s, true 1239 } 1240 1241 b := make([]byte, len(s)+2*utf8.UTFMax) 1242 w := copy(b, s[0:r]) 1243 for r < len(s) { 1244 // Out of room? Can only happen if s is full of 1245 // malformed UTF-8 and we're replacing each 1246 // byte with RuneError. 1247 if w >= len(b)-2*utf8.UTFMax { 1248 nb := make([]byte, (len(b)+utf8.UTFMax)*2) 1249 copy(nb, b[0:w]) 1250 b = nb 1251 } 1252 switch c := s[r]; { 1253 case c == '\\': 1254 r++ 1255 if r >= len(s) { 1256 return 1257 } 1258 switch s[r] { 1259 default: 1260 return 1261 case '"', '\\', '/', '\'': 1262 b[w] = s[r] 1263 r++ 1264 w++ 1265 case 'b': 1266 b[w] = '\b' 1267 r++ 1268 w++ 1269 case 'f': 1270 b[w] = '\f' 1271 r++ 1272 w++ 1273 case 'n': 1274 b[w] = '\n' 1275 r++ 1276 w++ 1277 case 'r': 1278 b[w] = '\r' 1279 r++ 1280 w++ 1281 case 't': 1282 b[w] = '\t' 1283 r++ 1284 w++ 1285 case 'u': 1286 r-- 1287 rr := getu4(s[r:]) 1288 if rr < 0 { 1289 return 1290 } 1291 r += 6 1292 if utf16.IsSurrogate(rr) { 1293 rr1 := getu4(s[r:]) 1294 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { 1295 // A valid pair; consume. 1296 r += 6 1297 w += utf8.EncodeRune(b[w:], dec) 1298 break 1299 } 1300 // Invalid surrogate; fall back to replacement rune. 1301 rr = unicode.ReplacementChar 1302 } 1303 w += utf8.EncodeRune(b[w:], rr) 1304 } 1305 1306 // Quote, control characters are invalid. 1307 case c == '"', c < ' ': 1308 return 1309 1310 // ASCII 1311 case c < utf8.RuneSelf: 1312 b[w] = c 1313 r++ 1314 w++ 1315 1316 // Coerce to well-formed UTF-8. 1317 default: 1318 rr, size := utf8.DecodeRune(s[r:]) 1319 r += size 1320 w += utf8.EncodeRune(b[w:], rr) 1321 } 1322 } 1323 return b[0:w], true 1324 }