...

Source file src/encoding/json/decode.go

Documentation: encoding/json

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

View as plain text