github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/encoding/json/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 } 221 222 // readIndex returns the position of the last byte read. 223 func (d *decodeState) readIndex() int { 224 return d.off - 1 225 } 226 227 // phasePanicMsg is used as a panic message when we end up with something that 228 // shouldn't happen. It can indicate a bug in the JSON decoder, or that 229 // something is editing the data slice while the decoder executes. 230 const phasePanicMsg = "JSON decoder out of sync - data changing underfoot?" 231 232 func (d *decodeState) init(data []byte) *decodeState { 233 d.data = data 234 d.off = 0 235 d.savedError = nil 236 if d.errorContext != nil { 237 d.errorContext.Struct = nil 238 // Reuse the allocated space for the FieldStack slice. 239 d.errorContext.FieldStack = d.errorContext.FieldStack[:0] 240 } 241 return d 242 } 243 244 // saveError saves the first err it is called with, 245 // for reporting at the end of the unmarshal. 246 func (d *decodeState) saveError(err error) { 247 if d.savedError == nil { 248 d.savedError = d.addErrorContext(err) 249 } 250 } 251 252 // addErrorContext returns a new error enhanced with information from d.errorContext 253 func (d *decodeState) addErrorContext(err error) error { 254 if d.errorContext != nil && (d.errorContext.Struct != nil || len(d.errorContext.FieldStack) > 0) { 255 switch err := err.(type) { 256 case *UnmarshalTypeError: 257 err.Struct = d.errorContext.Struct.Name() 258 err.Field = strings.Join(d.errorContext.FieldStack, ".") 259 } 260 } 261 return err 262 } 263 264 // skip scans to the end of what was started. 265 func (d *decodeState) skip() { 266 s, data, i := &d.scan, d.data, d.off 267 depth := len(s.parseState) 268 for { 269 op := s.step(s, data[i]) 270 i++ 271 if len(s.parseState) < depth { 272 d.off = i 273 d.opcode = op 274 return 275 } 276 } 277 } 278 279 // scanNext processes the byte at d.data[d.off]. 280 func (d *decodeState) scanNext() { 281 if d.off < len(d.data) { 282 d.opcode = d.scan.step(&d.scan, d.data[d.off]) 283 d.off++ 284 } else { 285 d.opcode = d.scan.eof() 286 d.off = len(d.data) + 1 // mark processed EOF with len+1 287 } 288 } 289 290 // scanWhile processes bytes in d.data[d.off:] until it 291 // receives a scan code not equal to op. 292 func (d *decodeState) scanWhile(op int) { 293 s, data, i := &d.scan, d.data, d.off 294 for i < len(data) { 295 newOp := s.step(s, data[i]) 296 i++ 297 if newOp != op { 298 d.opcode = newOp 299 d.off = i 300 return 301 } 302 } 303 304 d.off = len(data) + 1 // mark processed EOF with len+1 305 d.opcode = d.scan.eof() 306 } 307 308 // rescanLiteral is similar to scanWhile(scanContinue), but it specialises the 309 // common case where we're decoding a literal. The decoder scans the input 310 // twice, once for syntax errors and to check the length of the value, and the 311 // second to perform the decoding. 312 // 313 // Only in the second step do we use decodeState to tokenize literals, so we 314 // know there aren't any syntax errors. We can take advantage of that knowledge, 315 // and scan a literal's bytes much more quickly. 316 func (d *decodeState) rescanLiteral() { 317 data, i := d.data, d.off 318 Switch: 319 switch data[i-1] { 320 case '"': // string 321 for ; i < len(data); i++ { 322 switch data[i] { 323 case '\\': 324 i++ // escaped char 325 case '"': 326 i++ // tokenize the closing quote too 327 break Switch 328 } 329 } 330 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-': // number 331 for ; i < len(data); i++ { 332 switch data[i] { 333 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 334 '.', 'e', 'E', '+', '-': 335 default: 336 break Switch 337 } 338 } 339 case 't': // true 340 i += len("rue") 341 case 'f': // false 342 i += len("alse") 343 case 'n': // null 344 i += len("ull") 345 } 346 if i < len(data) { 347 d.opcode = stateEndValue(&d.scan, data[i]) 348 } else { 349 d.opcode = scanEnd 350 } 351 d.off = i + 1 352 } 353 354 // value consumes a JSON value from d.data[d.off-1:], decoding into v, and 355 // reads the following byte ahead. If v is invalid, the value is discarded. 356 // The first byte of the value has been read already. 357 func (d *decodeState) value(v reflect.Value) error { 358 switch d.opcode { 359 default: 360 panic(phasePanicMsg) 361 362 case scanBeginArray: 363 if v.IsValid() { 364 if err := d.array(v); err != nil { 365 return err 366 } 367 } else { 368 d.skip() 369 } 370 d.scanNext() 371 372 case scanBeginObject: 373 if v.IsValid() { 374 if err := d.object(v); err != nil { 375 return err 376 } 377 } else { 378 d.skip() 379 } 380 d.scanNext() 381 382 case scanBeginLiteral: 383 // All bytes inside literal return scanContinue op code. 384 start := d.readIndex() 385 d.rescanLiteral() 386 387 if v.IsValid() { 388 if err := d.literalStore(d.data[start:d.readIndex()], v, false); err != nil { 389 return err 390 } 391 } 392 } 393 return nil 394 } 395 396 type unquotedValue struct{} 397 398 // valueQuoted is like value but decodes a 399 // quoted string literal or literal null into an interface value. 400 // If it finds anything other than a quoted string literal or null, 401 // valueQuoted returns unquotedValue{}. 402 func (d *decodeState) valueQuoted() any { 403 switch d.opcode { 404 default: 405 panic(phasePanicMsg) 406 407 case scanBeginArray, scanBeginObject: 408 d.skip() 409 d.scanNext() 410 411 case scanBeginLiteral: 412 v := d.literalInterface() 413 switch v.(type) { 414 case nil, string: 415 return v 416 } 417 } 418 return unquotedValue{} 419 } 420 421 // indirect walks down v allocating pointers as needed, 422 // until it gets to a non-pointer. 423 // If it encounters an Unmarshaler, indirect stops and returns that. 424 // If decodingNull is true, indirect stops at the first settable pointer so it 425 // can be set to nil. 426 func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) { 427 // Issue #24153 indicates that it is generally not a guaranteed property 428 // that you may round-trip a reflect.Value by calling Value.Addr().Elem() 429 // and expect the value to still be settable for values derived from 430 // unexported embedded struct fields. 431 // 432 // The logic below effectively does this when it first addresses the value 433 // (to satisfy possible pointer methods) and continues to dereference 434 // subsequent pointers as necessary. 435 // 436 // After the first round-trip, we set v back to the original value to 437 // preserve the original RW flags contained in reflect.Value. 438 v0 := v 439 haveAddr := false 440 441 // If v is a named type and is addressable, 442 // start with its address, so that if the type has pointer methods, 443 // we find them. 444 if v.Kind() != reflect.Pointer && v.Type().Name() != "" && v.CanAddr() { 445 haveAddr = true 446 v = v.Addr() 447 } 448 for { 449 // Load value from interface, but only if the result will be 450 // usefully addressable. 451 if v.Kind() == reflect.Interface && !v.IsNil() { 452 e := v.Elem() 453 if e.Kind() == reflect.Pointer && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Pointer) { 454 haveAddr = false 455 v = e 456 continue 457 } 458 } 459 460 if v.Kind() != reflect.Pointer { 461 break 462 } 463 464 if decodingNull && v.CanSet() { 465 break 466 } 467 468 // Prevent infinite loop if v is an interface pointing to its own address: 469 // var v interface{} 470 // v = &v 471 if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v { 472 v = v.Elem() 473 break 474 } 475 if v.IsNil() { 476 v.Set(reflect.New(v.Type().Elem())) 477 } 478 if v.Type().NumMethod() > 0 && v.CanInterface() { 479 if u, ok := v.Interface().(Unmarshaler); ok { 480 return u, nil, reflect.Value{} 481 } 482 if !decodingNull { 483 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok { 484 return nil, u, reflect.Value{} 485 } 486 } 487 } 488 489 if haveAddr { 490 v = v0 // restore original value after round-trip Value.Addr().Elem() 491 haveAddr = false 492 } else { 493 v = v.Elem() 494 } 495 } 496 return nil, nil, v 497 } 498 499 // array consumes an array from d.data[d.off-1:], decoding into v. 500 // The first byte of the array ('[') has been read already. 501 func (d *decodeState) array(v reflect.Value) error { 502 // Check for unmarshaler. 503 u, ut, pv := indirect(v, false) 504 if u != nil { 505 start := d.readIndex() 506 d.skip() 507 return u.UnmarshalJSON(d.data[start:d.off]) 508 } 509 if ut != nil { 510 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)}) 511 d.skip() 512 return nil 513 } 514 v = pv 515 516 // Check type of target. 517 switch v.Kind() { 518 case reflect.Interface: 519 if v.NumMethod() == 0 { 520 // Decoding into nil interface? Switch to non-reflect code. 521 ai := d.arrayInterface() 522 v.Set(reflect.ValueOf(ai)) 523 return nil 524 } 525 // Otherwise it's invalid. 526 fallthrough 527 default: 528 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)}) 529 d.skip() 530 return nil 531 case reflect.Array, reflect.Slice: 532 break 533 } 534 535 i := 0 536 for { 537 // Look ahead for ] - can only happen on first iteration. 538 d.scanWhile(scanSkipSpace) 539 if d.opcode == scanEndArray { 540 break 541 } 542 543 // Get element of array, growing if necessary. 544 if v.Kind() == reflect.Slice { 545 // Grow slice if necessary 546 if i >= v.Cap() { 547 newcap := v.Cap() + v.Cap()/2 548 if newcap < 4 { 549 newcap = 4 550 } 551 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) 552 reflect.Copy(newv, v) 553 v.Set(newv) 554 } 555 if i >= v.Len() { 556 v.SetLen(i + 1) 557 } 558 } 559 560 if i < v.Len() { 561 // Decode into element. 562 if err := d.value(v.Index(i)); err != nil { 563 return err 564 } 565 } else { 566 // Ran out of fixed array: skip. 567 if err := d.value(reflect.Value{}); err != nil { 568 return err 569 } 570 } 571 i++ 572 573 // Next token must be , or ]. 574 if d.opcode == scanSkipSpace { 575 d.scanWhile(scanSkipSpace) 576 } 577 if d.opcode == scanEndArray { 578 break 579 } 580 if d.opcode != scanArrayValue { 581 panic(phasePanicMsg) 582 } 583 } 584 585 if i < v.Len() { 586 if v.Kind() == reflect.Array { 587 // Array. Zero the rest. 588 z := reflect.Zero(v.Type().Elem()) 589 for ; i < v.Len(); i++ { 590 v.Index(i).Set(z) 591 } 592 } else { 593 v.SetLen(i) 594 } 595 } 596 if i == 0 && v.Kind() == reflect.Slice { 597 v.Set(reflect.MakeSlice(v.Type(), 0, 0)) 598 } 599 return nil 600 } 601 602 var nullLiteral = []byte("null") 603 var textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() 604 605 // object consumes an object from d.data[d.off-1:], decoding into v. 606 // The first byte ('{') of the object has been read already. 607 func (d *decodeState) object(v reflect.Value) error { 608 // Check for unmarshaler. 609 u, ut, pv := indirect(v, false) 610 if u != nil { 611 start := d.readIndex() 612 d.skip() 613 return u.UnmarshalJSON(d.data[start:d.off]) 614 } 615 if ut != nil { 616 d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)}) 617 d.skip() 618 return nil 619 } 620 v = pv 621 t := v.Type() 622 623 // Decoding into nil interface? Switch to non-reflect code. 624 if v.Kind() == reflect.Interface && v.NumMethod() == 0 { 625 oi := d.objectInterface() 626 v.Set(reflect.ValueOf(oi)) 627 return nil 628 } 629 630 var fields structFields 631 632 // Check type of target: 633 // struct or 634 // map[T1]T2 where T1 is string, an integer type, 635 // or an encoding.TextUnmarshaler 636 switch v.Kind() { 637 case reflect.Map: 638 // Map key must either have string kind, have an integer kind, 639 // or be an encoding.TextUnmarshaler. 640 switch t.Key().Kind() { 641 case reflect.String, 642 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 643 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 644 default: 645 if !reflect.PointerTo(t.Key()).Implements(textUnmarshalerType) { 646 d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)}) 647 d.skip() 648 return nil 649 } 650 } 651 if v.IsNil() { 652 v.Set(reflect.MakeMap(t)) 653 } 654 case reflect.Struct: 655 fields = cachedTypeFields(t) 656 // ok 657 default: 658 d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)}) 659 d.skip() 660 return nil 661 } 662 663 var mapElem reflect.Value 664 var origErrorContext errorContext 665 if d.errorContext != nil { 666 origErrorContext = *d.errorContext 667 } 668 669 for { 670 // Read opening " of string key or closing }. 671 d.scanWhile(scanSkipSpace) 672 if d.opcode == scanEndObject { 673 // closing } - can only happen on first iteration. 674 break 675 } 676 if d.opcode != scanBeginLiteral { 677 panic(phasePanicMsg) 678 } 679 680 // Read key. 681 start := d.readIndex() 682 d.rescanLiteral() 683 item := d.data[start:d.readIndex()] 684 key, ok := unquoteBytes(item) 685 if !ok { 686 panic(phasePanicMsg) 687 } 688 689 // Figure out field corresponding to key. 690 var subv reflect.Value 691 destring := false // whether the value is wrapped in a string to be decoded first 692 693 if v.Kind() == reflect.Map { 694 elemType := t.Elem() 695 if !mapElem.IsValid() { 696 mapElem = reflect.New(elemType).Elem() 697 } else { 698 mapElem.Set(reflect.Zero(elemType)) 699 } 700 subv = mapElem 701 } else { 702 var f *field 703 if i, ok := fields.nameIndex[string(key)]; ok { 704 // Found an exact name match. 705 f = &fields.list[i] 706 } else { 707 // Fall back to the expensive case-insensitive 708 // linear search. 709 for i := range fields.list { 710 ff := &fields.list[i] 711 if ff.equalFold(ff.nameBytes, key) { 712 f = ff 713 break 714 } 715 } 716 } 717 if f != nil { 718 subv = v 719 destring = f.quoted 720 for _, i := range f.index { 721 if subv.Kind() == reflect.Pointer { 722 if subv.IsNil() { 723 // If a struct embeds a pointer to an unexported type, 724 // it is not possible to set a newly allocated value 725 // since the field is unexported. 726 // 727 // See https://golang.org/issue/21357 728 if !subv.CanSet() { 729 d.saveError(fmt.Errorf("json: cannot set embedded pointer to unexported struct: %v", subv.Type().Elem())) 730 // Invalidate subv to ensure d.value(subv) skips over 731 // the JSON value without assigning it to subv. 732 subv = reflect.Value{} 733 destring = false 734 break 735 } 736 subv.Set(reflect.New(subv.Type().Elem())) 737 } 738 subv = subv.Elem() 739 } 740 subv = subv.Field(i) 741 } 742 if d.errorContext == nil { 743 d.errorContext = new(errorContext) 744 } 745 d.errorContext.FieldStack = append(d.errorContext.FieldStack, f.name) 746 d.errorContext.Struct = t 747 } else if d.disallowUnknownFields { 748 d.saveError(fmt.Errorf("json: unknown field %q", key)) 749 } 750 } 751 752 // Read : before value. 753 if d.opcode == scanSkipSpace { 754 d.scanWhile(scanSkipSpace) 755 } 756 if d.opcode != scanObjectKey { 757 panic(phasePanicMsg) 758 } 759 d.scanWhile(scanSkipSpace) 760 761 if destring { 762 switch qv := d.valueQuoted().(type) { 763 case nil: 764 if err := d.literalStore(nullLiteral, subv, false); err != nil { 765 return err 766 } 767 case string: 768 if err := d.literalStore([]byte(qv), subv, true); err != nil { 769 return err 770 } 771 default: 772 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) 773 } 774 } else { 775 if err := d.value(subv); err != nil { 776 return err 777 } 778 } 779 780 // Write value back to map; 781 // if using struct, subv points into struct already. 782 if v.Kind() == reflect.Map { 783 kt := t.Key() 784 var kv reflect.Value 785 switch { 786 case reflect.PointerTo(kt).Implements(textUnmarshalerType): 787 kv = reflect.New(kt) 788 if err := d.literalStore(item, kv, true); err != nil { 789 return err 790 } 791 kv = kv.Elem() 792 case kt.Kind() == reflect.String: 793 kv = reflect.ValueOf(key).Convert(kt) 794 default: 795 switch kt.Kind() { 796 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 797 s := string(key) 798 n, err := strconv.ParseInt(s, 10, 64) 799 if err != nil || reflect.Zero(kt).OverflowInt(n) { 800 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)}) 801 break 802 } 803 kv = reflect.ValueOf(n).Convert(kt) 804 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 805 s := string(key) 806 n, err := strconv.ParseUint(s, 10, 64) 807 if err != nil || reflect.Zero(kt).OverflowUint(n) { 808 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)}) 809 break 810 } 811 kv = reflect.ValueOf(n).Convert(kt) 812 default: 813 panic("json: Unexpected key type") // should never occur 814 } 815 } 816 if kv.IsValid() { 817 v.SetMapIndex(kv, subv) 818 } 819 } 820 821 // Next token must be , or }. 822 if d.opcode == scanSkipSpace { 823 d.scanWhile(scanSkipSpace) 824 } 825 if d.errorContext != nil { 826 // Reset errorContext to its original state. 827 // Keep the same underlying array for FieldStack, to reuse the 828 // space and avoid unnecessary allocs. 829 d.errorContext.FieldStack = d.errorContext.FieldStack[:len(origErrorContext.FieldStack)] 830 d.errorContext.Struct = origErrorContext.Struct 831 } 832 if d.opcode == scanEndObject { 833 break 834 } 835 if d.opcode != scanObjectValue { 836 panic(phasePanicMsg) 837 } 838 } 839 return nil 840 } 841 842 // convertNumber converts the number literal s to a float64 or a Number 843 // depending on the setting of d.useNumber. 844 func (d *decodeState) convertNumber(s string) (any, error) { 845 if d.useNumber { 846 return Number(s), nil 847 } 848 f, err := strconv.ParseFloat(s, 64) 849 if err != nil { 850 return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeOf(0.0), Offset: int64(d.off)} 851 } 852 return f, nil 853 } 854 855 var numberType = reflect.TypeOf(Number("")) 856 857 // literalStore decodes a literal stored in item into v. 858 // 859 // fromQuoted indicates whether this literal came from unwrapping a 860 // string from the ",string" struct tag option. this is used only to 861 // produce more helpful error messages. 862 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) error { 863 // Check for unmarshaler. 864 if len(item) == 0 { 865 //Empty string given 866 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 867 return nil 868 } 869 isNull := item[0] == 'n' // null 870 u, ut, pv := indirect(v, isNull) 871 if u != nil { 872 return u.UnmarshalJSON(item) 873 } 874 if ut != nil { 875 if item[0] != '"' { 876 if fromQuoted { 877 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 878 return nil 879 } 880 val := "number" 881 switch item[0] { 882 case 'n': 883 val = "null" 884 case 't', 'f': 885 val = "bool" 886 } 887 d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.readIndex())}) 888 return nil 889 } 890 s, ok := unquoteBytes(item) 891 if !ok { 892 if fromQuoted { 893 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 894 } 895 panic(phasePanicMsg) 896 } 897 return ut.UnmarshalText(s) 898 } 899 900 v = pv 901 902 switch c := item[0]; c { 903 case 'n': // null 904 // The main parser checks that only true and false can reach here, 905 // but if this was a quoted string input, it could be anything. 906 if fromQuoted && string(item) != "null" { 907 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 908 break 909 } 910 switch v.Kind() { 911 case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice: 912 v.Set(reflect.Zero(v.Type())) 913 // otherwise, ignore null for primitives/string 914 } 915 case 't', 'f': // true, false 916 value := item[0] == 't' 917 // The main parser checks that only true and false can reach here, 918 // but if this was a quoted string input, it could be anything. 919 if fromQuoted && string(item) != "true" && string(item) != "false" { 920 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 921 break 922 } 923 switch v.Kind() { 924 default: 925 if fromQuoted { 926 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 927 } else { 928 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())}) 929 } 930 case reflect.Bool: 931 v.SetBool(value) 932 case reflect.Interface: 933 if v.NumMethod() == 0 { 934 v.Set(reflect.ValueOf(value)) 935 } else { 936 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.readIndex())}) 937 } 938 } 939 940 case '"': // string 941 s, ok := unquoteBytes(item) 942 if !ok { 943 if fromQuoted { 944 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 945 } 946 panic(phasePanicMsg) 947 } 948 switch v.Kind() { 949 default: 950 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())}) 951 case reflect.Slice: 952 if v.Type().Elem().Kind() != reflect.Uint8 { 953 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())}) 954 break 955 } 956 b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) 957 n, err := base64.StdEncoding.Decode(b, s) 958 if err != nil { 959 d.saveError(err) 960 break 961 } 962 v.SetBytes(b[:n]) 963 case reflect.String: 964 if v.Type() == numberType && !isValidNumber(string(s)) { 965 return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item) 966 } 967 v.SetString(string(s)) 968 case reflect.Interface: 969 if v.NumMethod() == 0 { 970 v.Set(reflect.ValueOf(string(s))) 971 } else { 972 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.readIndex())}) 973 } 974 } 975 976 default: // number 977 if c != '-' && (c < '0' || c > '9') { 978 if fromQuoted { 979 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 980 } 981 panic(phasePanicMsg) 982 } 983 s := string(item) 984 switch v.Kind() { 985 default: 986 if v.Kind() == reflect.String && v.Type() == numberType { 987 // s must be a valid number, because it's 988 // already been tokenized. 989 v.SetString(s) 990 break 991 } 992 if fromQuoted { 993 return fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()) 994 } 995 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())}) 996 case reflect.Interface: 997 n, err := d.convertNumber(s) 998 if err != nil { 999 d.saveError(err) 1000 break 1001 } 1002 if v.NumMethod() != 0 { 1003 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.readIndex())}) 1004 break 1005 } 1006 v.Set(reflect.ValueOf(n)) 1007 1008 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1009 n, err := strconv.ParseInt(s, 10, 64) 1010 if err != nil || v.OverflowInt(n) { 1011 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())}) 1012 break 1013 } 1014 v.SetInt(n) 1015 1016 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 1017 n, err := strconv.ParseUint(s, 10, 64) 1018 if err != nil || v.OverflowUint(n) { 1019 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())}) 1020 break 1021 } 1022 v.SetUint(n) 1023 1024 case reflect.Float32, reflect.Float64: 1025 n, err := strconv.ParseFloat(s, v.Type().Bits()) 1026 if err != nil || v.OverflowFloat(n) { 1027 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.readIndex())}) 1028 break 1029 } 1030 v.SetFloat(n) 1031 } 1032 } 1033 return nil 1034 } 1035 1036 // The xxxInterface routines build up a value to be stored 1037 // in an empty interface. They are not strictly necessary, 1038 // but they avoid the weight of reflection in this common case. 1039 1040 // valueInterface is like value but returns interface{} 1041 func (d *decodeState) valueInterface() (val any) { 1042 switch d.opcode { 1043 default: 1044 panic(phasePanicMsg) 1045 case scanBeginArray: 1046 val = d.arrayInterface() 1047 d.scanNext() 1048 case scanBeginObject: 1049 val = d.objectInterface() 1050 d.scanNext() 1051 case scanBeginLiteral: 1052 val = d.literalInterface() 1053 } 1054 return 1055 } 1056 1057 // arrayInterface is like array but returns []interface{}. 1058 func (d *decodeState) arrayInterface() []any { 1059 var v = make([]any, 0) 1060 for { 1061 // Look ahead for ] - can only happen on first iteration. 1062 d.scanWhile(scanSkipSpace) 1063 if d.opcode == scanEndArray { 1064 break 1065 } 1066 1067 v = append(v, d.valueInterface()) 1068 1069 // Next token must be , or ]. 1070 if d.opcode == scanSkipSpace { 1071 d.scanWhile(scanSkipSpace) 1072 } 1073 if d.opcode == scanEndArray { 1074 break 1075 } 1076 if d.opcode != scanArrayValue { 1077 panic(phasePanicMsg) 1078 } 1079 } 1080 return v 1081 } 1082 1083 // objectInterface is like object but returns map[string]interface{}. 1084 func (d *decodeState) objectInterface() map[string]any { 1085 m := make(map[string]any) 1086 for { 1087 // Read opening " of string key or closing }. 1088 d.scanWhile(scanSkipSpace) 1089 if d.opcode == scanEndObject { 1090 // closing } - can only happen on first iteration. 1091 break 1092 } 1093 if d.opcode != scanBeginLiteral { 1094 panic(phasePanicMsg) 1095 } 1096 1097 // Read string key. 1098 start := d.readIndex() 1099 d.rescanLiteral() 1100 item := d.data[start:d.readIndex()] 1101 key, ok := unquote(item) 1102 if !ok { 1103 panic(phasePanicMsg) 1104 } 1105 1106 // Read : before value. 1107 if d.opcode == scanSkipSpace { 1108 d.scanWhile(scanSkipSpace) 1109 } 1110 if d.opcode != scanObjectKey { 1111 panic(phasePanicMsg) 1112 } 1113 d.scanWhile(scanSkipSpace) 1114 1115 // Read value. 1116 m[key] = d.valueInterface() 1117 1118 // Next token must be , or }. 1119 if d.opcode == scanSkipSpace { 1120 d.scanWhile(scanSkipSpace) 1121 } 1122 if d.opcode == scanEndObject { 1123 break 1124 } 1125 if d.opcode != scanObjectValue { 1126 panic(phasePanicMsg) 1127 } 1128 } 1129 return m 1130 } 1131 1132 // literalInterface consumes and returns a literal from d.data[d.off-1:] and 1133 // it reads the following byte ahead. The first byte of the literal has been 1134 // read already (that's how the caller knows it's a literal). 1135 func (d *decodeState) literalInterface() any { 1136 // All bytes inside literal return scanContinue op code. 1137 start := d.readIndex() 1138 d.rescanLiteral() 1139 1140 item := d.data[start:d.readIndex()] 1141 1142 switch c := item[0]; c { 1143 case 'n': // null 1144 return nil 1145 1146 case 't', 'f': // true, false 1147 return c == 't' 1148 1149 case '"': // string 1150 s, ok := unquote(item) 1151 if !ok { 1152 panic(phasePanicMsg) 1153 } 1154 return s 1155 1156 default: // number 1157 if c != '-' && (c < '0' || c > '9') { 1158 panic(phasePanicMsg) 1159 } 1160 n, err := d.convertNumber(string(item)) 1161 if err != nil { 1162 d.saveError(err) 1163 } 1164 return n 1165 } 1166 } 1167 1168 // getu4 decodes \uXXXX from the beginning of s, returning the hex value, 1169 // or it returns -1. 1170 func getu4(s []byte) rune { 1171 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { 1172 return -1 1173 } 1174 var r rune 1175 for _, c := range s[2:6] { 1176 switch { 1177 case '0' <= c && c <= '9': 1178 c = c - '0' 1179 case 'a' <= c && c <= 'f': 1180 c = c - 'a' + 10 1181 case 'A' <= c && c <= 'F': 1182 c = c - 'A' + 10 1183 default: 1184 return -1 1185 } 1186 r = r*16 + rune(c) 1187 } 1188 return r 1189 } 1190 1191 // unquote converts a quoted JSON string literal s into an actual string t. 1192 // The rules are different than for Go, so cannot use strconv.Unquote. 1193 func unquote(s []byte) (t string, ok bool) { 1194 s, ok = unquoteBytes(s) 1195 t = string(s) 1196 return 1197 } 1198 1199 func unquoteBytes(s []byte) (t []byte, ok bool) { 1200 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 1201 return 1202 } 1203 s = s[1 : len(s)-1] 1204 1205 // Check for unusual characters. If there are none, 1206 // then no unquoting is needed, so return a slice of the 1207 // original bytes. 1208 r := 0 1209 for r < len(s) { 1210 c := s[r] 1211 if c == '\\' || c == '"' || c < ' ' { 1212 break 1213 } 1214 if c < utf8.RuneSelf { 1215 r++ 1216 continue 1217 } 1218 rr, size := utf8.DecodeRune(s[r:]) 1219 if rr == utf8.RuneError && size == 1 { 1220 break 1221 } 1222 r += size 1223 } 1224 if r == len(s) { 1225 return s, true 1226 } 1227 1228 b := make([]byte, len(s)+2*utf8.UTFMax) 1229 w := copy(b, s[0:r]) 1230 for r < len(s) { 1231 // Out of room? Can only happen if s is full of 1232 // malformed UTF-8 and we're replacing each 1233 // byte with RuneError. 1234 if w >= len(b)-2*utf8.UTFMax { 1235 nb := make([]byte, (len(b)+utf8.UTFMax)*2) 1236 copy(nb, b[0:w]) 1237 b = nb 1238 } 1239 switch c := s[r]; { 1240 case c == '\\': 1241 r++ 1242 if r >= len(s) { 1243 return 1244 } 1245 switch s[r] { 1246 default: 1247 return 1248 case '"', '\\', '/', '\'': 1249 b[w] = s[r] 1250 r++ 1251 w++ 1252 case 'b': 1253 b[w] = '\b' 1254 r++ 1255 w++ 1256 case 'f': 1257 b[w] = '\f' 1258 r++ 1259 w++ 1260 case 'n': 1261 b[w] = '\n' 1262 r++ 1263 w++ 1264 case 'r': 1265 b[w] = '\r' 1266 r++ 1267 w++ 1268 case 't': 1269 b[w] = '\t' 1270 r++ 1271 w++ 1272 case 'u': 1273 r-- 1274 rr := getu4(s[r:]) 1275 if rr < 0 { 1276 return 1277 } 1278 r += 6 1279 if utf16.IsSurrogate(rr) { 1280 rr1 := getu4(s[r:]) 1281 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { 1282 // A valid pair; consume. 1283 r += 6 1284 w += utf8.EncodeRune(b[w:], dec) 1285 break 1286 } 1287 // Invalid surrogate; fall back to replacement rune. 1288 rr = unicode.ReplacementChar 1289 } 1290 w += utf8.EncodeRune(b[w:], rr) 1291 } 1292 1293 // Quote, control characters are invalid. 1294 case c == '"', c < ' ': 1295 return 1296 1297 // ASCII 1298 case c < utf8.RuneSelf: 1299 b[w] = c 1300 r++ 1301 w++ 1302 1303 // Coerce to well-formed UTF-8. 1304 default: 1305 rr, size := utf8.DecodeRune(s[r:]) 1306 r += size 1307 w += utf8.EncodeRune(b[w:], rr) 1308 } 1309 } 1310 return b[0:w], true 1311 }