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.
5 // Represents JSON data structure using native Go types: booleans, floats,
6 // strings, arrays, and maps.
24 // Unmarshal parses the JSON-encoded data and stores the result
25 // in the value pointed to by v.
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:
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.
37 // To unmarshal JSON into a struct, Unmarshal matches incoming object
38 // keys to the keys used by Marshal (either the struct field name or its tag),
39 // preferring an exact match but also accepting a case-insensitive match.
41 // To unmarshal JSON into an interface value,
42 // Unmarshal stores one of these in the interface value:
44 // bool, for JSON booleans
45 // float64, for JSON numbers
46 // string, for JSON strings
47 // []interface{}, for JSON arrays
48 // map[string]interface{}, for JSON objects
51 // If a JSON value is not appropriate for a given target type,
52 // or if a JSON number overflows the target type, Unmarshal
53 // skips that field and completes the unmarshalling as best it can.
54 // If no more serious errors are encountered, Unmarshal returns
55 // an UnmarshalTypeError describing the earliest such error.
57 // When unmarshaling quoted strings, invalid UTF-8 or
58 // invalid UTF-16 surrogate pairs are not treated as an error.
59 // Instead, they are replaced by the Unicode replacement
62 func Unmarshal(data
[]byte, v
interface{}) error
{
63 // Check for well-formedness.
64 // Avoids filling out half a data structure
65 // before discovering a JSON syntax error.
67 err
:= checkValid(data
, &d
.scan
)
76 // Unmarshaler is the interface implemented by objects
77 // that can unmarshal a JSON description of themselves.
78 // The input can be assumed to be a valid encoding of
79 // a JSON value. UnmarshalJSON must copy the JSON data
80 // if it wishes to retain the data after returning.
81 type Unmarshaler
interface {
82 UnmarshalJSON([]byte) error
85 // An UnmarshalTypeError describes a JSON value that was
86 // not appropriate for a value of a specific Go type.
87 type UnmarshalTypeError
struct {
88 Value
string // description of JSON value - "bool", "array", "number -5"
89 Type reflect
.Type
// type of Go value it could not be assigned to
92 func (e
*UnmarshalTypeError
) Error() string {
93 return "json: cannot unmarshal " + e
.Value
+ " into Go value of type " + e
.Type
.String()
96 // An UnmarshalFieldError describes a JSON object key that
97 // led to an unexported (and therefore unwritable) struct field.
98 // (No longer used; kept for compatibility.)
99 type UnmarshalFieldError
struct {
102 Field reflect
.StructField
105 func (e
*UnmarshalFieldError
) Error() string {
106 return "json: cannot unmarshal object key " + strconv
.Quote(e
.Key
) + " into unexported field " + e
.Field
.Name
+ " of type " + e
.Type
.String()
109 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
110 // (The argument to Unmarshal must be a non-nil pointer.)
111 type InvalidUnmarshalError
struct {
115 func (e
*InvalidUnmarshalError
) Error() string {
117 return "json: Unmarshal(nil)"
120 if e
.Type
.Kind() != reflect
.Ptr
{
121 return "json: Unmarshal(non-pointer " + e
.Type
.String() + ")"
123 return "json: Unmarshal(nil " + e
.Type
.String() + ")"
126 func (d
*decodeState
) unmarshal(v
interface{}) (err error
) {
128 if r
:= recover(); r
!= nil {
129 if _
, ok
:= r
.(runtime
.Error
); ok
{
136 rv
:= reflect
.ValueOf(v
)
137 if rv
.Kind() != reflect
.Ptr || rv
.IsNil() {
138 return &InvalidUnmarshalError
{reflect
.TypeOf(v
)}
142 // We decode rv not rv.Elem because the Unmarshaler interface
143 // test must be applied at the top level of the value.
148 // A Number represents a JSON number literal.
151 // String returns the literal text of the number.
152 func (n Number
) String() string { return string(n
) }
154 // Float64 returns the number as a float64.
155 func (n Number
) Float64() (float64, error
) {
156 return strconv
.ParseFloat(string(n
), 64)
159 // Int64 returns the number as an int64.
160 func (n Number
) Int64() (int64, error
) {
161 return strconv
.ParseInt(string(n
), 10, 64)
164 // decodeState represents the state while decoding a JSON value.
165 type decodeState
struct {
167 off
int // read offset in data
169 nextscan scanner
// for calls to nextValue
171 tempstr
string // scratch space to avoid some allocations
175 // errPhase is used for errors that should not happen unless
176 // there is a bug in the JSON decoder or something is editing
177 // the data slice while the decoder executes.
178 var errPhase
= errors
.New("JSON decoder out of sync - data changing underfoot?")
180 func (d
*decodeState
) init(data
[]byte) *decodeState
{
187 // error aborts the decoding by panicking with err.
188 func (d
*decodeState
) error(err error
) {
192 // saveError saves the first err it is called with,
193 // for reporting at the end of the unmarshal.
194 func (d
*decodeState
) saveError(err error
) {
195 if d
.savedError
== nil {
200 // next cuts off and returns the next full JSON value in d.data[d.off:].
201 // The next value is known to be an object or array, not a literal.
202 func (d
*decodeState
) next() []byte {
204 item
, rest
, err
:= nextValue(d
.data
[d
.off
:], &d
.nextscan
)
208 d
.off
= len(d
.data
) - len(rest
)
210 // Our scanner has seen the opening brace/bracket
211 // and thinks we're still in the middle of the object.
212 // invent a closing brace/bracket to get it out.
214 d
.scan
.step(&d
.scan
, '}')
216 d
.scan
.step(&d
.scan
, ']')
222 // scanWhile processes bytes in d.data[d.off:] until it
223 // receives a scan code not equal to op.
224 // It updates d.off and returns the new scan code.
225 func (d
*decodeState
) scanWhile(op
int) int {
228 if d
.off
>= len(d
.data
) {
230 d
.off
= len(d
.data
) + 1 // mark processed EOF with len+1
232 c
:= int(d
.data
[d
.off
])
234 newOp
= d
.scan
.step(&d
.scan
, c
)
243 // value decodes a JSON value from d.data[d.off:] into the value.
244 // it updates d.off to point past the decoded value.
245 func (d
*decodeState
) value(v reflect
.Value
) {
247 _
, rest
, err
:= nextValue(d
.data
[d
.off
:], &d
.nextscan
)
251 d
.off
= len(d
.data
) - len(rest
)
253 // d.scan thinks we're still at the beginning of the item.
254 // Feed in an empty string - the shortest, simplest value -
255 // so that it knows we got to the end of the value.
259 d
.scan
.step
= stateBeginValue
261 d
.scan
.step(&d
.scan
, '"')
262 d
.scan
.step(&d
.scan
, '"')
264 n
:= len(d
.scan
.parseState
)
265 if n
> 0 && d
.scan
.parseState
[n
-1] == parseObjectKey
{
266 // d.scan thinks we just read an object key; finish the object
267 d
.scan
.step(&d
.scan
, ':')
268 d
.scan
.step(&d
.scan
, '"')
269 d
.scan
.step(&d
.scan
, '"')
270 d
.scan
.step(&d
.scan
, '}')
276 switch op
:= d
.scanWhile(scanSkipSpace
); op
{
283 case scanBeginObject
:
286 case scanBeginLiteral
:
291 // indirect walks down v allocating pointers as needed,
292 // until it gets to a non-pointer.
293 // if it encounters an Unmarshaler, indirect stops and returns that.
294 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
295 func (d
*decodeState
) indirect(v reflect
.Value
, decodingNull
bool) (Unmarshaler
, encoding
.TextUnmarshaler
, reflect
.Value
) {
296 // If v is a named type and is addressable,
297 // start with its address, so that if the type has pointer methods,
299 if v
.Kind() != reflect
.Ptr
&& v
.Type().Name() != "" && v
.CanAddr() {
303 // Load value from interface, but only if the result will be
304 // usefully addressable.
305 if v
.Kind() == reflect
.Interface
&& !v
.IsNil() {
307 if e
.Kind() == reflect
.Ptr
&& !e
.IsNil() && (!decodingNull || e
.Elem().Kind() == reflect
.Ptr
) {
313 if v
.Kind() != reflect
.Ptr
{
317 if v
.Elem().Kind() != reflect
.Ptr
&& decodingNull
&& v
.CanSet() {
321 v
.Set(reflect
.New(v
.Type().Elem()))
323 if v
.Type().NumMethod() > 0 {
324 if u
, ok
:= v
.Interface().(Unmarshaler
); ok
{
325 return u
, nil, reflect
.Value
{}
327 if u
, ok
:= v
.Interface().(encoding
.TextUnmarshaler
); ok
{
328 return nil, u
, reflect
.Value
{}
336 // array consumes an array from d.data[d.off-1:], decoding into the value v.
337 // the first byte of the array ('[') has been read already.
338 func (d
*decodeState
) array(v reflect
.Value
) {
339 // Check for unmarshaler.
340 u
, ut
, pv
:= d
.indirect(v
, false)
343 err
:= u
.UnmarshalJSON(d
.next())
350 d
.saveError(&UnmarshalTypeError
{"array", v
.Type()})
358 // Check type of target.
360 case reflect
.Interface
:
361 if v
.NumMethod() == 0 {
362 // Decoding into nil interface? Switch to non-reflect code.
363 v
.Set(reflect
.ValueOf(d
.arrayInterface()))
366 // Otherwise it's invalid.
369 d
.saveError(&UnmarshalTypeError
{"array", v
.Type()})
380 // Look ahead for ] - can only happen on first iteration.
381 op
:= d
.scanWhile(scanSkipSpace
)
382 if op
== scanEndArray
{
386 // Back up so d.value can have the byte we just read.
390 // Get element of array, growing if necessary.
391 if v
.Kind() == reflect
.Slice
{
392 // Grow slice if necessary
394 newcap
:= v
.Cap() + v
.Cap()/2
398 newv
:= reflect
.MakeSlice(v
.Type(), v
.Len(), newcap
)
399 reflect
.Copy(newv
, v
)
408 // Decode into element.
411 // Ran out of fixed array: skip.
412 d
.value(reflect
.Value
{})
416 // Next token must be , or ].
417 op
= d
.scanWhile(scanSkipSpace
)
418 if op
== scanEndArray
{
421 if op
!= scanArrayValue
{
427 if v
.Kind() == reflect
.Array
{
428 // Array. Zero the rest.
429 z
:= reflect
.Zero(v
.Type().Elem())
430 for ; i
< v
.Len(); i
++ {
437 if i
== 0 && v
.Kind() == reflect
.Slice
{
438 v
.Set(reflect
.MakeSlice(v
.Type(), 0, 0))
442 // object consumes an object from d.data[d.off-1:], decoding into the value v.
443 // the first byte of the object ('{') has been read already.
444 func (d
*decodeState
) object(v reflect
.Value
) {
445 // Check for unmarshaler.
446 u
, ut
, pv
:= d
.indirect(v
, false)
449 err
:= u
.UnmarshalJSON(d
.next())
456 d
.saveError(&UnmarshalTypeError
{"object", v
.Type()})
458 d
.next() // skip over { } in input
463 // Decoding into nil interface? Switch to non-reflect code.
464 if v
.Kind() == reflect
.Interface
&& v
.NumMethod() == 0 {
465 v
.Set(reflect
.ValueOf(d
.objectInterface()))
469 // Check type of target: struct or map[string]T
472 // map must have string kind
474 if t
.Key().Kind() != reflect
.String
{
475 d
.saveError(&UnmarshalTypeError
{"object", v
.Type()})
479 v
.Set(reflect
.MakeMap(t
))
484 d
.saveError(&UnmarshalTypeError
{"object", v
.Type()})
486 d
.next() // skip over { } in input
490 var mapElem reflect
.Value
493 // Read opening " of string key or closing }.
494 op
:= d
.scanWhile(scanSkipSpace
)
495 if op
== scanEndObject
{
496 // closing } - can only happen on first iteration.
499 if op
!= scanBeginLiteral
{
505 op
= d
.scanWhile(scanContinue
)
506 item
:= d
.data
[start
: d
.off
-1]
507 key
, ok
:= unquoteBytes(item
)
512 // Figure out field corresponding to key.
513 var subv reflect
.Value
514 destring
:= false // whether the value is wrapped in a string to be decoded first
516 if v
.Kind() == reflect
.Map
{
517 elemType
:= v
.Type().Elem()
518 if !mapElem
.IsValid() {
519 mapElem
= reflect
.New(elemType
).Elem()
521 mapElem
.Set(reflect
.Zero(elemType
))
526 fields
:= cachedTypeFields(v
.Type())
527 for i
:= range fields
{
529 if bytes
.Equal(ff
.nameBytes
, key
) {
533 if f
== nil && ff
.equalFold(ff
.nameBytes
, key
) {
540 for _
, i
:= range f
.index
{
541 if subv
.Kind() == reflect
.Ptr
{
543 subv
.Set(reflect
.New(subv
.Type().Elem()))
552 // Read : before value.
553 if op
== scanSkipSpace
{
554 op
= d
.scanWhile(scanSkipSpace
)
556 if op
!= scanObjectKey
{
562 d
.value(reflect
.ValueOf(&d
.tempstr
))
563 d
.literalStore([]byte(d
.tempstr
), subv
, true)
564 d
.tempstr
= "" // Zero scratch space for successive values.
569 // Write value back to map;
570 // if using struct, subv points into struct already.
571 if v
.Kind() == reflect
.Map
{
572 kv
:= reflect
.ValueOf(key
).Convert(v
.Type().Key())
573 v
.SetMapIndex(kv
, subv
)
576 // Next token must be , or }.
577 op
= d
.scanWhile(scanSkipSpace
)
578 if op
== scanEndObject
{
581 if op
!= scanObjectValue
{
587 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
588 // The first byte of the literal has been read already
589 // (that's how the caller knows it's a literal).
590 func (d
*decodeState
) literal(v reflect
.Value
) {
591 // All bytes inside literal return scanContinue op code.
593 op
:= d
.scanWhile(scanContinue
)
595 // Scan read one byte too far; back up.
599 d
.literalStore(d
.data
[start
:d
.off
], v
, false)
602 // convertNumber converts the number literal s to a float64 or a Number
603 // depending on the setting of d.useNumber.
604 func (d
*decodeState
) convertNumber(s
string) (interface{}, error
) {
606 return Number(s
), nil
608 f
, err
:= strconv
.ParseFloat(s
, 64)
610 return nil, &UnmarshalTypeError
{"number " + s
, reflect
.TypeOf(0.0)}
615 var numberType
= reflect
.TypeOf(Number(""))
617 // literalStore decodes a literal stored in item into v.
619 // fromQuoted indicates whether this literal came from unwrapping a
620 // string from the ",string" struct tag option. this is used only to
621 // produce more helpful error messages.
622 func (d
*decodeState
) literalStore(item
[]byte, v reflect
.Value
, fromQuoted
bool) {
623 // Check for unmarshaler.
626 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
629 wantptr
:= item
[0] == 'n' // null
630 u
, ut
, pv
:= d
.indirect(v
, wantptr
)
632 err
:= u
.UnmarshalJSON(item
)
641 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
643 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
646 s
, ok
:= unquoteBytes(item
)
649 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
654 err
:= ut
.UnmarshalText(s
)
663 switch c
:= item
[0]; c
{
666 case reflect
.Interface
, reflect
.Ptr
, reflect
.Map
, reflect
.Slice
:
667 v
.Set(reflect
.Zero(v
.Type()))
668 // otherwise, ignore null for primitives/string
670 case 't', 'f': // true, false
675 d
.saveError(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
677 d
.saveError(&UnmarshalTypeError
{"bool", v
.Type()})
681 case reflect
.Interface
:
682 if v
.NumMethod() == 0 {
683 v
.Set(reflect
.ValueOf(value
))
685 d
.saveError(&UnmarshalTypeError
{"bool", v
.Type()})
690 s
, ok
:= unquoteBytes(item
)
693 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
700 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
702 if v
.Type() != byteSliceType
{
703 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
706 b
:= make([]byte, base64
.StdEncoding
.DecodedLen(len(s
)))
707 n
, err
:= base64
.StdEncoding
.Decode(b
, s
)
712 v
.Set(reflect
.ValueOf(b
[0:n
]))
714 v
.SetString(string(s
))
715 case reflect
.Interface
:
716 if v
.NumMethod() == 0 {
717 v
.Set(reflect
.ValueOf(string(s
)))
719 d
.saveError(&UnmarshalTypeError
{"string", v
.Type()})
724 if c
!= '-' && (c
< '0' || c
> '9') {
726 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
734 if v
.Kind() == reflect
.String
&& v
.Type() == numberType
{
739 d
.error(fmt
.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item
, v
.Type()))
741 d
.error(&UnmarshalTypeError
{"number", v
.Type()})
743 case reflect
.Interface
:
744 n
, err
:= d
.convertNumber(s
)
749 if v
.NumMethod() != 0 {
750 d
.saveError(&UnmarshalTypeError
{"number", v
.Type()})
753 v
.Set(reflect
.ValueOf(n
))
755 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
756 n
, err
:= strconv
.ParseInt(s
, 10, 64)
757 if err
!= nil || v
.OverflowInt(n
) {
758 d
.saveError(&UnmarshalTypeError
{"number " + s
, v
.Type()})
763 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
764 n
, err
:= strconv
.ParseUint(s
, 10, 64)
765 if err
!= nil || v
.OverflowUint(n
) {
766 d
.saveError(&UnmarshalTypeError
{"number " + s
, v
.Type()})
771 case reflect
.Float32
, reflect
.Float64
:
772 n
, err
:= strconv
.ParseFloat(s
, v
.Type().Bits())
773 if err
!= nil || v
.OverflowFloat(n
) {
774 d
.saveError(&UnmarshalTypeError
{"number " + s
, v
.Type()})
782 // The xxxInterface routines build up a value to be stored
783 // in an empty interface. They are not strictly necessary,
784 // but they avoid the weight of reflection in this common case.
786 // valueInterface is like value but returns interface{}
787 func (d
*decodeState
) valueInterface() interface{} {
788 switch d
.scanWhile(scanSkipSpace
) {
793 return d
.arrayInterface()
794 case scanBeginObject
:
795 return d
.objectInterface()
796 case scanBeginLiteral
:
797 return d
.literalInterface()
801 // arrayInterface is like array but returns []interface{}.
802 func (d
*decodeState
) arrayInterface() []interface{} {
803 var v
= make([]interface{}, 0)
805 // Look ahead for ] - can only happen on first iteration.
806 op
:= d
.scanWhile(scanSkipSpace
)
807 if op
== scanEndArray
{
811 // Back up so d.value can have the byte we just read.
815 v
= append(v
, d
.valueInterface())
817 // Next token must be , or ].
818 op
= d
.scanWhile(scanSkipSpace
)
819 if op
== scanEndArray
{
822 if op
!= scanArrayValue
{
829 // objectInterface is like object but returns map[string]interface{}.
830 func (d
*decodeState
) objectInterface() map[string]interface{} {
831 m
:= make(map[string]interface{})
833 // Read opening " of string key or closing }.
834 op
:= d
.scanWhile(scanSkipSpace
)
835 if op
== scanEndObject
{
836 // closing } - can only happen on first iteration.
839 if op
!= scanBeginLiteral
{
845 op
= d
.scanWhile(scanContinue
)
846 item
:= d
.data
[start
: d
.off
-1]
847 key
, ok
:= unquote(item
)
852 // Read : before value.
853 if op
== scanSkipSpace
{
854 op
= d
.scanWhile(scanSkipSpace
)
856 if op
!= scanObjectKey
{
861 m
[key
] = d
.valueInterface()
863 // Next token must be , or }.
864 op
= d
.scanWhile(scanSkipSpace
)
865 if op
== scanEndObject
{
868 if op
!= scanObjectValue
{
875 // literalInterface is like literal but returns an interface value.
876 func (d
*decodeState
) literalInterface() interface{} {
877 // All bytes inside literal return scanContinue op code.
879 op
:= d
.scanWhile(scanContinue
)
881 // Scan read one byte too far; back up.
884 item
:= d
.data
[start
:d
.off
]
886 switch c
:= item
[0]; c
{
890 case 't', 'f': // true, false
894 s
, ok
:= unquote(item
)
901 if c
!= '-' && (c
< '0' || c
> '9') {
904 n
, err
:= d
.convertNumber(string(item
))
912 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
914 func getu4(s
[]byte) rune
{
915 if len(s
) < 6 || s
[0] != '\\' || s
[1] != 'u' {
918 r
, err
:= strconv
.ParseUint(string(s
[2:6]), 16, 64)
925 // unquote converts a quoted JSON string literal s into an actual string t.
926 // The rules are different than for Go, so cannot use strconv.Unquote.
927 func unquote(s
[]byte) (t
string, ok
bool) {
928 s
, ok
= unquoteBytes(s
)
933 func unquoteBytes(s
[]byte) (t
[]byte, ok
bool) {
934 if len(s
) < 2 || s
[0] != '"' || s
[len(s
)-1] != '"' {
939 // Check for unusual characters. If there are none,
940 // then no unquoting is needed, so return a slice of the
945 if c
== '\\' || c
== '"' || c
< ' ' {
948 if c
< utf8
.RuneSelf
{
952 rr
, size
:= utf8
.DecodeRune(s
[r
:])
953 if rr
== utf8
.RuneError
&& size
== 1 {
962 b
:= make([]byte, len(s
)+2*utf8
.UTFMax
)
965 // Out of room? Can only happen if s is full of
966 // malformed UTF-8 and we're replacing each
967 // byte with RuneError.
968 if w
>= len(b
)-2*utf8
.UTFMax
{
969 nb
:= make([]byte, (len(b
)+utf8
.UTFMax
)*2)
982 case '"', '\\', '/', '\'':
1013 if utf16
.IsSurrogate(rr
) {
1015 if dec
:= utf16
.DecodeRune(rr
, rr1
); dec
!= unicode
.ReplacementChar
{
1016 // A valid pair; consume.
1018 w
+= utf8
.EncodeRune(b
[w
:], dec
)
1021 // Invalid surrogate; fall back to replacement rune.
1022 rr
= unicode
.ReplacementChar
1024 w
+= utf8
.EncodeRune(b
[w
:], rr
)
1027 // Quote, control characters are invalid.
1028 case c
== '"', c
< ' ':
1032 case c
< utf8
.RuneSelf
:
1037 // Coerce to well-formed UTF-8.
1039 rr
, size
:= utf8
.DecodeRune(s
[r
:])
1041 w
+= utf8
.EncodeRune(b
[w
:], rr
)