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