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