github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/encoding/json/decode.go (about)

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