github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/src/encoding/json/decode.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Represents JSON data structure using native Go types: booleans, floats, 6 // strings, arrays, and maps. 7 8 package json 9 10 import ( 11 "bytes" 12 "encoding" 13 "encoding/base64" 14 "errors" 15 "fmt" 16 "reflect" 17 "runtime" 18 "strconv" 19 "unicode" 20 "unicode/utf16" 21 "unicode/utf8" 22 ) 23 24 // Unmarshal parses the JSON-encoded data and stores the result 25 // in the value pointed to by v. If v is nil or not a pointer, 26 // Unmarshal returns an InvalidUnmarshalError. 27 // 28 // Unmarshal uses the inverse of the encodings that 29 // Marshal uses, allocating maps, slices, and pointers as necessary, 30 // with the following additional rules: 31 // 32 // To unmarshal JSON into a pointer, Unmarshal first handles the case of 33 // the JSON being the JSON literal null. In that case, Unmarshal sets 34 // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into 35 // the value pointed at by the pointer. If the pointer is nil, Unmarshal 36 // allocates a new value for it to point to. 37 // 38 // To unmarshal JSON into a value implementing the Unmarshaler interface, 39 // Unmarshal calls that value's UnmarshalJSON method, including 40 // when the input is a JSON null. 41 // Otherwise, if the value implements encoding.TextUnmarshaler 42 // and the input is a JSON quoted string, Unmarshal calls that value's 43 // UnmarshalText method with the unquoted form of the string. 44 // 45 // To unmarshal JSON into a struct, Unmarshal matches incoming object 46 // keys to the keys used by Marshal (either the struct field name or its tag), 47 // preferring an exact match but also accepting a case-insensitive match. 48 // Unmarshal will only set exported fields of the struct. 49 // 50 // To unmarshal JSON into an interface value, 51 // Unmarshal stores one of these in the interface value: 52 // 53 // bool, for JSON booleans 54 // float64, for JSON numbers 55 // string, for JSON strings 56 // []interface{}, for JSON arrays 57 // map[string]interface{}, for JSON objects 58 // nil for JSON null 59 // 60 // To unmarshal a JSON array into a slice, Unmarshal resets the slice length 61 // to zero and then appends each element to the slice. 62 // As a special case, to unmarshal an empty JSON array into a slice, 63 // Unmarshal replaces the slice with a new empty slice. 64 // 65 // To unmarshal a JSON array into a Go array, Unmarshal decodes 66 // JSON array elements into corresponding Go array elements. 67 // If the Go array is smaller than the JSON array, 68 // the additional JSON array elements are discarded. 69 // If the JSON array is smaller than the Go array, 70 // the additional Go array elements are set to zero values. 71 // 72 // To unmarshal a JSON object into a map, Unmarshal first establishes a map to 73 // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal 74 // reuses the existing map, keeping existing entries. Unmarshal then stores 75 // key-value pairs from the JSON object into the map. The map's key type must 76 // either be a string, an integer, or implement encoding.TextUnmarshaler. 77 // 78 // If a JSON value is not appropriate for a given target type, 79 // or if a JSON number overflows the target type, Unmarshal 80 // skips that field and completes the unmarshaling as best it can. 81 // If no more serious errors are encountered, Unmarshal returns 82 // an UnmarshalTypeError describing the earliest such error. In any 83 // case, it's not guaranteed that all the remaining fields following 84 // the problematic one will be unmarshaled into the target object. 85 // 86 // The JSON null value unmarshals into an interface, map, pointer, or slice 87 // by setting that Go value to nil. Because null is often used in JSON to mean 88 // ``not present,'' unmarshaling a JSON null into any other Go type has no effect 89 // on the value and produces no error. 90 // 91 // When unmarshaling quoted strings, invalid UTF-8 or 92 // invalid UTF-16 surrogate pairs are not treated as an error. 93 // Instead, they are replaced by the Unicode replacement 94 // character U+FFFD. 95 // 96 func Unmarshal(data []byte, v interface{}) error { 97 // Check for well-formedness. 98 // Avoids filling out half a data structure 99 // before discovering a JSON syntax error. 100 var d decodeState 101 err := checkValid(data, &d.scan) 102 if err != nil { 103 return err 104 } 105 106 d.init(data) 107 return d.unmarshal(v) 108 } 109 110 // Unmarshaler is the interface implemented by types 111 // that can unmarshal a JSON description of themselves. 112 // The input can be assumed to be a valid encoding of 113 // a JSON value. UnmarshalJSON must copy the JSON data 114 // if it wishes to retain the data after returning. 115 // 116 // By convention, to approximate the behavior of Unmarshal itself, 117 // Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op. 118 type Unmarshaler interface { 119 UnmarshalJSON([]byte) error 120 } 121 122 // An UnmarshalTypeError describes a JSON value that was 123 // not appropriate for a value of a specific Go type. 124 type UnmarshalTypeError struct { 125 Value string // description of JSON value - "bool", "array", "number -5" 126 Type reflect.Type // type of Go value it could not be assigned to 127 Offset int64 // error occurred after reading Offset bytes 128 Struct string // name of the struct type containing the field 129 Field string // name of the field holding the Go value 130 } 131 132 func (e *UnmarshalTypeError) Error() string { 133 if e.Struct != "" || e.Field != "" { 134 return "json: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String() 135 } 136 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() 137 } 138 139 // An UnmarshalFieldError describes a JSON object key that 140 // led to an unexported (and therefore unwritable) struct field. 141 // (No longer used; kept for compatibility.) 142 type UnmarshalFieldError struct { 143 Key string 144 Type reflect.Type 145 Field reflect.StructField 146 } 147 148 func (e *UnmarshalFieldError) Error() string { 149 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() 150 } 151 152 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. 153 // (The argument to Unmarshal must be a non-nil pointer.) 154 type InvalidUnmarshalError struct { 155 Type reflect.Type 156 } 157 158 func (e *InvalidUnmarshalError) Error() string { 159 if e.Type == nil { 160 return "json: Unmarshal(nil)" 161 } 162 163 if e.Type.Kind() != reflect.Ptr { 164 return "json: Unmarshal(non-pointer " + e.Type.String() + ")" 165 } 166 return "json: Unmarshal(nil " + e.Type.String() + ")" 167 } 168 169 func (d *decodeState) unmarshal(v interface{}) (err error) { 170 defer func() { 171 if r := recover(); r != nil { 172 if _, ok := r.(runtime.Error); ok { 173 panic(r) 174 } 175 err = r.(error) 176 } 177 }() 178 179 rv := reflect.ValueOf(v) 180 if rv.Kind() != reflect.Ptr || rv.IsNil() { 181 return &InvalidUnmarshalError{reflect.TypeOf(v)} 182 } 183 184 d.scan.reset() 185 // We decode rv not rv.Elem because the Unmarshaler interface 186 // test must be applied at the top level of the value. 187 d.value(rv) 188 return d.savedError 189 } 190 191 // A Number represents a JSON number literal. 192 type Number string 193 194 // String returns the literal text of the number. 195 func (n Number) String() string { return string(n) } 196 197 // Float64 returns the number as a float64. 198 func (n Number) Float64() (float64, error) { 199 return strconv.ParseFloat(string(n), 64) 200 } 201 202 // Int64 returns the number as an int64. 203 func (n Number) Int64() (int64, error) { 204 return strconv.ParseInt(string(n), 10, 64) 205 } 206 207 // isValidNumber reports whether s is a valid JSON number literal. 208 func isValidNumber(s string) bool { 209 // This function implements the JSON numbers grammar. 210 // See https://tools.ietf.org/html/rfc7159#section-6 211 // and http://json.org/number.gif 212 213 if s == "" { 214 return false 215 } 216 217 // Optional - 218 if s[0] == '-' { 219 s = s[1:] 220 if s == "" { 221 return false 222 } 223 } 224 225 // Digits 226 switch { 227 default: 228 return false 229 230 case s[0] == '0': 231 s = s[1:] 232 233 case '1' <= s[0] && s[0] <= '9': 234 s = s[1:] 235 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 236 s = s[1:] 237 } 238 } 239 240 // . followed by 1 or more digits. 241 if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' { 242 s = s[2:] 243 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 244 s = s[1:] 245 } 246 } 247 248 // e or E followed by an optional - or + and 249 // 1 or more digits. 250 if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') { 251 s = s[1:] 252 if s[0] == '+' || s[0] == '-' { 253 s = s[1:] 254 if s == "" { 255 return false 256 } 257 } 258 for len(s) > 0 && '0' <= s[0] && s[0] <= '9' { 259 s = s[1:] 260 } 261 } 262 263 // Make sure we are at the end. 264 return s == "" 265 } 266 267 // decodeState represents the state while decoding a JSON value. 268 type decodeState struct { 269 data []byte 270 off int // read offset in data 271 scan scanner 272 nextscan scanner // for calls to nextValue 273 errorContext struct { // provides context for type errors 274 Struct string 275 Field string 276 } 277 savedError error 278 useNumber bool 279 } 280 281 // errPhase is used for errors that should not happen unless 282 // there is a bug in the JSON decoder or something is editing 283 // the data slice while the decoder executes. 284 var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?") 285 286 func (d *decodeState) init(data []byte) *decodeState { 287 d.data = data 288 d.off = 0 289 d.savedError = nil 290 d.errorContext.Struct = "" 291 d.errorContext.Field = "" 292 return d 293 } 294 295 // error aborts the decoding by panicking with err. 296 func (d *decodeState) error(err error) { 297 panic(d.addErrorContext(err)) 298 } 299 300 // saveError saves the first err it is called with, 301 // for reporting at the end of the unmarshal. 302 func (d *decodeState) saveError(err error) { 303 if d.savedError == nil { 304 d.savedError = d.addErrorContext(err) 305 } 306 } 307 308 // addErrorContext returns a new error enhanced with information from d.errorContext 309 func (d *decodeState) addErrorContext(err error) error { 310 if d.errorContext.Struct != "" || d.errorContext.Field != "" { 311 switch err := err.(type) { 312 case *UnmarshalTypeError: 313 err.Struct = d.errorContext.Struct 314 err.Field = d.errorContext.Field 315 return err 316 } 317 } 318 return err 319 } 320 321 // next cuts off and returns the next full JSON value in d.data[d.off:]. 322 // The next value is known to be an object or array, not a literal. 323 func (d *decodeState) next() []byte { 324 c := d.data[d.off] 325 item, rest, err := nextValue(d.data[d.off:], &d.nextscan) 326 if err != nil { 327 d.error(err) 328 } 329 d.off = len(d.data) - len(rest) 330 331 // Our scanner has seen the opening brace/bracket 332 // and thinks we're still in the middle of the object. 333 // invent a closing brace/bracket to get it out. 334 if c == '{' { 335 d.scan.step(&d.scan, '}') 336 } else { 337 d.scan.step(&d.scan, ']') 338 } 339 340 return item 341 } 342 343 // scanWhile processes bytes in d.data[d.off:] until it 344 // receives a scan code not equal to op. 345 // It updates d.off and returns the new scan code. 346 func (d *decodeState) scanWhile(op int) int { 347 var newOp int 348 for { 349 if d.off >= len(d.data) { 350 newOp = d.scan.eof() 351 d.off = len(d.data) + 1 // mark processed EOF with len+1 352 } else { 353 c := d.data[d.off] 354 d.off++ 355 newOp = d.scan.step(&d.scan, c) 356 } 357 if newOp != op { 358 break 359 } 360 } 361 return newOp 362 } 363 364 // discardObject and discardArray are dummy data targets 365 // used by the (*decodeState).value method, which 366 // accepts a zero reflect.Value to discard a value. 367 // The (*decodeState).object and (*decodeState).array methods, 368 // however, require a valid reflect.Value destination. 369 // These are the target values used when the caller of value 370 // wants to skip a field. 371 // 372 // Because these values refer to zero-sized objects 373 // and thus can't be mutated, they're safe for concurrent use 374 // by different goroutines unmarshalling skipped fields. 375 var ( 376 discardObject = reflect.ValueOf(struct{}{}) 377 discardArray = reflect.ValueOf([0]interface{}{}) 378 ) 379 380 // value decodes a JSON value from d.data[d.off:] into the value. 381 // It updates d.off to point past the decoded value. If v is 382 // invalid, the JSON value is discarded. 383 func (d *decodeState) value(v reflect.Value) { 384 switch op := d.scanWhile(scanSkipSpace); op { 385 default: 386 d.error(errPhase) 387 388 case scanBeginArray: 389 if !v.IsValid() { 390 v = discardArray 391 } 392 d.array(v) 393 394 case scanBeginObject: 395 if !v.IsValid() { 396 v = discardObject 397 } 398 d.object(v) 399 400 case scanBeginLiteral: 401 d.literal(v) 402 } 403 } 404 405 type unquotedValue struct{} 406 407 // valueQuoted is like value but decodes a 408 // quoted string literal or literal null into an interface value. 409 // If it finds anything other than a quoted string literal or null, 410 // valueQuoted returns unquotedValue{}. 411 func (d *decodeState) valueQuoted() interface{} { 412 switch op := d.scanWhile(scanSkipSpace); op { 413 default: 414 d.error(errPhase) 415 416 case scanBeginArray: 417 d.array(reflect.Value{}) 418 419 case scanBeginObject: 420 d.object(reflect.Value{}) 421 422 case scanBeginLiteral: 423 switch v := d.literalInterface().(type) { 424 case nil, string: 425 return v 426 } 427 } 428 return unquotedValue{} 429 } 430 431 // indirect walks down v allocating pointers as needed, 432 // until it gets to a non-pointer. 433 // if it encounters an Unmarshaler, indirect stops and returns that. 434 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil. 435 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) { 436 // If v is a named type and is addressable, 437 // start with its address, so that if the type has pointer methods, 438 // we find them. 439 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() { 440 v = v.Addr() 441 } 442 for { 443 // Load value from interface, but only if the result will be 444 // usefully addressable. 445 if v.Kind() == reflect.Interface && !v.IsNil() { 446 e := v.Elem() 447 if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) { 448 v = e 449 continue 450 } 451 } 452 453 if v.Kind() != reflect.Ptr { 454 break 455 } 456 457 if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() { 458 break 459 } 460 if v.IsNil() { 461 v.Set(reflect.New(v.Type().Elem())) 462 } 463 if v.Type().NumMethod() > 0 { 464 if u, ok := v.Interface().(Unmarshaler); ok { 465 return u, nil, reflect.Value{} 466 } 467 if !decodingNull { 468 if u, ok := v.Interface().(encoding.TextUnmarshaler); ok { 469 return nil, u, reflect.Value{} 470 } 471 } 472 } 473 v = v.Elem() 474 } 475 return nil, nil, v 476 } 477 478 // array consumes an array from d.data[d.off-1:], decoding into the value v. 479 // the first byte of the array ('[') has been read already. 480 func (d *decodeState) array(v reflect.Value) { 481 // Check for unmarshaler. 482 u, ut, pv := d.indirect(v, false) 483 if u != nil { 484 d.off-- 485 err := u.UnmarshalJSON(d.next()) 486 if err != nil { 487 d.error(err) 488 } 489 return 490 } 491 if ut != nil { 492 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)}) 493 d.off-- 494 d.next() 495 return 496 } 497 498 v = pv 499 500 // Check type of target. 501 switch v.Kind() { 502 case reflect.Interface: 503 if v.NumMethod() == 0 { 504 // Decoding into nil interface? Switch to non-reflect code. 505 v.Set(reflect.ValueOf(d.arrayInterface())) 506 return 507 } 508 // Otherwise it's invalid. 509 fallthrough 510 default: 511 d.saveError(&UnmarshalTypeError{Value: "array", Type: v.Type(), Offset: int64(d.off)}) 512 d.off-- 513 d.next() 514 return 515 case reflect.Array, reflect.Slice: 516 break 517 } 518 519 i := 0 520 for { 521 // Look ahead for ] - can only happen on first iteration. 522 op := d.scanWhile(scanSkipSpace) 523 if op == scanEndArray { 524 break 525 } 526 527 // Back up so d.value can have the byte we just read. 528 d.off-- 529 d.scan.undo(op) 530 531 // Get element of array, growing if necessary. 532 if v.Kind() == reflect.Slice { 533 // Grow slice if necessary 534 if i >= v.Cap() { 535 newcap := v.Cap() + v.Cap()/2 536 if newcap < 4 { 537 newcap = 4 538 } 539 newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) 540 reflect.Copy(newv, v) 541 v.Set(newv) 542 } 543 if i >= v.Len() { 544 v.SetLen(i + 1) 545 } 546 } 547 548 if i < v.Len() { 549 // Decode into element. 550 d.value(v.Index(i)) 551 } else { 552 // Ran out of fixed array: skip. 553 d.value(reflect.Value{}) 554 } 555 i++ 556 557 // Next token must be , or ]. 558 op = d.scanWhile(scanSkipSpace) 559 if op == scanEndArray { 560 break 561 } 562 if op != scanArrayValue { 563 d.error(errPhase) 564 } 565 } 566 567 if i < v.Len() { 568 if v.Kind() == reflect.Array { 569 // Array. Zero the rest. 570 z := reflect.Zero(v.Type().Elem()) 571 for ; i < v.Len(); i++ { 572 v.Index(i).Set(z) 573 } 574 } else { 575 v.SetLen(i) 576 } 577 } 578 if i == 0 && v.Kind() == reflect.Slice { 579 v.Set(reflect.MakeSlice(v.Type(), 0, 0)) 580 } 581 } 582 583 var nullLiteral = []byte("null") 584 var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem() 585 586 // object consumes an object from d.data[d.off-1:], decoding into the value v. 587 // the first byte ('{') of the object has been read already. 588 func (d *decodeState) object(v reflect.Value) { 589 // Check for unmarshaler. 590 u, ut, pv := d.indirect(v, false) 591 if u != nil { 592 d.off-- 593 err := u.UnmarshalJSON(d.next()) 594 if err != nil { 595 d.error(err) 596 } 597 return 598 } 599 if ut != nil { 600 d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)}) 601 d.off-- 602 d.next() // skip over { } in input 603 return 604 } 605 v = pv 606 607 // Decoding into nil interface? Switch to non-reflect code. 608 if v.Kind() == reflect.Interface && v.NumMethod() == 0 { 609 v.Set(reflect.ValueOf(d.objectInterface())) 610 return 611 } 612 613 // Check type of target: 614 // struct or 615 // map[T1]T2 where T1 is string, an integer type, 616 // or an encoding.TextUnmarshaler 617 switch v.Kind() { 618 case reflect.Map: 619 // Map key must either have string kind, have an integer kind, 620 // or be an encoding.TextUnmarshaler. 621 t := v.Type() 622 switch t.Key().Kind() { 623 case reflect.String, 624 reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 625 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 626 default: 627 if !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) { 628 d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)}) 629 d.off-- 630 d.next() // skip over { } in input 631 return 632 } 633 } 634 if v.IsNil() { 635 v.Set(reflect.MakeMap(t)) 636 } 637 case reflect.Struct: 638 // ok 639 default: 640 d.saveError(&UnmarshalTypeError{Value: "object", Type: v.Type(), Offset: int64(d.off)}) 641 d.off-- 642 d.next() // skip over { } in input 643 return 644 } 645 646 var mapElem reflect.Value 647 648 for { 649 // Read opening " of string key or closing }. 650 op := d.scanWhile(scanSkipSpace) 651 if op == scanEndObject { 652 // closing } - can only happen on first iteration. 653 break 654 } 655 if op != scanBeginLiteral { 656 d.error(errPhase) 657 } 658 659 // Read key. 660 start := d.off - 1 661 op = d.scanWhile(scanContinue) 662 item := d.data[start : d.off-1] 663 key, ok := unquoteBytes(item) 664 if !ok { 665 d.error(errPhase) 666 } 667 668 // Figure out field corresponding to key. 669 var subv reflect.Value 670 destring := false // whether the value is wrapped in a string to be decoded first 671 672 if v.Kind() == reflect.Map { 673 elemType := v.Type().Elem() 674 if !mapElem.IsValid() { 675 mapElem = reflect.New(elemType).Elem() 676 } else { 677 mapElem.Set(reflect.Zero(elemType)) 678 } 679 subv = mapElem 680 } else { 681 var f *field 682 fields := cachedTypeFields(v.Type()) 683 for i := range fields { 684 ff := &fields[i] 685 if bytes.Equal(ff.nameBytes, key) { 686 f = ff 687 break 688 } 689 if f == nil && ff.equalFold(ff.nameBytes, key) { 690 f = ff 691 } 692 } 693 if f != nil { 694 subv = v 695 destring = f.quoted 696 for _, i := range f.index { 697 if subv.Kind() == reflect.Ptr { 698 if subv.IsNil() { 699 subv.Set(reflect.New(subv.Type().Elem())) 700 } 701 subv = subv.Elem() 702 } 703 subv = subv.Field(i) 704 } 705 d.errorContext.Field = f.name 706 d.errorContext.Struct = v.Type().Name() 707 } 708 } 709 710 // Read : before value. 711 if op == scanSkipSpace { 712 op = d.scanWhile(scanSkipSpace) 713 } 714 if op != scanObjectKey { 715 d.error(errPhase) 716 } 717 718 if destring { 719 switch qv := d.valueQuoted().(type) { 720 case nil: 721 d.literalStore(nullLiteral, subv, false) 722 case string: 723 d.literalStore([]byte(qv), subv, true) 724 default: 725 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal unquoted value into %v", subv.Type())) 726 } 727 } else { 728 d.value(subv) 729 } 730 731 // Write value back to map; 732 // if using struct, subv points into struct already. 733 if v.Kind() == reflect.Map { 734 kt := v.Type().Key() 735 var kv reflect.Value 736 switch { 737 case kt.Kind() == reflect.String: 738 kv = reflect.ValueOf(key).Convert(kt) 739 case reflect.PtrTo(kt).Implements(textUnmarshalerType): 740 kv = reflect.New(v.Type().Key()) 741 d.literalStore(item, kv, true) 742 kv = kv.Elem() 743 default: 744 switch kt.Kind() { 745 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 746 s := string(key) 747 n, err := strconv.ParseInt(s, 10, 64) 748 if err != nil || reflect.Zero(kt).OverflowInt(n) { 749 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)}) 750 return 751 } 752 kv = reflect.ValueOf(n).Convert(kt) 753 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 754 s := string(key) 755 n, err := strconv.ParseUint(s, 10, 64) 756 if err != nil || reflect.Zero(kt).OverflowUint(n) { 757 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)}) 758 return 759 } 760 kv = reflect.ValueOf(n).Convert(kt) 761 default: 762 panic("json: Unexpected key type") // should never occur 763 } 764 } 765 v.SetMapIndex(kv, subv) 766 } 767 768 // Next token must be , or }. 769 op = d.scanWhile(scanSkipSpace) 770 if op == scanEndObject { 771 break 772 } 773 if op != scanObjectValue { 774 d.error(errPhase) 775 } 776 777 d.errorContext.Struct = "" 778 d.errorContext.Field = "" 779 } 780 } 781 782 // literal consumes a literal from d.data[d.off-1:], decoding into the value v. 783 // The first byte of the literal has been read already 784 // (that's how the caller knows it's a literal). 785 func (d *decodeState) literal(v reflect.Value) { 786 // All bytes inside literal return scanContinue op code. 787 start := d.off - 1 788 op := d.scanWhile(scanContinue) 789 790 // Scan read one byte too far; back up. 791 d.off-- 792 d.scan.undo(op) 793 794 if v.IsValid() { 795 d.literalStore(d.data[start:d.off], v, false) 796 } 797 } 798 799 // convertNumber converts the number literal s to a float64 or a Number 800 // depending on the setting of d.useNumber. 801 func (d *decodeState) convertNumber(s string) (interface{}, error) { 802 if d.useNumber { 803 return Number(s), nil 804 } 805 f, err := strconv.ParseFloat(s, 64) 806 if err != nil { 807 return nil, &UnmarshalTypeError{Value: "number " + s, Type: reflect.TypeOf(0.0), Offset: int64(d.off)} 808 } 809 return f, nil 810 } 811 812 var numberType = reflect.TypeOf(Number("")) 813 814 // literalStore decodes a literal stored in item into v. 815 // 816 // fromQuoted indicates whether this literal came from unwrapping a 817 // string from the ",string" struct tag option. this is used only to 818 // produce more helpful error messages. 819 func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) { 820 // Check for unmarshaler. 821 if len(item) == 0 { 822 //Empty string given 823 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 824 return 825 } 826 isNull := item[0] == 'n' // null 827 u, ut, pv := d.indirect(v, isNull) 828 if u != nil { 829 err := u.UnmarshalJSON(item) 830 if err != nil { 831 d.error(err) 832 } 833 return 834 } 835 if ut != nil { 836 if item[0] != '"' { 837 if fromQuoted { 838 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 839 } else { 840 var val string 841 switch item[0] { 842 case 'n': 843 val = "null" 844 case 't', 'f': 845 val = "bool" 846 default: 847 val = "number" 848 } 849 d.saveError(&UnmarshalTypeError{Value: val, Type: v.Type(), Offset: int64(d.off)}) 850 } 851 return 852 } 853 s, ok := unquoteBytes(item) 854 if !ok { 855 if fromQuoted { 856 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 857 } else { 858 d.error(errPhase) 859 } 860 } 861 err := ut.UnmarshalText(s) 862 if err != nil { 863 d.error(err) 864 } 865 return 866 } 867 868 v = pv 869 870 switch c := item[0]; c { 871 case 'n': // null 872 // The main parser checks that only true and false can reach here, 873 // but if this was a quoted string input, it could be anything. 874 if fromQuoted && string(item) != "null" { 875 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 876 break 877 } 878 switch v.Kind() { 879 case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: 880 v.Set(reflect.Zero(v.Type())) 881 // otherwise, ignore null for primitives/string 882 } 883 case 't', 'f': // true, false 884 value := item[0] == 't' 885 // The main parser checks that only true and false can reach here, 886 // but if this was a quoted string input, it could be anything. 887 if fromQuoted && string(item) != "true" && string(item) != "false" { 888 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 889 break 890 } 891 switch v.Kind() { 892 default: 893 if fromQuoted { 894 d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 895 } else { 896 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.off)}) 897 } 898 case reflect.Bool: 899 v.SetBool(value) 900 case reflect.Interface: 901 if v.NumMethod() == 0 { 902 v.Set(reflect.ValueOf(value)) 903 } else { 904 d.saveError(&UnmarshalTypeError{Value: "bool", Type: v.Type(), Offset: int64(d.off)}) 905 } 906 } 907 908 case '"': // string 909 s, ok := unquoteBytes(item) 910 if !ok { 911 if fromQuoted { 912 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 913 } else { 914 d.error(errPhase) 915 } 916 } 917 switch v.Kind() { 918 default: 919 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.off)}) 920 case reflect.Slice: 921 if v.Type().Elem().Kind() != reflect.Uint8 { 922 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.off)}) 923 break 924 } 925 b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) 926 n, err := base64.StdEncoding.Decode(b, s) 927 if err != nil { 928 d.saveError(err) 929 break 930 } 931 v.SetBytes(b[:n]) 932 case reflect.String: 933 v.SetString(string(s)) 934 case reflect.Interface: 935 if v.NumMethod() == 0 { 936 v.Set(reflect.ValueOf(string(s))) 937 } else { 938 d.saveError(&UnmarshalTypeError{Value: "string", Type: v.Type(), Offset: int64(d.off)}) 939 } 940 } 941 942 default: // number 943 if c != '-' && (c < '0' || c > '9') { 944 if fromQuoted { 945 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 946 } else { 947 d.error(errPhase) 948 } 949 } 950 s := string(item) 951 switch v.Kind() { 952 default: 953 if v.Kind() == reflect.String && v.Type() == numberType { 954 v.SetString(s) 955 if !isValidNumber(s) { 956 d.error(fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item)) 957 } 958 break 959 } 960 if fromQuoted { 961 d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) 962 } else { 963 d.error(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.off)}) 964 } 965 case reflect.Interface: 966 n, err := d.convertNumber(s) 967 if err != nil { 968 d.saveError(err) 969 break 970 } 971 if v.NumMethod() != 0 { 972 d.saveError(&UnmarshalTypeError{Value: "number", Type: v.Type(), Offset: int64(d.off)}) 973 break 974 } 975 v.Set(reflect.ValueOf(n)) 976 977 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 978 n, err := strconv.ParseInt(s, 10, 64) 979 if err != nil || v.OverflowInt(n) { 980 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.off)}) 981 break 982 } 983 v.SetInt(n) 984 985 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 986 n, err := strconv.ParseUint(s, 10, 64) 987 if err != nil || v.OverflowUint(n) { 988 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.off)}) 989 break 990 } 991 v.SetUint(n) 992 993 case reflect.Float32, reflect.Float64: 994 n, err := strconv.ParseFloat(s, v.Type().Bits()) 995 if err != nil || v.OverflowFloat(n) { 996 d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: v.Type(), Offset: int64(d.off)}) 997 break 998 } 999 v.SetFloat(n) 1000 } 1001 } 1002 } 1003 1004 // The xxxInterface routines build up a value to be stored 1005 // in an empty interface. They are not strictly necessary, 1006 // but they avoid the weight of reflection in this common case. 1007 1008 // valueInterface is like value but returns interface{} 1009 func (d *decodeState) valueInterface() interface{} { 1010 switch d.scanWhile(scanSkipSpace) { 1011 default: 1012 d.error(errPhase) 1013 panic("unreachable") 1014 case scanBeginArray: 1015 return d.arrayInterface() 1016 case scanBeginObject: 1017 return d.objectInterface() 1018 case scanBeginLiteral: 1019 return d.literalInterface() 1020 } 1021 } 1022 1023 // arrayInterface is like array but returns []interface{}. 1024 func (d *decodeState) arrayInterface() []interface{} { 1025 var v = make([]interface{}, 0) 1026 for { 1027 // Look ahead for ] - can only happen on first iteration. 1028 op := d.scanWhile(scanSkipSpace) 1029 if op == scanEndArray { 1030 break 1031 } 1032 1033 // Back up so d.value can have the byte we just read. 1034 d.off-- 1035 d.scan.undo(op) 1036 1037 v = append(v, d.valueInterface()) 1038 1039 // Next token must be , or ]. 1040 op = d.scanWhile(scanSkipSpace) 1041 if op == scanEndArray { 1042 break 1043 } 1044 if op != scanArrayValue { 1045 d.error(errPhase) 1046 } 1047 } 1048 return v 1049 } 1050 1051 // objectInterface is like object but returns map[string]interface{}. 1052 func (d *decodeState) objectInterface() map[string]interface{} { 1053 m := make(map[string]interface{}) 1054 for { 1055 // Read opening " of string key or closing }. 1056 op := d.scanWhile(scanSkipSpace) 1057 if op == scanEndObject { 1058 // closing } - can only happen on first iteration. 1059 break 1060 } 1061 if op != scanBeginLiteral { 1062 d.error(errPhase) 1063 } 1064 1065 // Read string key. 1066 start := d.off - 1 1067 op = d.scanWhile(scanContinue) 1068 item := d.data[start : d.off-1] 1069 key, ok := unquote(item) 1070 if !ok { 1071 d.error(errPhase) 1072 } 1073 1074 // Read : before value. 1075 if op == scanSkipSpace { 1076 op = d.scanWhile(scanSkipSpace) 1077 } 1078 if op != scanObjectKey { 1079 d.error(errPhase) 1080 } 1081 1082 // Read value. 1083 m[key] = d.valueInterface() 1084 1085 // Next token must be , or }. 1086 op = d.scanWhile(scanSkipSpace) 1087 if op == scanEndObject { 1088 break 1089 } 1090 if op != scanObjectValue { 1091 d.error(errPhase) 1092 } 1093 } 1094 return m 1095 } 1096 1097 // literalInterface is like literal but returns an interface value. 1098 func (d *decodeState) literalInterface() interface{} { 1099 // All bytes inside literal return scanContinue op code. 1100 start := d.off - 1 1101 op := d.scanWhile(scanContinue) 1102 1103 // Scan read one byte too far; back up. 1104 d.off-- 1105 d.scan.undo(op) 1106 item := d.data[start:d.off] 1107 1108 switch c := item[0]; c { 1109 case 'n': // null 1110 return nil 1111 1112 case 't', 'f': // true, false 1113 return c == 't' 1114 1115 case '"': // string 1116 s, ok := unquote(item) 1117 if !ok { 1118 d.error(errPhase) 1119 } 1120 return s 1121 1122 default: // number 1123 if c != '-' && (c < '0' || c > '9') { 1124 d.error(errPhase) 1125 } 1126 n, err := d.convertNumber(string(item)) 1127 if err != nil { 1128 d.saveError(err) 1129 } 1130 return n 1131 } 1132 } 1133 1134 // getu4 decodes \uXXXX from the beginning of s, returning the hex value, 1135 // or it returns -1. 1136 func getu4(s []byte) rune { 1137 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { 1138 return -1 1139 } 1140 r, err := strconv.ParseUint(string(s[2:6]), 16, 64) 1141 if err != nil { 1142 return -1 1143 } 1144 return rune(r) 1145 } 1146 1147 // unquote converts a quoted JSON string literal s into an actual string t. 1148 // The rules are different than for Go, so cannot use strconv.Unquote. 1149 func unquote(s []byte) (t string, ok bool) { 1150 s, ok = unquoteBytes(s) 1151 t = string(s) 1152 return 1153 } 1154 1155 func unquoteBytes(s []byte) (t []byte, ok bool) { 1156 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { 1157 return 1158 } 1159 s = s[1 : len(s)-1] 1160 1161 // Check for unusual characters. If there are none, 1162 // then no unquoting is needed, so return a slice of the 1163 // original bytes. 1164 r := 0 1165 for r < len(s) { 1166 c := s[r] 1167 if c == '\\' || c == '"' || c < ' ' { 1168 break 1169 } 1170 if c < utf8.RuneSelf { 1171 r++ 1172 continue 1173 } 1174 rr, size := utf8.DecodeRune(s[r:]) 1175 if rr == utf8.RuneError && size == 1 { 1176 break 1177 } 1178 r += size 1179 } 1180 if r == len(s) { 1181 return s, true 1182 } 1183 1184 b := make([]byte, len(s)+2*utf8.UTFMax) 1185 w := copy(b, s[0:r]) 1186 for r < len(s) { 1187 // Out of room? Can only happen if s is full of 1188 // malformed UTF-8 and we're replacing each 1189 // byte with RuneError. 1190 if w >= len(b)-2*utf8.UTFMax { 1191 nb := make([]byte, (len(b)+utf8.UTFMax)*2) 1192 copy(nb, b[0:w]) 1193 b = nb 1194 } 1195 switch c := s[r]; { 1196 case c == '\\': 1197 r++ 1198 if r >= len(s) { 1199 return 1200 } 1201 switch s[r] { 1202 default: 1203 return 1204 case '"', '\\', '/', '\'': 1205 b[w] = s[r] 1206 r++ 1207 w++ 1208 case 'b': 1209 b[w] = '\b' 1210 r++ 1211 w++ 1212 case 'f': 1213 b[w] = '\f' 1214 r++ 1215 w++ 1216 case 'n': 1217 b[w] = '\n' 1218 r++ 1219 w++ 1220 case 'r': 1221 b[w] = '\r' 1222 r++ 1223 w++ 1224 case 't': 1225 b[w] = '\t' 1226 r++ 1227 w++ 1228 case 'u': 1229 r-- 1230 rr := getu4(s[r:]) 1231 if rr < 0 { 1232 return 1233 } 1234 r += 6 1235 if utf16.IsSurrogate(rr) { 1236 rr1 := getu4(s[r:]) 1237 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { 1238 // A valid pair; consume. 1239 r += 6 1240 w += utf8.EncodeRune(b[w:], dec) 1241 break 1242 } 1243 // Invalid surrogate; fall back to replacement rune. 1244 rr = unicode.ReplacementChar 1245 } 1246 w += utf8.EncodeRune(b[w:], rr) 1247 } 1248 1249 // Quote, control characters are invalid. 1250 case c == '"', c < ' ': 1251 return 1252 1253 // ASCII 1254 case c < utf8.RuneSelf: 1255 b[w] = c 1256 r++ 1257 w++ 1258 1259 // Coerce to well-formed UTF-8. 1260 default: 1261 rr, size := utf8.DecodeRune(s[r:]) 1262 r += size 1263 w += utf8.EncodeRune(b[w:], rr) 1264 } 1265 } 1266 return b[0:w], true 1267 }