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