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