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