...
Run Format

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

View as plain text