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