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