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