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