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.
22 // Unmarshal parses the JSON-encoded data and stores the result
23 // in the value pointed to by v.
25 // Unmarshal uses the inverse of the encodings that
26 // Marshal uses, allocating maps, slices, and pointers as necessary,
27 // with the following additional rules:
29 // To unmarshal JSON into a pointer, Unmarshal first handles the case of
30 // the JSON being the JSON literal null. In that case, Unmarshal sets
31 // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
32 // the value pointed at by the pointer. If the pointer is nil, Unmarshal
33 // allocates a new value for it to point to.
35 // To unmarshal JSON into an interface value, Unmarshal unmarshals
36 // the JSON into the concrete value contained in the interface value.
37 // If the interface value is nil, that is, has no concrete value stored in it,
38 // Unmarshal stores one of these in the interface value:
40 // bool, for JSON booleans
41 // float64, for JSON numbers
42 // string, for JSON strings
43 // []interface{}, for JSON arrays
44 // map[string]interface{}, for JSON objects
47 // If a JSON value is not appropriate for a given target type,
48 // or if a JSON number overflows the target type, Unmarshal
49 // skips that field and completes the unmarshalling as best it can.
50 // If no more serious errors are encountered, Unmarshal returns
51 // an UnmarshalTypeError describing the earliest such error.
53 func Unmarshal(data []byte, v interface{}) os.Error {
54 d := new(decodeState).init(data)
56 // Quick check for well-formedness.
57 // Avoids filling out half a data structure
58 // before discovering a JSON syntax error.
59 err := checkValid(data, &d.scan)
67 // Unmarshaler is the interface implemented by objects
68 // that can unmarshal a JSON description of themselves.
69 // The input can be assumed to be a valid JSON object
70 // encoding. UnmarshalJSON must copy the JSON data
71 // if it wishes to retain the data after returning.
72 type Unmarshaler interface {
73 UnmarshalJSON([]byte) os.Error
76 // An UnmarshalTypeError describes a JSON value that was
77 // not appropriate for a value of a specific Go type.
78 type UnmarshalTypeError struct {
79 Value string // description of JSON value - "bool", "array", "number -5"
80 Type reflect.Type // type of Go value it could not be assigned to
83 func (e *UnmarshalTypeError) String() string {
84 return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
87 // An UnmarshalFieldError describes a JSON object key that
88 // led to an unexported (and therefore unwritable) struct field.
89 type UnmarshalFieldError struct {
92 Field reflect.StructField
95 func (e *UnmarshalFieldError) String() string {
96 return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
99 // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
100 // (The argument to Unmarshal must be a non-nil pointer.)
101 type InvalidUnmarshalError struct {
105 func (e *InvalidUnmarshalError) String() string {
107 return "json: Unmarshal(nil)"
110 if e.Type.Kind() != reflect.Ptr {
111 return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
113 return "json: Unmarshal(nil " + e.Type.String() + ")"
116 func (d *decodeState) unmarshal(v interface{}) (err os.Error) {
118 if r := recover(); r != nil {
119 if _, ok := r.(runtime.Error); ok {
126 rv := reflect.ValueOf(v)
128 if pv.Kind() != reflect.Ptr || pv.IsNil() {
129 return &InvalidUnmarshalError{reflect.TypeOf(v)}
133 // We decode rv not pv.Elem because the Unmarshaler interface
134 // test must be applied at the top level of the value.
139 // decodeState represents the state while decoding a JSON value.
140 type decodeState struct {
142 off int // read offset in data
144 nextscan scanner // for calls to nextValue
146 tempstr string // scratch space to avoid some allocations
149 // errPhase is used for errors that should not happen unless
150 // there is a bug in the JSON decoder or something is editing
151 // the data slice while the decoder executes.
152 var errPhase = os.NewError("JSON decoder out of sync - data changing underfoot?")
154 func (d *decodeState) init(data []byte) *decodeState {
161 // error aborts the decoding by panicking with err.
162 func (d *decodeState) error(err os.Error) {
166 // saveError saves the first err it is called with,
167 // for reporting at the end of the unmarshal.
168 func (d *decodeState) saveError(err os.Error) {
169 if d.savedError == nil {
174 // next cuts off and returns the next full JSON value in d.data[d.off:].
175 // The next value is known to be an object or array, not a literal.
176 func (d *decodeState) next() []byte {
178 item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
182 d.off = len(d.data) - len(rest)
184 // Our scanner has seen the opening brace/bracket
185 // and thinks we're still in the middle of the object.
186 // invent a closing brace/bracket to get it out.
188 d.scan.step(&d.scan, '}')
190 d.scan.step(&d.scan, ']')
196 // scanWhile processes bytes in d.data[d.off:] until it
197 // receives a scan code not equal to op.
198 // It updates d.off and returns the new scan code.
199 func (d *decodeState) scanWhile(op int) int {
202 if d.off >= len(d.data) {
204 d.off = len(d.data) + 1 // mark processed EOF with len+1
206 c := int(d.data[d.off])
208 newOp = d.scan.step(&d.scan, c)
217 // value decodes a JSON value from d.data[d.off:] into the value.
218 // it updates d.off to point past the decoded value.
219 func (d *decodeState) value(v reflect.Value) {
221 _, rest, err := nextValue(d.data[d.off:], &d.nextscan)
225 d.off = len(d.data) - len(rest)
227 // d.scan thinks we're still at the beginning of the item.
228 // Feed in an empty string - the shortest, simplest value -
229 // so that it knows we got to the end of the value.
230 if d.scan.step == stateRedo {
233 d.scan.step(&d.scan, '"')
234 d.scan.step(&d.scan, '"')
238 switch op := d.scanWhile(scanSkipSpace); op {
245 case scanBeginObject:
248 case scanBeginLiteral:
253 // indirect walks down v allocating pointers as needed,
254 // until it gets to a non-pointer.
255 // if it encounters an Unmarshaler, indirect stops and returns that.
256 // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
257 func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) {
258 // If v is a named type and is addressable,
259 // start with its address, so that if the type has pointer methods,
261 if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
265 var isUnmarshaler bool
266 if v.Type().NumMethod() > 0 {
267 // Remember that this is an unmarshaler,
268 // but wait to return it until after allocating
269 // the pointer (if necessary).
270 _, isUnmarshaler = v.Interface().(Unmarshaler)
273 if iv := v; iv.Kind() == reflect.Interface && !iv.IsNil() {
279 if pv.Kind() != reflect.Ptr {
283 if pv.Elem().Kind() != reflect.Ptr && decodingNull && pv.CanSet() {
287 pv.Set(reflect.New(pv.Type().Elem()))
290 // Using v.Interface().(Unmarshaler)
291 // here means that we have to use a pointer
292 // as the struct field. We cannot use a value inside
293 // a pointer to a struct, because in that case
294 // v.Interface() is the value (x.f) not the pointer (&x.f).
295 // This is an unfortunate consequence of reflect.
296 // An alternative would be to look up the
297 // UnmarshalJSON method and return a FuncValue.
298 return v.Interface().(Unmarshaler), reflect.Value{}
305 // array consumes an array from d.data[d.off-1:], decoding into the value v.
306 // the first byte of the array ('[') has been read already.
307 func (d *decodeState) array(v reflect.Value) {
308 // Check for unmarshaler.
309 unmarshaler, pv := d.indirect(v, false)
310 if unmarshaler != nil {
312 err := unmarshaler.UnmarshalJSON(d.next())
320 // Decoding into nil interface? Switch to non-reflect code.
322 ok := iv.Kind() == reflect.Interface
324 iv.Set(reflect.ValueOf(d.arrayInterface()))
328 // Check type of target.
330 if av.Kind() != reflect.Array && av.Kind() != reflect.Slice {
331 d.saveError(&UnmarshalTypeError{"array", v.Type()})
341 // Look ahead for ] - can only happen on first iteration.
342 op := d.scanWhile(scanSkipSpace)
343 if op == scanEndArray {
347 // Back up so d.value can have the byte we just read.
351 // Get element of array, growing if necessary.
352 if i >= av.Cap() && sv.IsValid() {
353 newcap := sv.Cap() + sv.Cap()/2
357 newv := reflect.MakeSlice(sv.Type(), sv.Len(), newcap)
358 reflect.Copy(newv, sv)
361 if i >= av.Len() && sv.IsValid() {
362 // Must be slice; gave up on array during i >= av.Cap().
366 // Decode into element.
370 // Ran out of fixed array: skip.
371 d.value(reflect.Value{})
375 // Next token must be , or ].
376 op = d.scanWhile(scanSkipSpace)
377 if op == scanEndArray {
380 if op != scanArrayValue {
386 // Array. Zero the rest.
387 z := reflect.Zero(av.Type().Elem())
388 for ; i < av.Len(); i++ {
397 // object consumes an object from d.data[d.off-1:], decoding into the value v.
398 // the first byte of the object ('{') has been read already.
399 func (d *decodeState) object(v reflect.Value) {
400 // Check for unmarshaler.
401 unmarshaler, pv := d.indirect(v, false)
402 if unmarshaler != nil {
404 err := unmarshaler.UnmarshalJSON(d.next())
412 // Decoding into nil interface? Switch to non-reflect code.
414 if iv.Kind() == reflect.Interface {
415 iv.Set(reflect.ValueOf(d.objectInterface()))
419 // Check type of target: struct or map[string]T
426 // map must have string type
428 if t.Key() != reflect.TypeOf("") {
429 d.saveError(&UnmarshalTypeError{"object", v.Type()})
434 mv.Set(reflect.MakeMap(t))
439 d.saveError(&UnmarshalTypeError{"object", v.Type()})
442 if !mv.IsValid() && !sv.IsValid() {
444 d.next() // skip over { } in input
448 var mapElem reflect.Value
451 // Read opening " of string key or closing }.
452 op := d.scanWhile(scanSkipSpace)
453 if op == scanEndObject {
454 // closing } - can only happen on first iteration.
457 if op != scanBeginLiteral {
463 op = d.scanWhile(scanContinue)
464 item := d.data[start : d.off-1]
465 key, ok := unquote(item)
470 // Figure out field corresponding to key.
471 var subv reflect.Value
472 destring := false // whether the value is wrapped in a string to be decoded first
475 elemType := mv.Type().Elem()
476 if !mapElem.IsValid() {
477 mapElem = reflect.New(elemType).Elem()
479 mapElem.Set(reflect.Zero(elemType))
483 var f reflect.StructField
486 for i := 0; i < sv.NumField(); i++ {
488 tag := sf.Tag.Get("json")
490 // Pretend this field doesn't exist.
494 tagName, _ := parseTag(tag)
498 break // no better match possible
500 // Second, exact field name match
505 // Third, case-insensitive field name match,
506 // but only if a better match hasn't already been seen
507 if !ok && strings.EqualFold(sf.Name, key) {
513 // Extract value; name must be exported.
516 d.saveError(&UnmarshalFieldError{key, st, f})
518 subv = sv.FieldByIndex(f.Index)
520 _, opts := parseTag(f.Tag.Get("json"))
521 destring = opts.Contains("string")
525 // Read : before value.
526 if op == scanSkipSpace {
527 op = d.scanWhile(scanSkipSpace)
529 if op != scanObjectKey {
535 d.value(reflect.ValueOf(&d.tempstr))
536 d.literalStore([]byte(d.tempstr), subv)
540 // Write value back to map;
541 // if using struct, subv points into struct already.
543 mv.SetMapIndex(reflect.ValueOf(key), subv)
546 // Next token must be , or }.
547 op = d.scanWhile(scanSkipSpace)
548 if op == scanEndObject {
551 if op != scanObjectValue {
557 // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
558 // The first byte of the literal has been read already
559 // (that's how the caller knows it's a literal).
560 func (d *decodeState) literal(v reflect.Value) {
561 // All bytes inside literal return scanContinue op code.
563 op := d.scanWhile(scanContinue)
565 // Scan read one byte too far; back up.
569 d.literalStore(d.data[start:d.off], v)
572 // literalStore decodes a literal stored in item into v.
573 func (d *decodeState) literalStore(item []byte, v reflect.Value) {
574 // Check for unmarshaler.
575 wantptr := item[0] == 'n' // null
576 unmarshaler, pv := d.indirect(v, wantptr)
577 if unmarshaler != nil {
578 err := unmarshaler.UnmarshalJSON(item)
586 switch c := item[0]; c {
590 d.saveError(&UnmarshalTypeError{"null", v.Type()})
591 case reflect.Interface, reflect.Ptr, reflect.Map:
592 v.Set(reflect.Zero(v.Type()))
595 case 't', 'f': // true, false
599 d.saveError(&UnmarshalTypeError{"bool", v.Type()})
602 case reflect.Interface:
603 v.Set(reflect.ValueOf(value))
607 s, ok := unquoteBytes(item)
613 d.saveError(&UnmarshalTypeError{"string", v.Type()})
615 if v.Type() != byteSliceType {
616 d.saveError(&UnmarshalTypeError{"string", v.Type()})
619 b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
620 n, err := base64.StdEncoding.Decode(b, s)
625 v.Set(reflect.ValueOf(b[0:n]))
627 v.SetString(string(s))
628 case reflect.Interface:
629 v.Set(reflect.ValueOf(string(s)))
633 if c != '-' && (c < '0' || c > '9') {
639 d.error(&UnmarshalTypeError{"number", v.Type()})
640 case reflect.Interface:
641 n, err := strconv.Atof64(s)
643 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
646 v.Set(reflect.ValueOf(n))
648 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
649 n, err := strconv.Atoi64(s)
650 if err != nil || v.OverflowInt(n) {
651 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
656 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
657 n, err := strconv.Atoui64(s)
658 if err != nil || v.OverflowUint(n) {
659 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
664 case reflect.Float32, reflect.Float64:
665 n, err := strconv.AtofN(s, v.Type().Bits())
666 if err != nil || v.OverflowFloat(n) {
667 d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
675 // The xxxInterface routines build up a value to be stored
676 // in an empty interface. They are not strictly necessary,
677 // but they avoid the weight of reflection in this common case.
679 // valueInterface is like value but returns interface{}
680 func (d *decodeState) valueInterface() interface{} {
681 switch d.scanWhile(scanSkipSpace) {
685 return d.arrayInterface()
686 case scanBeginObject:
687 return d.objectInterface()
688 case scanBeginLiteral:
689 return d.literalInterface()
694 // arrayInterface is like array but returns []interface{}.
695 func (d *decodeState) arrayInterface() []interface{} {
698 // Look ahead for ] - can only happen on first iteration.
699 op := d.scanWhile(scanSkipSpace)
700 if op == scanEndArray {
704 // Back up so d.value can have the byte we just read.
708 v = append(v, d.valueInterface())
710 // Next token must be , or ].
711 op = d.scanWhile(scanSkipSpace)
712 if op == scanEndArray {
715 if op != scanArrayValue {
722 // objectInterface is like object but returns map[string]interface{}.
723 func (d *decodeState) objectInterface() map[string]interface{} {
724 m := make(map[string]interface{})
726 // Read opening " of string key or closing }.
727 op := d.scanWhile(scanSkipSpace)
728 if op == scanEndObject {
729 // closing } - can only happen on first iteration.
732 if op != scanBeginLiteral {
738 op = d.scanWhile(scanContinue)
739 item := d.data[start : d.off-1]
740 key, ok := unquote(item)
745 // Read : before value.
746 if op == scanSkipSpace {
747 op = d.scanWhile(scanSkipSpace)
749 if op != scanObjectKey {
754 m[key] = d.valueInterface()
756 // Next token must be , or }.
757 op = d.scanWhile(scanSkipSpace)
758 if op == scanEndObject {
761 if op != scanObjectValue {
768 // literalInterface is like literal but returns an interface value.
769 func (d *decodeState) literalInterface() interface{} {
770 // All bytes inside literal return scanContinue op code.
772 op := d.scanWhile(scanContinue)
774 // Scan read one byte too far; back up.
777 item := d.data[start:d.off]
779 switch c := item[0]; c {
783 case 't', 'f': // true, false
787 s, ok := unquote(item)
794 if c != '-' && (c < '0' || c > '9') {
797 n, err := strconv.Atof64(string(item))
799 d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.TypeOf(0.0)})
806 // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
808 func getu4(s []byte) int {
809 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
812 rune, err := strconv.Btoui64(string(s[2:6]), 16)
819 // unquote converts a quoted JSON string literal s into an actual string t.
820 // The rules are different than for Go, so cannot use strconv.Unquote.
821 func unquote(s []byte) (t string, ok bool) {
822 s, ok = unquoteBytes(s)
827 func unquoteBytes(s []byte) (t []byte, ok bool) {
828 if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
833 // Check for unusual characters. If there are none,
834 // then no unquoting is needed, so return a slice of the
839 if c == '\\' || c == '"' || c < ' ' {
842 if c < utf8.RuneSelf {
846 rune, size := utf8.DecodeRune(s[r:])
847 if rune == utf8.RuneError && size == 1 {
856 b := make([]byte, len(s)+2*utf8.UTFMax)
859 // Out of room? Can only happen if s is full of
860 // malformed UTF-8 and we're replacing each
861 // byte with RuneError.
862 if w >= len(b)-2*utf8.UTFMax {
863 nb := make([]byte, (len(b)+utf8.UTFMax)*2)
876 case '"', '\\', '/', '\'':
907 if utf16.IsSurrogate(rune) {
908 rune1 := getu4(s[r:])
909 if dec := utf16.DecodeRune(rune, rune1); dec != unicode.ReplacementChar {
910 // A valid pair; consume.
912 w += utf8.EncodeRune(b[w:], dec)
915 // Invalid surrogate; fall back to replacement rune.
916 rune = unicode.ReplacementChar
918 w += utf8.EncodeRune(b[w:], rune)
921 // Quote, control characters are invalid.
922 case c == '"', c < ' ':
926 case c < utf8.RuneSelf:
931 // Coerce to well-formed UTF-8.
933 rune, size := utf8.DecodeRune(s[r:])
935 w += utf8.EncodeRune(b[w:], rune)