github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/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 "runtime" 18 "strconv" 19 "unicode" 20 "unicode/utf16" 21 "unicode/utf8" 22 ) 23 24 // Unmarshal parses the JSON-encoded data and stores the result 25 // in the value pointed to by v. 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 struct, Unmarshal matches incoming object 38 // keys to the keys used by Marshal (either the struct field name or its tag), 39 // preferring an exact match but also accepting a case-insensitive match. 40 // 41 // To unmarshal JSON into an interface value, 42 // Unmarshal stores one of these in the interface value: 43 // 44 // bool, for JSON booleans 45 // float64, for JSON numbers 46 // string, for JSON strings 47 // []interface{}, for JSON arrays 48 // map[string]interface{}, for JSON objects 49 // nil for JSON null 50 // 51 // To unmarshal a JSON array into a slice, Unmarshal resets the slice length 52 // to zero and then appends each element to the slice. 53 // As a special case, to unmarshal an empty JSON array into a slice, 54 // Unmarshal replaces the slice with a new empty slice. 55 // 56 // To unmarshal a JSON array into a Go array, Unmarshal decodes 57 // JSON array elements into corresponding Go array elements. 58 // If the Go array is smaller than the JSON array, 59 // the additional JSON array elements are discarded. 60 // If the JSON array is smaller than the Go array, 61 // the additional Go array elements are set to zero values. 62 // 63 // To unmarshal a JSON object into a string-keyed map, Unmarshal first 64 // establishes a map to use, If the map is nil, Unmarshal allocates a new map. 65 // Otherwise Unmarshal reuses the existing map, keeping existing entries. 66 // Unmarshal then stores key-value pairs from the JSON object into the map. 67 // 68 // If a JSON value is not appropriate for a given target type, 69 // or if a JSON number overflows the target type, Unmarshal 70 // skips that field and completes the unmarshaling as best it can. 71 // If no more serious errors are encountered, Unmarshal returns 72 // an UnmarshalTypeError describing the earliest such error. 73 // 74 // The JSON null value unmarshals into an interface, map, pointer, or slice 75 // by setting that Go value to nil. Because null is often used in JSON to mean 76 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect 77 // on the value and produces no error. 78 // 79 // When unmarshaling quoted strings, invalid UTF-8 or 80 // invalid UTF-16 surrogate pairs are not treated as an error. 81 // Instead, they are replaced by the Unicode replacement 82 // character U+FFFD. 83 // 84 func Unmarshal(data []byte, v interface{}) error { 85 // Check for well-formedness. 86 // Avoids filling out half a data structure 87 // before discovering a JSON syntax error. 88 var d decodeState 89 err := checkValid(data, &d.scan) 90 if err != nil { 91 return err 92 } 93 94 d.init(data) 95 return d.unmarshal(v) 96 } 97 98 // Unmarshaler is the interface implemented by objects 99 // that can unmarshal a JSON description of themselves. 100 // The input can be assumed to be a valid encoding of 101 // a JSON value. UnmarshalJSON must copy the JSON data 102 // if it wishes to retain the data after returning. 103 type Unmarshaler interface { 104 UnmarshalJSON([]byte) error 105 } 106 107 // An UnmarshalTypeError describes a JSON value that was 108 // not appropriate for a value of a specific Go type. 109 type UnmarshalTypeError struct { 110 Value string // description of JSON value - "bool", "array", "number -5" 111 Type reflect.Type // type of Go value it could not be assigned to 112 Offset int64 // error occurred after reading Offset bytes 113 } 114 115 func (e *UnmarshalTypeError) Error() string { 116 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() 117 } 118 119 // An UnmarshalFieldError describes a JSON object key that 120 // led to an unexported (and therefore unwritable) struct field. 121 // (No longer used; kept for compatibility.) 122 type UnmarshalFieldError struct { 123 Key string 124 Type reflect.Type 125 Field reflect.StructField 126 } 127 128 func (e *UnmarshalFieldError) Error() string { 129 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() 130 } 131 132 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. 133 // (The argument to Unmarshal must be a non-nil pointer.) 134 type InvalidUnmarshalError struct { 135 Type reflect.Type 136 } 137 138 func (e *InvalidUnmarshalError) Error() string { 139 if e.Type == nil { 140 return "json: Unmarshal(nil)" 141 } 142 143 if e.Type.Kind() != reflect.Ptr { 144 return "json: Unmarshal(non-pointer " + e.Type.String() + ")" 145 } 146 return "json: Unmarshal(nil " + e.Type.String() + ")" 147 } 148 149 func (d *decodeState) unmarshal(v interface{}) (err error) { 150 defer func() { 151 if r := recover(); r != nil { 152 if _, ok := r.(runtime.Error); ok { 153 panic(r) 154 } 155 err = r.(error) 156 } 157 }() 158 159 rv := reflect.ValueOf(v) 160 if rv.Kind() != reflect.Ptr || rv.IsNil() { 161 return &InvalidUnmarshalError{reflect.TypeOf(v)} 162 } 163 164 d.scan.reset() 165 // We decode rv not rv.Elem because the Unmarshaler interface 166 // test must be applied at the top level of the value. 167 d.value(rv) 168 return d.savedError 169 } 170 171 // A Number represents a JSON number literal. 172 type Number string 173 174 // String returns the literal text of the number. 175 func (n Number) String() string { return string(n) } 176 177 // Float64 returns the number as a float64. 178 func (n Number) Float64() (float64, error) { 179 return strconv.ParseFloat(string(n), 64) 180 } 181 182 // Int64 returns the number as an int64. 183 func (n Number) Int64() (int64, error) { 184 return strconv.ParseInt(string(n), 10, 64) 185 } 186 187 // isValidNumber reports whether s is a valid JSON number literal. 188 func isValidNumber(s string) bool { 189 // This function implements the JSON numbers grammar. 190 // See https://tools.ietf.org/html/rfc7159#section-6 191 // and http://json.org/number.gif 192 193 if s == "" { 194 return false 195 } 196 197 // Optional - 198 if s[0] == '-' { 199 s = s[1:] 200 if s == "" { 201 return false 202 } 203 } 204 205 // Digits 206 switch { 207 default: 208 return false 209 210 case s[0] == '0': 211 s = s[1:] 212 213 case '1' <= s[0] && s[0] <= '9': 214 s = s[1:] 215 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 216 s = s[1:] 217 } 218 } 219 220 // . followed by 1 or more digits. 221 if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { 222 s = s[2:] 223 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 224 s = s[1:] 225 } 226 } 227 228 // e or E followed by an optional - or + and 229 // 1 or more digits. 230 if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { 231 s = s[1:] 232 if s[0] == '+' || s[0] == '-' { 233 s = s[1:] 234 if s == "" { 235 return false 236 } 237 } 238 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 239 s = s[1:] 240 } 241 } 242 243 // Make sure we are at the end. 244 return s == "" 245 } 246 247 // decodeState represents the state while decoding a JSON value. 248 type decodeState struct { 249 data []byte 250 off int // read offset in data 251 scan scanner 252 nextscan scanner // for calls to nextValue 253 savedError error 254 useNumber bool 255 } 256 257 // errPhase is used for errors that should not happen unless 258 // there is a bug in the JSON decoder or something is editing 259 // the data slice while the decoder executes. 260 var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?") 261 262 func (d *decodeState) init(data []byte) *decodeState { 263 d.data = data 264 d.off = 0 265 d.savedError = nil 266 return d 267 } 268 269 // error aborts the decoding by panicking with err. 270 func (d *decodeState) error(err error) { 271 panic(err) 272 } 273 274 // saveError saves the first err it is called with, 275 // for reporting at the end of the unmarshal. 276 func (d *decodeState) saveError(err error) { 277 if d.savedError == nil { 278 d.savedError = err 279 } 280 } 281 282 // next cuts off and returns the next full JSON value in d.data[d.off:]. 283 // The next value is known to be an object or array, not a literal. 284 func (d *decodeState) next() []byte { 285 c := d.data[d.off] 286 item, rest, err := nextValue(d.data[d.off:], &d.nextscan) 287 if err != nil { 288 d.error(err) 289 } 290 d.off = len(d.data) - len(rest) 291 292 // Our scanner has seen the opening brace/bracket 293 // and thinks we're still in the middle of the object. 294 // invent a closing brace/bracket to get it out. 295 if c == '{' { 296 d.scan.step(&d.scan, '}') 297 } else { 298 d.scan.step(&d.scan, ']') 299 } 300 301 return item 302 } 303 304 // scanWhile processes bytes in d.data[d.off:] until it 305 // receives a scan code not equal to op. 306 // It updates d.off and returns the new scan code. 307 func (d *decodeState) scanWhile(op int) int { 308 var newOp int 309 for { 310 if d.off >= len(d.data) { 311 newOp = d.scan.eof() 312 d.off = len(d.data) + 1 // mark processed EOF with len+1 313 } else { 314 c := d.data[d.off] 315 d.off++ 316 newOp = d.scan.step(&d.scan, c) 317 } 318 if newOp != op { 319 break 320 } 321 } 322 return newOp 323 } 324 325 // value decodes a JSON value from d.data[d.off:] into the value. 326 // it updates d.off to point past the decoded value. 327 func (d *decodeState) value(v reflect.Value) { 328 if !v.IsValid() { 329 _, rest, err := nextValue(d.data[d.off:], &d.nextscan) 330 if err != nil { 331 d.error(err) 332 } 333 d.off = len(d.data) - len(rest) 334 335 // d.scan thinks we're still at the beginning of the item. 336 // Feed in an empty string - the shortest, simplest value - 337 // so that it knows we got to the end of the value. 338 if d.scan.redo { 339 // rewind. 340 d.scan.redo = false 341 d.scan.step = stateBeginValue 342 } 343 d.scan.step(&d.scan, '"') 344 d.scan.step(&d.scan, '"') 345 346 n := len(d.scan.parseState) 347 if n > 0 && d.scan.parseState[n-1] == parseObjectKey { 348 // d.scan thinks we just read an object key; finish the object 349 d.scan.step(&d.scan, ':') 350 d.scan.step(&d.scan, '"') 351 d.scan.step(&d.scan, '"') 352 d.scan.step(&d.scan, '}') 353 } 354 355 return 356 } 357 358 switch op := d.scanWhile(scanSkipSpace); op { 359 default: 360 d.error(errPhase) 361 362 case scanBeginArray: 363 d.array(v) 364 365 case scanBeginObject: 366 d.object(v) 367 368 case scanBeginLiteral: 369 d.literal(v) 370 } 371 } 372 373 type unquotedValue struct{} 374 375 // valueQuoted is like value but decodes a 376 // quoted string literal or literal null into an interface value. 377 // If it finds anything other than a quoted string literal or null, 378 // valueQuoted returns unquotedValue{}. 379 func (d *decodeState) valueQuoted() interface{} { 380 switch op := d.scanWhile(scanSkipSpace); op { 381 default: 382 d.error(errPhase) 383 384 case scanBeginArray: 385 d.array(reflect.Value{}) 386 387 case scanBeginObject: 388 d.object(reflect.Value{}) 389 390 case scanBeginLiteral: 391 switch v := d.literalInterface().(type) { 392 case nil, string: 393 return v 394 } 395 } 396 return unquotedValue{} 397 } 398 399 // indirect walks down v allocating pointers as needed, 400 // until it gets to a non-pointer. 401 // if it encounters an Unmarshaler, indirect stops and returns that. 402 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil. 403 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) { 404 // If v is a named type and is addressable, 405 // start with its address, so that if the type has pointer methods, 406 // we find them. 407 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() { 408 v = v.Addr() 409 } 410 for { 411 // Load value from interface, but only if the result will be 412 // usefully addressable. 413 if v.Kind() == reflect.Interface && !v.IsNil() { 414 e := v.Elem() 415 if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) { 416 v = e 417 continue 418 } 419 } 420 421 if v.Kind() != reflect.Ptr { 422 break 423 } 424 425 if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() { 426 break 427 } 428 if v.IsNil() { 429 v.Set(reflect.New(v.Type().Elem())) 430 } 431 if v.Type().NumMethod() > 0 { 432 if u, ok := v.Interface().(Unmarshaler); ok { 433 return u, nil, reflect.Value{} 434 } 435 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok { 436 return nil, u, reflect.Value{} 437 } 438 } 439 v = v.Elem() 440 } 441 return nil, nil, v 442 } 443 444 // array consumes an array from d.data[d.off-1:], decoding into the value v. 445 // the first byte of the array ('[') has been read already. 446 func (d *decodeState) array(v reflect.Value) { 447 // Check for unmarshaler. 448 u, ut, pv := d.indirect(v, false) 449 if u != nil { 450 d.off-- 451 err := u.UnmarshalJSON(d.next()) 452 if err != nil { 453 d.error(err) 454 } 455 return 456 } 457 if ut != nil { 458 d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)}) 459 d.off-- 460 d.next() 461 return 462 } 463 464 v = pv 465 466 // Check type of target. 467 switch v.Kind() { 468 case reflect.Interface: 469 if v.NumMethod() == 0 { 470 // Decoding into nil interface? Switch to non-reflect code. 471 v.Set(reflect.ValueOf(d.arrayInterface())) 472 return 473 } 474 // Otherwise it's invalid. 475 fallthrough 476 default: 477 d.saveError(&UnmarshalTypeError{"array", v.Type(), int64(d.off)}) 478 d.off-- 479 d.next() 480 return 481 case reflect.Array: 482 case reflect.Slice: 483 break 484 } 485 486 i := 0 487 for { 488 // Look ahead for ] - can only happen on first iteration. 489 op := d.scanWhile(scanSkipSpace) 490 if op == scanEndArray { 491 break 492 } 493 494 // Back up so d.value can have the byte we just read. 495 d.off-- 496 d.scan.undo(op) 497 498 // Get element of array, growing if necessary. 499 if v.Kind() == reflect.Slice { 500 // Grow slice if necessary 501 if i >= v.Cap() { 502 newcap := v.Cap() + v.Cap()/2 503 if newcap < 4 { 504 newcap = 4 505 } 506 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) 507 reflect.Copy(newv, v) 508 v.Set(newv) 509 } 510 if i >= v.Len() { 511 v.SetLen(i + 1) 512 } 513 } 514 515 if i < v.Len() { 516 // Decode into element. 517 d.value(v.Index(i)) 518 } else { 519 // Ran out of fixed array: skip. 520 d.value(reflect.Value{}) 521 } 522 i++ 523 524 // Next token must be , or ]. 525 op = d.scanWhile(scanSkipSpace) 526 if op == scanEndArray { 527 break 528 } 529 if op != scanArrayValue { 530 d.error(errPhase) 531 } 532 } 533 534 if i < v.Len() { 535 if v.Kind() == reflect.Array { 536 // Array. Zero the rest. 537 z := reflect.Zero(v.Type().Elem()) 538 for ; i < v.Len(); i++ { 539 v.Index(i).Set(z) 540 } 541 } else { 542 v.SetLen(i) 543 } 544 } 545 if i == 0 && v.Kind() == reflect.Slice { 546 v.Set(reflect.MakeSlice(v.Type(), 0, 0)) 547 } 548 } 549 550 var nullLiteral = []byte("null") 551 552 // object consumes an object from d.data[d.off-1:], decoding into the value v. 553 // the first byte ('{') of the object has been read already. 554 func (d *decodeState) object(v reflect.Value) { 555 // Check for unmarshaler. 556 u, ut, pv := d.indirect(v, false) 557 if u != nil { 558 d.off-- 559 err := u.UnmarshalJSON(d.next()) 560 if err != nil { 561 d.error(err) 562 } 563 return 564 } 565 if ut != nil { 566 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) 567 d.off-- 568 d.next() // skip over { } in input 569 return 570 } 571 v = pv 572 573 // Decoding into nil interface? Switch to non-reflect code. 574 if v.Kind() == reflect.Interface && v.NumMethod() == 0 { 575 v.Set(reflect.ValueOf(d.objectInterface())) 576 return 577 } 578 579 // Check type of target: struct or map[string]T 580 switch v.Kind() { 581 case reflect.Map: 582 // map must have string kind 583 t := v.Type() 584 if t.Key().Kind() != reflect.String { 585 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) 586 d.off-- 587 d.next() // skip over { } in input 588 return 589 } 590 if v.IsNil() { 591 v.Set(reflect.MakeMap(t)) 592 } 593 case reflect.Struct: 594 595 default: 596 d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)}) 597 d.off-- 598 d.next() // skip over { } in input 599 return 600 } 601 602 var mapElem reflect.Value 603 604 for { 605 // Read opening " of string key or closing }. 606 op := d.scanWhile(scanSkipSpace) 607 if op == scanEndObject { 608 // closing } - can only happen on first iteration. 609 break 610 } 611 if op != scanBeginLiteral { 612 d.error(errPhase) 613 } 614 615 // Read key. 616 start := d.off - 1 617 op = d.scanWhile(scanContinue) 618 item := d.data[start : d.off-1] 619 key, ok := unquoteBytes(item) 620 if !ok { 621 d.error(errPhase) 622 } 623 624 // Figure out field corresponding to key. 625 var subv reflect.Value 626 destring := false // whether the value is wrapped in a string to be decoded first 627 628 if v.Kind() == reflect.Map { 629 elemType := v.Type().Elem() 630 if !mapElem.IsValid() { 631 mapElem = reflect.New(elemType).Elem() 632 } else { 633 mapElem.Set(reflect.Zero(elemType)) 634 } 635 subv = mapElem 636 } else { 637 var f *field 638 fields := cachedTypeFields(v.Type()) 639 for i := range fields { 640 ff := &fields[i] 641 if bytes.Equal(ff.nameBytes, key) { 642 f = ff 643 break 644 } 645 if f == nil && ff.equalFold(ff.nameBytes, key) { 646 f = ff 647 } 648 } 649 if f != nil { 650 subv = v 651 destring = f.quoted 652 for _, i := range f.index { 653 if subv.Kind() == reflect.Ptr { 654 if subv.IsNil() { 655 subv.Set(reflect.New(subv.Type().Elem())) 656 } 657 subv = subv.Elem() 658 } 659 subv = subv.Field(i) 660 } 661 } 662 } 663 664 // Read : before value. 665 if op == scanSkipSpace { 666 op = d.scanWhile(scanSkipSpace) 667 } 668 if op != scanObjectKey { 669 d.error(errPhase) 670 } 671 672 // Read value. 673 if destring { 674 switch qv := d.valueQuoted().(type) { 675 case nil: 676 d.literalStore(nullLiteral, subv, false) 677 case string: 678 d.literalStore([]byte(qv), subv, true) 679 default: 680 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) 681 } 682 } else { 683 d.value(subv) 684 } 685 686 // Write value back to map; 687 // if using struct, subv points into struct already. 688 if v.Kind() == reflect.Map { 689 kv := reflect.ValueOf(key).Convert(v.Type().Key()) 690 v.SetMapIndex(kv, subv) 691 } 692 693 // Next token must be , or }. 694 op = d.scanWhile(scanSkipSpace) 695 if op == scanEndObject { 696 break 697 } 698 if op != scanObjectValue { 699 d.error(errPhase) 700 } 701 } 702 } 703 704 // literal consumes a literal from d.data[d.off-1:], decoding into the value v. 705 // The first byte of the literal has been read already 706 // (that's how the caller knows it's a literal). 707 func (d *decodeState) literal(v reflect.Value) { 708 // All bytes inside literal return scanContinue op code. 709 start := d.off - 1 710 op := d.scanWhile(scanContinue) 711 712 // Scan read one byte too far; back up. 713 d.off-- 714 d.scan.undo(op) 715 716 d.literalStore(d.data[start:d.off], v, false) 717 } 718 719 // convertNumber converts the number literal s to a float64 or a Number 720 // depending on the setting of d.useNumber. 721 func (d *decodeState) convertNumber(s string) (interface{}, error) { 722 if d.useNumber { 723 return Number(s), nil 724 } 725 f, err := strconv.ParseFloat(s, 64) 726 if err != nil { 727 return nil, &UnmarshalTypeError{"number " + s, reflect.TypeOf(0.0), int64(d.off)} 728 } 729 return f, nil 730 } 731 732 var numberType = reflect.TypeOf(Number("")) 733 734 // literalStore decodes a literal stored in item into v. 735 // 736 // fromQuoted indicates whether this literal came from unwrapping a 737 // string from the ",string" struct tag option. this is used only to 738 // produce more helpful error messages. 739 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) { 740 // Check for unmarshaler. 741 if len(item) == 0 { 742 //Empty string given 743 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 744 return 745 } 746 wantptr := item[0] == 'n' // null 747 u, ut, pv := d.indirect(v, wantptr) 748 if u != nil { 749 err := u.UnmarshalJSON(item) 750 if err != nil { 751 d.error(err) 752 } 753 return 754 } 755 if ut != nil { 756 if item[0] != '"' { 757 if fromQuoted { 758 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 759 } else { 760 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) 761 } 762 return 763 } 764 s, ok := unquoteBytes(item) 765 if !ok { 766 if fromQuoted { 767 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 768 } else { 769 d.error(errPhase) 770 } 771 } 772 err := ut.UnmarshalText(s) 773 if err != nil { 774 d.error(err) 775 } 776 return 777 } 778 779 v = pv 780 781 switch c := item[0]; c { 782 case 'n': // null 783 switch v.Kind() { 784 case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: 785 v.Set(reflect.Zero(v.Type())) 786 // otherwise, ignore null for primitives/string 787 } 788 case 't', 'f': // true, false 789 value := c == 't' 790 switch v.Kind() { 791 default: 792 if fromQuoted { 793 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 794 } else { 795 d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)}) 796 } 797 case reflect.Bool: 798 v.SetBool(value) 799 case reflect.Interface: 800 if v.NumMethod() == 0 { 801 v.Set(reflect.ValueOf(value)) 802 } else { 803 d.saveError(&UnmarshalTypeError{"bool", v.Type(), int64(d.off)}) 804 } 805 } 806 807 case '"': // string 808 s, ok := unquoteBytes(item) 809 if !ok { 810 if fromQuoted { 811 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 812 } else { 813 d.error(errPhase) 814 } 815 } 816 switch v.Kind() { 817 default: 818 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) 819 case reflect.Slice: 820 if v.Type().Elem().Kind() != reflect.Uint8 { 821 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) 822 break 823 } 824 b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) 825 n, err := base64.StdEncoding.Decode(b, s) 826 if err != nil { 827 d.saveError(err) 828 break 829 } 830 v.SetBytes(b[:n]) 831 case reflect.String: 832 v.SetString(string(s)) 833 case reflect.Interface: 834 if v.NumMethod() == 0 { 835 v.Set(reflect.ValueOf(string(s))) 836 } else { 837 d.saveError(&UnmarshalTypeError{"string", v.Type(), int64(d.off)}) 838 } 839 } 840 841 default: // number 842 if c != '-' && (c < '0' || c > '9') { 843 if fromQuoted { 844 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 845 } else { 846 d.error(errPhase) 847 } 848 } 849 s := string(item) 850 switch v.Kind() { 851 default: 852 if v.Kind() == reflect.String && v.Type() == numberType { 853 v.SetString(s) 854 if !isValidNumber(s) { 855 d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)) 856 } 857 break 858 } 859 if fromQuoted { 860 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 861 } else { 862 d.error(&UnmarshalTypeError{"number", v.Type(), int64(d.off)}) 863 } 864 case reflect.Interface: 865 n, err := d.convertNumber(s) 866 if err != nil { 867 d.saveError(err) 868 break 869 } 870 if v.NumMethod() != 0 { 871 d.saveError(&UnmarshalTypeError{"number", v.Type(), int64(d.off)}) 872 break 873 } 874 v.Set(reflect.ValueOf(n)) 875 876 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 877 n, err := strconv.ParseInt(s, 10, 64) 878 if err != nil || v.OverflowInt(n) { 879 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)}) 880 break 881 } 882 v.SetInt(n) 883 884 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 885 n, err := strconv.ParseUint(s, 10, 64) 886 if err != nil || v.OverflowUint(n) { 887 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)}) 888 break 889 } 890 v.SetUint(n) 891 892 case reflect.Float32, reflect.Float64: 893 n, err := strconv.ParseFloat(s, v.Type().Bits()) 894 if err != nil || v.OverflowFloat(n) { 895 d.saveError(&UnmarshalTypeError{"number " + s, v.Type(), int64(d.off)}) 896 break 897 } 898 v.SetFloat(n) 899 } 900 } 901 } 902 903 // The xxxInterface routines build up a value to be stored 904 // in an empty interface. They are not strictly necessary, 905 // but they avoid the weight of reflection in this common case. 906 907 // valueInterface is like value but returns interface{} 908 func (d *decodeState) valueInterface() interface{} { 909 switch d.scanWhile(scanSkipSpace) { 910 default: 911 d.error(errPhase) 912 panic("unreachable") 913 case scanBeginArray: 914 return d.arrayInterface() 915 case scanBeginObject: 916 return d.objectInterface() 917 case scanBeginLiteral: 918 return d.literalInterface() 919 } 920 } 921 922 // arrayInterface is like array but returns []interface{}. 923 func (d *decodeState) arrayInterface() []interface{} { 924 var v = make([]interface{}, 0) 925 for { 926 // Look ahead for ] - can only happen on first iteration. 927 op := d.scanWhile(scanSkipSpace) 928 if op == scanEndArray { 929 break 930 } 931 932 // Back up so d.value can have the byte we just read. 933 d.off-- 934 d.scan.undo(op) 935 936 v = append(v, d.valueInterface()) 937 938 // Next token must be , or ]. 939 op = d.scanWhile(scanSkipSpace) 940 if op == scanEndArray { 941 break 942 } 943 if op != scanArrayValue { 944 d.error(errPhase) 945 } 946 } 947 return v 948 } 949 950 // objectInterface is like object but returns map[string]interface{}. 951 func (d *decodeState) objectInterface() map[string]interface{} { 952 m := make(map[string]interface{}) 953 for { 954 // Read opening " of string key or closing }. 955 op := d.scanWhile(scanSkipSpace) 956 if op == scanEndObject { 957 // closing } - can only happen on first iteration. 958 break 959 } 960 if op != scanBeginLiteral { 961 d.error(errPhase) 962 } 963 964 // Read string key. 965 start := d.off - 1 966 op = d.scanWhile(scanContinue) 967 item := d.data[start : d.off-1] 968 key, ok := unquote(item) 969 if !ok { 970 d.error(errPhase) 971 } 972 973 // Read : before value. 974 if op == scanSkipSpace { 975 op = d.scanWhile(scanSkipSpace) 976 } 977 if op != scanObjectKey { 978 d.error(errPhase) 979 } 980 981 // Read value. 982 m[key] = d.valueInterface() 983 984 // Next token must be , or }. 985 op = d.scanWhile(scanSkipSpace) 986 if op == scanEndObject { 987 break 988 } 989 if op != scanObjectValue { 990 d.error(errPhase) 991 } 992 } 993 return m 994 } 995 996 // literalInterface is like literal but returns an interface value. 997 func (d *decodeState) literalInterface() interface{} { 998 // All bytes inside literal return scanContinue op code. 999 start := d.off - 1 1000 op := d.scanWhile(scanContinue) 1001 1002 // Scan read one byte too far; back up. 1003 d.off-- 1004 d.scan.undo(op) 1005 item := d.data[start:d.off] 1006 1007 switch c := item[0]; c { 1008 case 'n': // null 1009 return nil 1010 1011 case 't', 'f': // true, false 1012 return c == 't' 1013 1014 case '"': // string 1015 s, ok := unquote(item) 1016 if !ok { 1017 d.error(errPhase) 1018 } 1019 return s 1020 1021 default: // number 1022 if c != '-' && (c < '0' || c > '9') { 1023 d.error(errPhase) 1024 } 1025 n, err := d.convertNumber(string(item)) 1026 if err != nil { 1027 d.saveError(err) 1028 } 1029 return n 1030 } 1031 } 1032 1033 // getu4 decodes \uXXXX from the beginning of s, returning the hex value, 1034 // or it returns -1. 1035 func getu4(s []byte) rune { 1036 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { 1037 return -1 1038 } 1039 r, err := strconv.ParseUint(string(s[2:6]), 16, 64) 1040 if err != nil { 1041 return -1 1042 } 1043 return rune(r) 1044 } 1045 1046 // unquote converts a quoted JSON string literal s into an actual string t. 1047 // The rules are different than for Go, so cannot use strconv.Unquote. 1048 func unquote(s []byte) (t string, ok bool) { 1049 s, ok = unquoteBytes(s) 1050 t = string(s) 1051 return 1052 } 1053 1054 func unquoteBytes(s []byte) (t []byte, ok bool) { 1055 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 1056 return 1057 } 1058 s = s[1 : len(s)-1] 1059 1060 // Check for unusual characters. If there are none, 1061 // then no unquoting is needed, so return a slice of the 1062 // original bytes. 1063 r := 0 1064 for r < len(s) { 1065 c := s[r] 1066 if c == '\\' || c == '"' || c < ' ' { 1067 break 1068 } 1069 if c < utf8.RuneSelf { 1070 r++ 1071 continue 1072 } 1073 rr, size := utf8.DecodeRune(s[r:]) 1074 if rr == utf8.RuneError && size == 1 { 1075 break 1076 } 1077 r += size 1078 } 1079 if r == len(s) { 1080 return s, true 1081 } 1082 1083 b := make([]byte, len(s)+2*utf8.UTFMax) 1084 w := copy(b, s[0:r]) 1085 for r < len(s) { 1086 // Out of room? Can only happen if s is full of 1087 // malformed UTF-8 and we're replacing each 1088 // byte with RuneError. 1089 if w >= len(b)-2*utf8.UTFMax { 1090 nb := make([]byte, (len(b)+utf8.UTFMax)*2) 1091 copy(nb, b[0:w]) 1092 b = nb 1093 } 1094 switch c := s[r]; { 1095 case c == '\\': 1096 r++ 1097 if r >= len(s) { 1098 return 1099 } 1100 switch s[r] { 1101 default: 1102 return 1103 case '"', '\\', '/', '\'': 1104 b[w] = s[r] 1105 r++ 1106 w++ 1107 case 'b': 1108 b[w] = '\b' 1109 r++ 1110 w++ 1111 case 'f': 1112 b[w] = '\f' 1113 r++ 1114 w++ 1115 case 'n': 1116 b[w] = '\n' 1117 r++ 1118 w++ 1119 case 'r': 1120 b[w] = '\r' 1121 r++ 1122 w++ 1123 case 't': 1124 b[w] = '\t' 1125 r++ 1126 w++ 1127 case 'u': 1128 r-- 1129 rr := getu4(s[r:]) 1130 if rr < 0 { 1131 return 1132 } 1133 r += 6 1134 if utf16.IsSurrogate(rr) { 1135 rr1 := getu4(s[r:]) 1136 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { 1137 // A valid pair; consume. 1138 r += 6 1139 w += utf8.EncodeRune(b[w:], dec) 1140 break 1141 } 1142 // Invalid surrogate; fall back to replacement rune. 1143 rr = unicode.ReplacementChar 1144 } 1145 w += utf8.EncodeRune(b[w:], rr) 1146 } 1147 1148 // Quote, control characters are invalid. 1149 case c == '"', c < ' ': 1150 return 1151 1152 // ASCII 1153 case c < utf8.RuneSelf: 1154 b[w] = c 1155 r++ 1156 w++ 1157 1158 // Coerce to well-formed UTF-8. 1159 default: 1160 rr, size := utf8.DecodeRune(s[r:]) 1161 r += size 1162 w += utf8.EncodeRune(b[w:], rr) 1163 } 1164 } 1165 return b[0:w], true 1166 }