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