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