github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/encoding/json/decode.go (about)

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