...
Run Format

Source file src/encoding/json/decode.go

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

View as plain text