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