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