Source file src/pkg/encoding/json/decode.go
1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Represents JSON data structure using native Go types: booleans, floats, 6 // strings, arrays, and maps. 7 8 package json 9 10 import ( 11 "encoding/base64" 12 "errors" 13 "fmt" 14 "reflect" 15 "runtime" 16 "strconv" 17 "strings" 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. 25 // 26 // Unmarshal uses the inverse of the encodings that 27 // Marshal uses, allocating maps, slices, and pointers as necessary, 28 // with the following additional rules: 29 // 30 // To unmarshal JSON into a pointer, Unmarshal first handles the case of 31 // the JSON being the JSON literal null. In that case, Unmarshal sets 32 // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into 33 // the value pointed at by the pointer. If the pointer is nil, Unmarshal 34 // allocates a new value for it to point to. 35 // 36 // To unmarshal JSON into an interface value, Unmarshal unmarshals 37 // the JSON into the concrete value contained in the interface value. 38 // If the interface value is nil, that is, has no concrete value stored in it, 39 // Unmarshal stores one of these in the interface value: 40 // 41 // bool, for JSON booleans 42 // float64, for JSON numbers 43 // string, for JSON strings 44 // []interface{}, for JSON arrays 45 // map[string]interface{}, for JSON objects 46 // nil for JSON null 47 // 48 // If a JSON value is not appropriate for a given target type, 49 // or if a JSON number overflows the target type, Unmarshal 50 // skips that field and completes the unmarshalling as best it can. 51 // If no more serious errors are encountered, Unmarshal returns 52 // an UnmarshalTypeError describing the earliest such error. 53 // 54 func Unmarshal(data []byte, v interface{}) error { 55 d := new(decodeState).init(data) 56 57 // Quick check for well-formedness. 58 // Avoids filling out half a data structure 59 // before discovering a JSON syntax error. 60 err := checkValid(data, &d.scan) 61 if err != nil { 62 return err 63 } 64 65 return d.unmarshal(v) 66 } 67 68 // Unmarshaler is the interface implemented by objects 69 // that can unmarshal a JSON description of themselves. 70 // The input can be assumed to be a valid JSON object 71 // encoding. UnmarshalJSON must copy the JSON data 72 // if it wishes to retain the data after returning. 73 type Unmarshaler interface { 74 UnmarshalJSON([]byte) error 75 } 76 77 // An UnmarshalTypeError describes a JSON value that was 78 // not appropriate for a value of a specific Go type. 79 type UnmarshalTypeError struct { 80 Value string // description of JSON value - "bool", "array", "number -5" 81 Type reflect.Type // type of Go value it could not be assigned to 82 } 83 84 func (e *UnmarshalTypeError) Error() string { 85 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() 86 } 87 88 // An UnmarshalFieldError describes a JSON object key that 89 // led to an unexported (and therefore unwritable) struct field. 90 type UnmarshalFieldError struct { 91 Key string 92 Type reflect.Type 93 Field reflect.StructField 94 } 95 96 func (e *UnmarshalFieldError) Error() string { 97 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() 98 } 99 100 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. 101 // (The argument to Unmarshal must be a non-nil pointer.) 102 type InvalidUnmarshalError struct { 103 Type reflect.Type 104 } 105 106 func (e *InvalidUnmarshalError) Error() string { 107 if e.Type == nil { 108 return "json: Unmarshal(nil)" 109 } 110 111 if e.Type.Kind() != reflect.Ptr { 112 return "json: Unmarshal(non-pointer " + e.Type.String() + ")" 113 } 114 return "json: Unmarshal(nil " + e.Type.String() + ")" 115 } 116 117 func (d *decodeState) unmarshal(v interface{}) (err error) { 118 defer func() { 119 if r := recover(); r != nil { 120 if _, ok := r.(runtime.Error); ok { 121 panic(r) 122 } 123 err = r.(error) 124 } 125 }() 126 127 rv := reflect.ValueOf(v) 128 pv := rv 129 if pv.Kind() != reflect.Ptr || pv.IsNil() { 130 return &InvalidUnmarshalError{reflect.TypeOf(v)} 131 } 132 133 d.scan.reset() 134 // We decode rv not pv.Elem because the Unmarshaler interface 135 // test must be applied at the top level of the value. 136 d.value(rv) 137 return d.savedError 138 } 139 140 // decodeState represents the state while decoding a JSON value. 141 type decodeState struct { 142 data []byte 143 off int // read offset in data 144 scan scanner 145 nextscan scanner // for calls to nextValue 146 savedError error 147 tempstr string // scratch space to avoid some allocations 148 } 149 150 // errPhase is used for errors that should not happen unless 151 // there is a bug in the JSON decoder or something is editing 152 // the data slice while the decoder executes. 153 var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?") 154 155 func (d *decodeState) init(data []byte) *decodeState { 156 d.data = data 157 d.off = 0 158 d.savedError = nil 159 return d 160 } 161 162 // error aborts the decoding by panicking with err. 163 func (d *decodeState) error(err error) { 164 panic(err) 165 } 166 167 // saveError saves the first err it is called with, 168 // for reporting at the end of the unmarshal. 169 func (d *decodeState) saveError(err error) { 170 if d.savedError == nil { 171 d.savedError = err 172 } 173 } 174 175 // next cuts off and returns the next full JSON value in d.data[d.off:]. 176 // The next value is known to be an object or array, not a literal. 177 func (d *decodeState) next() []byte { 178 c := d.data[d.off] 179 item, rest, err := nextValue(d.data[d.off:], &d.nextscan) 180 if err != nil { 181 d.error(err) 182 } 183 d.off = len(d.data) - len(rest) 184 185 // Our scanner has seen the opening brace/bracket 186 // and thinks we're still in the middle of the object. 187 // invent a closing brace/bracket to get it out. 188 if c == '{' { 189 d.scan.step(&d.scan, '}') 190 } else { 191 d.scan.step(&d.scan, ']') 192 } 193 194 return item 195 } 196 197 // scanWhile processes bytes in d.data[d.off:] until it 198 // receives a scan code not equal to op. 199 // It updates d.off and returns the new scan code. 200 func (d *decodeState) scanWhile(op int) int { 201 var newOp int 202 for { 203 if d.off >= len(d.data) { 204 newOp = d.scan.eof() 205 d.off = len(d.data) + 1 // mark processed EOF with len+1 206 } else { 207 c := int(d.data[d.off]) 208 d.off++ 209 newOp = d.scan.step(&d.scan, c) 210 } 211 if newOp != op { 212 break 213 } 214 } 215 return newOp 216 } 217 218 // value decodes a JSON value from d.data[d.off:] into the value. 219 // it updates d.off to point past the decoded value. 220 func (d *decodeState) value(v reflect.Value) { 221 if !v.IsValid() { 222 _, rest, err := nextValue(d.data[d.off:], &d.nextscan) 223 if err != nil { 224 d.error(err) 225 } 226 d.off = len(d.data) - len(rest) 227 228 // d.scan thinks we're still at the beginning of the item. 229 // Feed in an empty string - the shortest, simplest value - 230 // so that it knows we got to the end of the value. 231 if d.scan.redo { 232 // rewind. 233 d.scan.redo = false 234 d.scan.step = stateBeginValue 235 } 236 d.scan.step(&d.scan, '"') 237 d.scan.step(&d.scan, '"') 238 return 239 } 240 241 switch op := d.scanWhile(scanSkipSpace); op { 242 default: 243 d.error(errPhase) 244 245 case scanBeginArray: 246 d.array(v) 247 248 case scanBeginObject: 249 d.object(v) 250 251 case scanBeginLiteral: 252 d.literal(v) 253 } 254 } 255 256 // indirect walks down v allocating pointers as needed, 257 // until it gets to a non-pointer. 258 // if it encounters an Unmarshaler, indirect stops and returns that. 259 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil. 260 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) { 261 // If v is a named type and is addressable, 262 // start with its address, so that if the type has pointer methods, 263 // we find them. 264 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() { 265 v = v.Addr() 266 } 267 for { 268 var isUnmarshaler bool 269 if v.Type().NumMethod() > 0 { 270 // Remember that this is an unmarshaler, 271 // but wait to return it until after allocating 272 // the pointer (if necessary). 273 _, isUnmarshaler = v.Interface().(Unmarshaler) 274 } 275 276 if iv := v; iv.Kind() == reflect.Interface && !iv.IsNil() { 277 v = iv.Elem() 278 continue 279 } 280 281 pv := v 282 if pv.Kind() != reflect.Ptr { 283 break 284 } 285 286 if pv.Elem().Kind() != reflect.Ptr && decodingNull && pv.CanSet() { 287 return nil, pv 288 } 289 if pv.IsNil() { 290 pv.Set(reflect.New(pv.Type().Elem())) 291 } 292 if isUnmarshaler { 293 // Using v.Interface().(Unmarshaler) 294 // here means that we have to use a pointer 295 // as the struct field. We cannot use a value inside 296 // a pointer to a struct, because in that case 297 // v.Interface() is the value (x.f) not the pointer (&x.f). 298 // This is an unfortunate consequence of reflect. 299 // An alternative would be to look up the 300 // UnmarshalJSON method and return a FuncValue. 301 return v.Interface().(Unmarshaler), reflect.Value{} 302 } 303 v = pv.Elem() 304 } 305 return nil, v 306 } 307 308 // array consumes an array from d.data[d.off-1:], decoding into the value v. 309 // the first byte of the array ('[') has been read already. 310 func (d *decodeState) array(v reflect.Value) { 311 // Check for unmarshaler. 312 unmarshaler, pv := d.indirect(v, false) 313 if unmarshaler != nil { 314 d.off-- 315 err := unmarshaler.UnmarshalJSON(d.next()) 316 if err != nil { 317 d.error(err) 318 } 319 return 320 } 321 v = pv 322 323 // Check type of target. 324 switch v.Kind() { 325 default: 326 d.saveError(&UnmarshalTypeError{"array", v.Type()}) 327 d.off-- 328 d.next() 329 return 330 case reflect.Interface: 331 // Decoding into nil interface? Switch to non-reflect code. 332 v.Set(reflect.ValueOf(d.arrayInterface())) 333 return 334 case reflect.Array: 335 case reflect.Slice: 336 break 337 } 338 339 i := 0 340 for { 341 // Look ahead for ] - can only happen on first iteration. 342 op := d.scanWhile(scanSkipSpace) 343 if op == scanEndArray { 344 break 345 } 346 347 // Back up so d.value can have the byte we just read. 348 d.off-- 349 d.scan.undo(op) 350 351 // Get element of array, growing if necessary. 352 if v.Kind() == reflect.Slice { 353 // Grow slice if necessary 354 if i >= v.Cap() { 355 newcap := v.Cap() + v.Cap()/2 356 if newcap < 4 { 357 newcap = 4 358 } 359 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) 360 reflect.Copy(newv, v) 361 v.Set(newv) 362 } 363 if i >= v.Len() { 364 v.SetLen(i + 1) 365 } 366 } 367 368 if i < v.Len() { 369 // Decode into element. 370 d.value(v.Index(i)) 371 } else { 372 // Ran out of fixed array: skip. 373 d.value(reflect.Value{}) 374 } 375 i++ 376 377 // Next token must be , or ]. 378 op = d.scanWhile(scanSkipSpace) 379 if op == scanEndArray { 380 break 381 } 382 if op != scanArrayValue { 383 d.error(errPhase) 384 } 385 } 386 387 if i < v.Len() { 388 if v.Kind() == reflect.Array { 389 // Array. Zero the rest. 390 z := reflect.Zero(v.Type().Elem()) 391 for ; i < v.Len(); i++ { 392 v.Index(i).Set(z) 393 } 394 } else { 395 v.SetLen(i) 396 } 397 } 398 if i == 0 && v.Kind() == reflect.Slice { 399 v.Set(reflect.MakeSlice(v.Type(), 0, 0)) 400 } 401 } 402 403 // object consumes an object from d.data[d.off-1:], decoding into the value v. 404 // the first byte of the object ('{') has been read already. 405 func (d *decodeState) object(v reflect.Value) { 406 // Check for unmarshaler. 407 unmarshaler, pv := d.indirect(v, false) 408 if unmarshaler != nil { 409 d.off-- 410 err := unmarshaler.UnmarshalJSON(d.next()) 411 if err != nil { 412 d.error(err) 413 } 414 return 415 } 416 v = pv 417 418 // Decoding into nil interface? Switch to non-reflect code. 419 iv := v 420 if iv.Kind() == reflect.Interface { 421 iv.Set(reflect.ValueOf(d.objectInterface())) 422 return 423 } 424 425 // Check type of target: struct or map[string]T 426 var ( 427 mv reflect.Value 428 sv reflect.Value 429 ) 430 switch v.Kind() { 431 case reflect.Map: 432 // map must have string type 433 t := v.Type() 434 if t.Key() != reflect.TypeOf("") { 435 d.saveError(&UnmarshalTypeError{"object", v.Type()}) 436 break 437 } 438 mv = v 439 if mv.IsNil() { 440 mv.Set(reflect.MakeMap(t)) 441 } 442 case reflect.Struct: 443 sv = v 444 default: 445 d.saveError(&UnmarshalTypeError{"object", v.Type()}) 446 } 447 448 if !mv.IsValid() && !sv.IsValid() { 449 d.off-- 450 d.next() // skip over { } in input 451 return 452 } 453 454 var mapElem reflect.Value 455 456 for { 457 // Read opening " of string key or closing }. 458 op := d.scanWhile(scanSkipSpace) 459 if op == scanEndObject { 460 // closing } - can only happen on first iteration. 461 break 462 } 463 if op != scanBeginLiteral { 464 d.error(errPhase) 465 } 466 467 // Read string key. 468 start := d.off - 1 469 op = d.scanWhile(scanContinue) 470 item := d.data[start : d.off-1] 471 key, ok := unquote(item) 472 if !ok { 473 d.error(errPhase) 474 } 475 476 // Figure out field corresponding to key. 477 var subv reflect.Value 478 destring := false // whether the value is wrapped in a string to be decoded first 479 480 if mv.IsValid() { 481 elemType := mv.Type().Elem() 482 if !mapElem.IsValid() { 483 mapElem = reflect.New(elemType).Elem() 484 } else { 485 mapElem.Set(reflect.Zero(elemType)) 486 } 487 subv = mapElem 488 } else { 489 var f reflect.StructField 490 var ok bool 491 st := sv.Type() 492 for i := 0; i < sv.NumField(); i++ { 493 sf := st.Field(i) 494 tag := sf.Tag.Get("json") 495 if tag == "-" { 496 // Pretend this field doesn't exist. 497 continue 498 } 499 if sf.Anonymous { 500 // Pretend this field doesn't exist, 501 // so that we can do a good job with 502 // these in a later version. 503 continue 504 } 505 // First, tag match 506 tagName, _ := parseTag(tag) 507 if tagName == key { 508 f = sf 509 ok = true 510 break // no better match possible 511 } 512 // Second, exact field name match 513 if sf.Name == key { 514 f = sf 515 ok = true 516 } 517 // Third, case-insensitive field name match, 518 // but only if a better match hasn't already been seen 519 if !ok && strings.EqualFold(sf.Name, key) { 520 f = sf 521 ok = true 522 } 523 } 524 525 // Extract value; name must be exported. 526 if ok { 527 if f.PkgPath != "" { 528 d.saveError(&UnmarshalFieldError{key, st, f}) 529 } else { 530 subv = sv.FieldByIndex(f.Index) 531 } 532 _, opts := parseTag(f.Tag.Get("json")) 533 destring = opts.Contains("string") 534 } 535 } 536 537 // Read : before value. 538 if op == scanSkipSpace { 539 op = d.scanWhile(scanSkipSpace) 540 } 541 if op != scanObjectKey { 542 d.error(errPhase) 543 } 544 545 // Read value. 546 if destring { 547 d.value(reflect.ValueOf(&d.tempstr)) 548 d.literalStore([]byte(d.tempstr), subv, true) 549 } else { 550 d.value(subv) 551 } 552 // Write value back to map; 553 // if using struct, subv points into struct already. 554 if mv.IsValid() { 555 mv.SetMapIndex(reflect.ValueOf(key), subv) 556 } 557 558 // Next token must be , or }. 559 op = d.scanWhile(scanSkipSpace) 560 if op == scanEndObject { 561 break 562 } 563 if op != scanObjectValue { 564 d.error(errPhase) 565 } 566 } 567 } 568 569 // literal consumes a literal from d.data[d.off-1:], decoding into the value v. 570 // The first byte of the literal has been read already 571 // (that's how the caller knows it's a literal). 572 func (d *decodeState) literal(v reflect.Value) { 573 // All bytes inside literal return scanContinue op code. 574 start := d.off - 1 575 op := d.scanWhile(scanContinue) 576 577 // Scan read one byte too far; back up. 578 d.off-- 579 d.scan.undo(op) 580 581 d.literalStore(d.data[start:d.off], v, false) 582 } 583 584 // literalStore decodes a literal stored in item into v. 585 // 586 // fromQuoted indicates whether this literal came from unwrapping a 587 // string from the ",string" struct tag option. this is used only to 588 // produce more helpful error messages. 589 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) { 590 // Check for unmarshaler. 591 wantptr := item[0] == 'n' // null 592 unmarshaler, pv := d.indirect(v, wantptr) 593 if unmarshaler != nil { 594 err := unmarshaler.UnmarshalJSON(item) 595 if err != nil { 596 d.error(err) 597 } 598 return 599 } 600 v = pv 601 602 switch c := item[0]; c { 603 case 'n': // null 604 switch v.Kind() { 605 default: 606 d.saveError(&UnmarshalTypeError{"null", v.Type()}) 607 case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: 608 v.Set(reflect.Zero(v.Type())) 609 } 610 611 case 't', 'f': // true, false 612 value := c == 't' 613 switch v.Kind() { 614 default: 615 if fromQuoted { 616 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 617 } else { 618 d.saveError(&UnmarshalTypeError{"bool", v.Type()}) 619 } 620 case reflect.Bool: 621 v.SetBool(value) 622 case reflect.Interface: 623 v.Set(reflect.ValueOf(value)) 624 } 625 626 case '"': // string 627 s, ok := unquoteBytes(item) 628 if !ok { 629 if fromQuoted { 630 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 631 } else { 632 d.error(errPhase) 633 } 634 } 635 switch v.Kind() { 636 default: 637 d.saveError(&UnmarshalTypeError{"string", v.Type()}) 638 case reflect.Slice: 639 if v.Type() != byteSliceType { 640 d.saveError(&UnmarshalTypeError{"string", v.Type()}) 641 break 642 } 643 b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) 644 n, err := base64.StdEncoding.Decode(b, s) 645 if err != nil { 646 d.saveError(err) 647 break 648 } 649 v.Set(reflect.ValueOf(b[0:n])) 650 case reflect.String: 651 v.SetString(string(s)) 652 case reflect.Interface: 653 v.Set(reflect.ValueOf(string(s))) 654 } 655 656 default: // number 657 if c != '-' && (c < '0' || c > '9') { 658 if fromQuoted { 659 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 660 } else { 661 d.error(errPhase) 662 } 663 } 664 s := string(item) 665 switch v.Kind() { 666 default: 667 if fromQuoted { 668 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 669 } else { 670 d.error(&UnmarshalTypeError{"number", v.Type()}) 671 } 672 case reflect.Interface: 673 n, err := strconv.ParseFloat(s, 64) 674 if err != nil { 675 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 676 break 677 } 678 v.Set(reflect.ValueOf(n)) 679 680 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 681 n, err := strconv.ParseInt(s, 10, 64) 682 if err != nil || v.OverflowInt(n) { 683 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 684 break 685 } 686 v.SetInt(n) 687 688 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 689 n, err := strconv.ParseUint(s, 10, 64) 690 if err != nil || v.OverflowUint(n) { 691 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 692 break 693 } 694 v.SetUint(n) 695 696 case reflect.Float32, reflect.Float64: 697 n, err := strconv.ParseFloat(s, v.Type().Bits()) 698 if err != nil || v.OverflowFloat(n) { 699 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) 700 break 701 } 702 v.SetFloat(n) 703 } 704 } 705 } 706 707 // The xxxInterface routines build up a value to be stored 708 // in an empty interface. They are not strictly necessary, 709 // but they avoid the weight of reflection in this common case. 710 711 // valueInterface is like value but returns interface{} 712 func (d *decodeState) valueInterface() interface{} { 713 switch d.scanWhile(scanSkipSpace) { 714 default: 715 d.error(errPhase) 716 case scanBeginArray: 717 return d.arrayInterface() 718 case scanBeginObject: 719 return d.objectInterface() 720 case scanBeginLiteral: 721 return d.literalInterface() 722 } 723 panic("unreachable") 724 } 725 726 // arrayInterface is like array but returns []interface{}. 727 func (d *decodeState) arrayInterface() []interface{} { 728 var v []interface{} 729 for { 730 // Look ahead for ] - can only happen on first iteration. 731 op := d.scanWhile(scanSkipSpace) 732 if op == scanEndArray { 733 break 734 } 735 736 // Back up so d.value can have the byte we just read. 737 d.off-- 738 d.scan.undo(op) 739 740 v = append(v, d.valueInterface()) 741 742 // Next token must be , or ]. 743 op = d.scanWhile(scanSkipSpace) 744 if op == scanEndArray { 745 break 746 } 747 if op != scanArrayValue { 748 d.error(errPhase) 749 } 750 } 751 return v 752 } 753 754 // objectInterface is like object but returns map[string]interface{}. 755 func (d *decodeState) objectInterface() map[string]interface{} { 756 m := make(map[string]interface{}) 757 for { 758 // Read opening " of string key or closing }. 759 op := d.scanWhile(scanSkipSpace) 760 if op == scanEndObject { 761 // closing } - can only happen on first iteration. 762 break 763 } 764 if op != scanBeginLiteral { 765 d.error(errPhase) 766 } 767 768 // Read string key. 769 start := d.off - 1 770 op = d.scanWhile(scanContinue) 771 item := d.data[start : d.off-1] 772 key, ok := unquote(item) 773 if !ok { 774 d.error(errPhase) 775 } 776 777 // Read : before value. 778 if op == scanSkipSpace { 779 op = d.scanWhile(scanSkipSpace) 780 } 781 if op != scanObjectKey { 782 d.error(errPhase) 783 } 784 785 // Read value. 786 m[key] = d.valueInterface() 787 788 // Next token must be , or }. 789 op = d.scanWhile(scanSkipSpace) 790 if op == scanEndObject { 791 break 792 } 793 if op != scanObjectValue { 794 d.error(errPhase) 795 } 796 } 797 return m 798 } 799 800 // literalInterface is like literal but returns an interface value. 801 func (d *decodeState) literalInterface() interface{} { 802 // All bytes inside literal return scanContinue op code. 803 start := d.off - 1 804 op := d.scanWhile(scanContinue) 805 806 // Scan read one byte too far; back up. 807 d.off-- 808 d.scan.undo(op) 809 item := d.data[start:d.off] 810 811 switch c := item[0]; c { 812 case 'n': // null 813 return nil 814 815 case 't', 'f': // true, false 816 return c == 't' 817 818 case '"': // string 819 s, ok := unquote(item) 820 if !ok { 821 d.error(errPhase) 822 } 823 return s 824 825 default: // number 826 if c != '-' && (c < '0' || c > '9') { 827 d.error(errPhase) 828 } 829 n, err := strconv.ParseFloat(string(item), 64) 830 if err != nil { 831 d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.TypeOf(0.0)}) 832 } 833 return n 834 } 835 panic("unreachable") 836 } 837 838 // getu4 decodes \uXXXX from the beginning of s, returning the hex value, 839 // or it returns -1. 840 func getu4(s []byte) rune { 841 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { 842 return -1 843 } 844 r, err := strconv.ParseUint(string(s[2:6]), 16, 64) 845 if err != nil { 846 return -1 847 } 848 return rune(r) 849 } 850 851 // unquote converts a quoted JSON string literal s into an actual string t. 852 // The rules are different than for Go, so cannot use strconv.Unquote. 853 func unquote(s []byte) (t string, ok bool) { 854 s, ok = unquoteBytes(s) 855 t = string(s) 856 return 857 } 858 859 func unquoteBytes(s []byte) (t []byte, ok bool) { 860 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 861 return 862 } 863 s = s[1 : len(s)-1] 864 865 // Check for unusual characters. If there are none, 866 // then no unquoting is needed, so return a slice of the 867 // original bytes. 868 r := 0 869 for r < len(s) { 870 c := s[r] 871 if c == '\\' || c == '"' || c < ' ' { 872 break 873 } 874 if c < utf8.RuneSelf { 875 r++ 876 continue 877 } 878 rr, size := utf8.DecodeRune(s[r:]) 879 if rr == utf8.RuneError && size == 1 { 880 break 881 } 882 r += size 883 } 884 if r == len(s) { 885 return s, true 886 } 887 888 b := make([]byte, len(s)+2*utf8.UTFMax) 889 w := copy(b, s[0:r]) 890 for r < len(s) { 891 // Out of room? Can only happen if s is full of 892 // malformed UTF-8 and we're replacing each 893 // byte with RuneError. 894 if w >= len(b)-2*utf8.UTFMax { 895 nb := make([]byte, (len(b)+utf8.UTFMax)*2) 896 copy(nb, b[0:w]) 897 b = nb 898 } 899 switch c := s[r]; { 900 case c == '\\': 901 r++ 902 if r >= len(s) { 903 return 904 } 905 switch s[r] { 906 default: 907 return 908 case '"', '\\', '/', '\'': 909 b[w] = s[r] 910 r++ 911 w++ 912 case 'b': 913 b[w] = '\b' 914 r++ 915 w++ 916 case 'f': 917 b[w] = '\f' 918 r++ 919 w++ 920 case 'n': 921 b[w] = '\n' 922 r++ 923 w++ 924 case 'r': 925 b[w] = '\r' 926 r++ 927 w++ 928 case 't': 929 b[w] = '\t' 930 r++ 931 w++ 932 case 'u': 933 r-- 934 rr := getu4(s[r:]) 935 if rr < 0 { 936 return 937 } 938 r += 6 939 if utf16.IsSurrogate(rr) { 940 rr1 := getu4(s[r:]) 941 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { 942 // A valid pair; consume. 943 r += 6 944 w += utf8.EncodeRune(b[w:], dec) 945 break 946 } 947 // Invalid surrogate; fall back to replacement rune. 948 rr = unicode.ReplacementChar 949 } 950 w += utf8.EncodeRune(b[w:], rr) 951 } 952 953 // Quote, control characters are invalid. 954 case c == '"', c < ' ': 955 return 956 957 // ASCII 958 case c < utf8.RuneSelf: 959 b[w] = c 960 r++ 961 w++ 962 963 // Coerce to well-formed UTF-8. 964 default: 965 rr, size := utf8.DecodeRune(s[r:]) 966 r += size 967 w += utf8.EncodeRune(b[w:], rr) 968 } 969 } 970 return b[0:w], true 971 } 972 973 // The following is issue 3069. 974 975 // BUG(rsc): This package ignores anonymous (embedded) struct fields 976 // during encoding and decoding. A future version may assign meaning 977 // to them. To force an anonymous field to be ignored in all future 978 // versions of this package, use an explicit `json:"-"` tag in the struct 979 // definition.