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