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