github.com/qiniu/dyn@v1.3.0/text/internal/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  	"encoding/base64"
    12  	"errors"
    13  	"fmt"
    14  	"reflect"
    15  	"runtime"
    16  	"strconv"
    17  	"strings"
    18  	"unicode"
    19  	"unicode/utf16"
    20  	"unicode/utf8"
    21  )
    22  
    23  // Unmarshal parses the JSON-encoded data and stores the result
    24  // in the value pointed to by v.
    25  //
    26  // Unmarshal uses the inverse of the encodings that
    27  // Marshal uses, allocating maps, slices, and pointers as necessary,
    28  // with the following additional rules:
    29  //
    30  // To unmarshal JSON into a pointer, Unmarshal first handles the case of
    31  // the JSON being the JSON literal null.  In that case, Unmarshal sets
    32  // the pointer to nil.  Otherwise, Unmarshal unmarshals the JSON into
    33  // the value pointed at by the pointer.  If the pointer is nil, Unmarshal
    34  // allocates a new value for it to point to.
    35  //
    36  // To unmarshal JSON into an interface value, Unmarshal unmarshals
    37  // the JSON into the concrete value contained in the interface value.
    38  // If the interface value is nil, that is, has no concrete value stored in it,
    39  // Unmarshal stores one of these in the interface value:
    40  //
    41  //	bool, for JSON booleans
    42  //	float64, for JSON numbers
    43  //	string, for JSON strings
    44  //	[]interface{}, for JSON arrays
    45  //	map[string]interface{}, for JSON objects
    46  //	nil for JSON null
    47  //
    48  // If a JSON value is not appropriate for a given target type,
    49  // or if a JSON number overflows the target type, Unmarshal
    50  // skips that field and completes the unmarshalling as best it can.
    51  // If no more serious errors are encountered, Unmarshal returns
    52  // an UnmarshalTypeError describing the earliest such error.
    53  //
    54  func Unmarshal(data []byte, v interface{}) error {
    55  	d := new(decodeState).init(data)
    56  
    57  	// Quick check for well-formedness.
    58  	// Avoids filling out half a data structure
    59  	// before discovering a JSON syntax error.
    60  	err := checkValid(data, &d.scan)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	return d.unmarshal(v)
    66  }
    67  
    68  // Unmarshaler is the interface implemented by objects
    69  // that can unmarshal a JSON description of themselves.
    70  // The input can be assumed to be a valid JSON object
    71  // encoding.  UnmarshalJSON must copy the JSON data
    72  // if it wishes to retain the data after returning.
    73  type Unmarshaler interface {
    74  	UnmarshalJSON([]byte) error
    75  }
    76  
    77  // An UnmarshalTypeError describes a JSON value that was
    78  // not appropriate for a value of a specific Go type.
    79  type UnmarshalTypeError struct {
    80  	Value string       // description of JSON value - "bool", "array", "number -5"
    81  	Type  reflect.Type // type of Go value it could not be assigned to
    82  }
    83  
    84  func (e *UnmarshalTypeError) Error() string {
    85  	return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
    86  }
    87  
    88  // An UnmarshalFieldError describes a JSON object key that
    89  // led to an unexported (and therefore unwritable) struct field.
    90  type UnmarshalFieldError struct {
    91  	Key   string
    92  	Type  reflect.Type
    93  	Field reflect.StructField
    94  }
    95  
    96  func (e *UnmarshalFieldError) Error() string {
    97  	return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String()
    98  }
    99  
   100  // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
   101  // (The argument to Unmarshal must be a non-nil pointer.)
   102  type InvalidUnmarshalError struct {
   103  	Type reflect.Type
   104  }
   105  
   106  func (e *InvalidUnmarshalError) Error() string {
   107  	if e.Type == nil {
   108  		return "json: Unmarshal(nil)"
   109  	}
   110  
   111  	if e.Type.Kind() != reflect.Ptr {
   112  		return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
   113  	}
   114  	return "json: Unmarshal(nil " + e.Type.String() + ")"
   115  }
   116  
   117  func (d *decodeState) unmarshal(v interface{}) (err error) {
   118  	defer func() {
   119  		if r := recover(); r != nil {
   120  			if _, ok := r.(runtime.Error); ok {
   121  				panic(r)
   122  			}
   123  			err = r.(error)
   124  		}
   125  	}()
   126  
   127  	rv := reflect.ValueOf(v)
   128  	pv := rv
   129  	if pv.Kind() != reflect.Ptr || pv.IsNil() {
   130  		return &InvalidUnmarshalError{reflect.TypeOf(v)}
   131  	}
   132  
   133  	d.scan.reset()
   134  	// We decode rv not pv.Elem because the Unmarshaler interface
   135  	// test must be applied at the top level of the value.
   136  	d.value(rv)
   137  	return d.savedError
   138  }
   139  
   140  // decodeState represents the state while decoding a JSON value.
   141  type decodeState struct {
   142  	data       []byte
   143  	off        int // read offset in data
   144  	scan       scanner
   145  	nextscan   scanner // for calls to nextValue
   146  	savedError error
   147  	tempstr    string // scratch space to avoid some allocations
   148  }
   149  
   150  // errPhase is used for errors that should not happen unless
   151  // there is a bug in the JSON decoder or something is editing
   152  // the data slice while the decoder executes.
   153  var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?")
   154  
   155  func (d *decodeState) init(data []byte) *decodeState {
   156  	d.data = data
   157  	d.off = 0
   158  	d.savedError = nil
   159  	return d
   160  }
   161  
   162  // error aborts the decoding by panicking with err.
   163  func (d *decodeState) error(err error) {
   164  	panic(err)
   165  }
   166  
   167  // saveError saves the first err it is called with,
   168  // for reporting at the end of the unmarshal.
   169  func (d *decodeState) saveError(err error) {
   170  	if d.savedError == nil {
   171  		d.savedError = err
   172  	}
   173  }
   174  
   175  // next cuts off and returns the next full JSON value in d.data[d.off:].
   176  // The next value is known to be an object or array, not a literal.
   177  func (d *decodeState) next() []byte {
   178  	c := d.data[d.off]
   179  	item, rest, err := nextValue(d.data[d.off:], &d.nextscan)
   180  	if err != nil {
   181  		d.error(err)
   182  	}
   183  	d.off = len(d.data) - len(rest)
   184  
   185  	// Our scanner has seen the opening brace/bracket
   186  	// and thinks we're still in the middle of the object.
   187  	// invent a closing brace/bracket to get it out.
   188  	if c == '{' {
   189  		d.scan.step(&d.scan, '}')
   190  	} else {
   191  		d.scan.step(&d.scan, ']')
   192  	}
   193  
   194  	return item
   195  }
   196  
   197  // scanWhile processes bytes in d.data[d.off:] until it
   198  // receives a scan code not equal to op.
   199  // It updates d.off and returns the new scan code.
   200  func (d *decodeState) scanWhile(op int) int {
   201  	var newOp int
   202  	for {
   203  		if d.off >= len(d.data) {
   204  			newOp = d.scan.eof()
   205  			d.off = len(d.data) + 1 // mark processed EOF with len+1
   206  		} else {
   207  			c := int(d.data[d.off])
   208  			d.off++
   209  			newOp = d.scan.step(&d.scan, c)
   210  		}
   211  		if newOp != op {
   212  			break
   213  		}
   214  	}
   215  	return newOp
   216  }
   217  
   218  // value decodes a JSON value from d.data[d.off:] into the value.
   219  // it updates d.off to point past the decoded value.
   220  func (d *decodeState) value(v reflect.Value) {
   221  	if !v.IsValid() {
   222  		_, rest, err := nextValue(d.data[d.off:], &d.nextscan)
   223  		if err != nil {
   224  			d.error(err)
   225  		}
   226  		d.off = len(d.data) - len(rest)
   227  
   228  		// d.scan thinks we're still at the beginning of the item.
   229  		// Feed in an empty string - the shortest, simplest value -
   230  		// so that it knows we got to the end of the value.
   231  		if d.scan.redo {
   232  			// rewind.
   233  			d.scan.redo = false
   234  			d.scan.step = stateBeginValue
   235  		}
   236  		d.scan.step(&d.scan, '"')
   237  		d.scan.step(&d.scan, '"')
   238  		return
   239  	}
   240  
   241  	switch op := d.scanWhile(scanSkipSpace); op {
   242  	default:
   243  		d.error(errPhase)
   244  
   245  	case scanBeginArray:
   246  		d.array(v)
   247  
   248  	case scanBeginObject:
   249  		d.object(v)
   250  
   251  	case scanBeginLiteral:
   252  		d.literal(v)
   253  	}
   254  }
   255  
   256  // indirect walks down v allocating pointers as needed,
   257  // until it gets to a non-pointer.
   258  // if it encounters an Unmarshaler, indirect stops and returns that.
   259  // if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
   260  func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) {
   261  	// If v is a named type and is addressable,
   262  	// start with its address, so that if the type has pointer methods,
   263  	// we find them.
   264  	if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
   265  		v = v.Addr()
   266  	}
   267  	for {
   268  		var isUnmarshaler bool
   269  		if v.Type().NumMethod() > 0 {
   270  			// Remember that this is an unmarshaler,
   271  			// but wait to return it until after allocating
   272  			// the pointer (if necessary).
   273  			_, isUnmarshaler = v.Interface().(Unmarshaler)
   274  		}
   275  
   276  		// Load value from interface, but only if the result will be
   277  		// usefully addressable.
   278  		if iv := v; iv.Kind() == reflect.Interface && !iv.IsNil() {
   279  			e := iv.Elem()
   280  			if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
   281  				v = e
   282  				continue
   283  			}
   284  		}
   285  
   286  		pv := v
   287  		if pv.Kind() != reflect.Ptr {
   288  			break
   289  		}
   290  
   291  		if pv.Elem().Kind() != reflect.Ptr && decodingNull && pv.CanSet() {
   292  			return nil, pv
   293  		}
   294  		if pv.IsNil() {
   295  			pv.Set(reflect.New(pv.Type().Elem()))
   296  		}
   297  		if isUnmarshaler {
   298  			// Using v.Interface().(Unmarshaler)
   299  			// here means that we have to use a pointer
   300  			// as the struct field.  We cannot use a value inside
   301  			// a pointer to a struct, because in that case
   302  			// v.Interface() is the value (x.f) not the pointer (&x.f).
   303  			// This is an unfortunate consequence of reflect.
   304  			// An alternative would be to look up the
   305  			// UnmarshalJSON method and return a FuncValue.
   306  			return v.Interface().(Unmarshaler), reflect.Value{}
   307  		}
   308  		v = pv.Elem()
   309  	}
   310  	return nil, v
   311  }
   312  
   313  // array consumes an array from d.data[d.off-1:], decoding into the value v.
   314  // the first byte of the array ('[') has been read already.
   315  func (d *decodeState) array(v reflect.Value) {
   316  	// Check for unmarshaler.
   317  	unmarshaler, pv := d.indirect(v, false)
   318  	if unmarshaler != nil {
   319  		d.off--
   320  		err := unmarshaler.UnmarshalJSON(d.next())
   321  		if err != nil {
   322  			d.error(err)
   323  		}
   324  		return
   325  	}
   326  	v = pv
   327  
   328  	// Check type of target.
   329  	switch v.Kind() {
   330  	default:
   331  		d.saveError(&UnmarshalTypeError{"array", v.Type()})
   332  		d.off--
   333  		d.next()
   334  		return
   335  	case reflect.Interface:
   336  		// Decoding into nil interface?  Switch to non-reflect code.
   337  		v.Set(reflect.ValueOf(d.arrayInterface()))
   338  		return
   339  	case reflect.Array:
   340  	case reflect.Slice:
   341  		break
   342  	}
   343  
   344  	i := 0
   345  	for {
   346  		// Look ahead for ] - can only happen on first iteration.
   347  		op := d.scanWhile(scanSkipSpace)
   348  		if op == scanEndArray {
   349  			break
   350  		}
   351  
   352  		// Back up so d.value can have the byte we just read.
   353  		d.off--
   354  		d.scan.undo(op)
   355  
   356  		// Get element of array, growing if necessary.
   357  		if v.Kind() == reflect.Slice {
   358  			// Grow slice if necessary
   359  			if i >= v.Cap() {
   360  				newcap := v.Cap() + v.Cap()/2
   361  				if newcap < 4 {
   362  					newcap = 4
   363  				}
   364  				newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
   365  				reflect.Copy(newv, v)
   366  				v.Set(newv)
   367  			}
   368  			if i >= v.Len() {
   369  				v.SetLen(i + 1)
   370  			}
   371  		}
   372  
   373  		if i < v.Len() {
   374  			// Decode into element.
   375  			d.value(v.Index(i))
   376  		} else {
   377  			// Ran out of fixed array: skip.
   378  			d.value(reflect.Value{})
   379  		}
   380  		i++
   381  
   382  		// Next token must be , or ].
   383  		op = d.scanWhile(scanSkipSpace)
   384  		if op == scanEndArray {
   385  			break
   386  		}
   387  		if op != scanArrayValue {
   388  			d.error(errPhase)
   389  		}
   390  	}
   391  
   392  	if i < v.Len() {
   393  		if v.Kind() == reflect.Array {
   394  			// Array.  Zero the rest.
   395  			z := reflect.Zero(v.Type().Elem())
   396  			for ; i < v.Len(); i++ {
   397  				v.Index(i).Set(z)
   398  			}
   399  		} else {
   400  			v.SetLen(i)
   401  		}
   402  	}
   403  	if i == 0 && v.Kind() == reflect.Slice {
   404  		v.Set(reflect.MakeSlice(v.Type(), 0, 0))
   405  	}
   406  }
   407  
   408  // object consumes an object from d.data[d.off-1:], decoding into the value v.
   409  // the first byte of the object ('{') has been read already.
   410  func (d *decodeState) object(v reflect.Value) {
   411  	// Check for unmarshaler.
   412  	unmarshaler, pv := d.indirect(v, false)
   413  	if unmarshaler != nil {
   414  		d.off--
   415  		err := unmarshaler.UnmarshalJSON(d.next())
   416  		if err != nil {
   417  			d.error(err)
   418  		}
   419  		return
   420  	}
   421  	v = pv
   422  
   423  	// Decoding into nil interface?  Switch to non-reflect code.
   424  	iv := v
   425  	if iv.Kind() == reflect.Interface {
   426  		iv.Set(reflect.ValueOf(d.objectInterface()))
   427  		return
   428  	}
   429  
   430  	// Check type of target: struct or map[string]T
   431  	var (
   432  		mv reflect.Value
   433  		sv reflect.Value
   434  	)
   435  	switch v.Kind() {
   436  	case reflect.Map:
   437  		// map must have string type
   438  		t := v.Type()
   439  		if t.Key() != reflect.TypeOf("") {
   440  			d.saveError(&UnmarshalTypeError{"object", v.Type()})
   441  			break
   442  		}
   443  		mv = v
   444  		if mv.IsNil() {
   445  			mv.Set(reflect.MakeMap(t))
   446  		}
   447  	case reflect.Struct:
   448  		sv = v
   449  	default:
   450  		d.saveError(&UnmarshalTypeError{"object", v.Type()})
   451  	}
   452  
   453  	if !mv.IsValid() && !sv.IsValid() {
   454  		d.off--
   455  		d.next() // skip over { } in input
   456  		return
   457  	}
   458  
   459  	var mapElem reflect.Value
   460  
   461  	for {
   462  		// Read opening " of string key or closing }.
   463  		op := d.scanWhile(scanSkipSpace)
   464  		if op == scanEndObject {
   465  			// closing } - can only happen on first iteration.
   466  			break
   467  		}
   468  		if op != scanBeginLiteral {
   469  			d.error(errPhase)
   470  		}
   471  
   472  		// Read string key.
   473  		start := d.off - 1
   474  		op = d.scanWhile(scanContinue)
   475  		item := d.data[start : d.off-1]
   476  		key, ok := unquote(item)
   477  		if !ok {
   478  			d.error(errPhase)
   479  		}
   480  
   481  		// Figure out field corresponding to key.
   482  		var subv reflect.Value
   483  		destring := false // whether the value is wrapped in a string to be decoded first
   484  
   485  		if mv.IsValid() {
   486  			elemType := mv.Type().Elem()
   487  			if !mapElem.IsValid() {
   488  				mapElem = reflect.New(elemType).Elem()
   489  			} else {
   490  				mapElem.Set(reflect.Zero(elemType))
   491  			}
   492  			subv = mapElem
   493  		} else {
   494  			var f reflect.StructField
   495  			var ok bool
   496  			st := sv.Type()
   497  			for i := 0; i < sv.NumField(); i++ {
   498  				sf := st.Field(i)
   499  				tag := sf.Tag.Get("json")
   500  				if tag == "-" {
   501  					// Pretend this field doesn't exist.
   502  					continue
   503  				}
   504  				if sf.Anonymous {
   505  					// Pretend this field doesn't exist,
   506  					// so that we can do a good job with
   507  					// these in a later version.
   508  					continue
   509  				}
   510  				// First, tag match
   511  				tagName, _ := parseTag(tag)
   512  				if tagName == key {
   513  					f = sf
   514  					ok = true
   515  					break // no better match possible
   516  				}
   517  				// Second, exact field name match
   518  				if sf.Name == key {
   519  					f = sf
   520  					ok = true
   521  				}
   522  				// Third, case-insensitive field name match,
   523  				// but only if a better match hasn't already been seen
   524  				if !ok && strings.EqualFold(sf.Name, key) {
   525  					f = sf
   526  					ok = true
   527  				}
   528  			}
   529  
   530  			// Extract value; name must be exported.
   531  			if ok {
   532  				if f.PkgPath != "" {
   533  					d.saveError(&UnmarshalFieldError{key, st, f})
   534  				} else {
   535  					subv = sv.FieldByIndex(f.Index)
   536  				}
   537  				_, opts := parseTag(f.Tag.Get("json"))
   538  				destring = opts.Contains("string")
   539  			}
   540  		}
   541  
   542  		// Read : before value.
   543  		if op == scanSkipSpace {
   544  			op = d.scanWhile(scanSkipSpace)
   545  		}
   546  		if op != scanObjectKey {
   547  			d.error(errPhase)
   548  		}
   549  
   550  		// Read value.
   551  		if destring {
   552  			d.value(reflect.ValueOf(&d.tempstr))
   553  			d.literalStore([]byte(d.tempstr), subv, true)
   554  		} else {
   555  			d.value(subv)
   556  		}
   557  		// Write value back to map;
   558  		// if using struct, subv points into struct already.
   559  		if mv.IsValid() {
   560  			mv.SetMapIndex(reflect.ValueOf(key), subv)
   561  		}
   562  
   563  		// Next token must be , or }.
   564  		op = d.scanWhile(scanSkipSpace)
   565  		if op == scanEndObject {
   566  			break
   567  		}
   568  		if op != scanObjectValue {
   569  			d.error(errPhase)
   570  		}
   571  	}
   572  }
   573  
   574  // literal consumes a literal from d.data[d.off-1:], decoding into the value v.
   575  // The first byte of the literal has been read already
   576  // (that's how the caller knows it's a literal).
   577  func (d *decodeState) literal(v reflect.Value) {
   578  	// All bytes inside literal return scanContinue op code.
   579  	start := d.off - 1
   580  	op := d.scanWhile(scanContinue)
   581  
   582  	// Scan read one byte too far; back up.
   583  	d.off--
   584  	d.scan.undo(op)
   585  
   586  	d.literalStore(d.data[start:d.off], v, false)
   587  }
   588  
   589  // literalStore decodes a literal stored in item into v.
   590  //
   591  // fromQuoted indicates whether this literal came from unwrapping a
   592  // string from the ",string" struct tag option. this is used only to
   593  // produce more helpful error messages.
   594  func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
   595  	// Check for unmarshaler.
   596  	if len(item) == 0 {
   597  		//Empty string given
   598  		d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   599  		return
   600  	}
   601  	wantptr := item[0] == 'n' // null
   602  	unmarshaler, pv := d.indirect(v, wantptr)
   603  	if unmarshaler != nil {
   604  		err := unmarshaler.UnmarshalJSON(item)
   605  		if err != nil {
   606  			d.error(err)
   607  		}
   608  		return
   609  	}
   610  	v = pv
   611  
   612  	switch c := item[0]; c {
   613  	case 'n': // null
   614  		switch v.Kind() {
   615  		default:
   616  			d.saveError(&UnmarshalTypeError{"null", v.Type()})
   617  		case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
   618  			v.Set(reflect.Zero(v.Type()))
   619  		}
   620  
   621  	case 't', 'f': // true, false
   622  		value := c == 't'
   623  		switch v.Kind() {
   624  		default:
   625  			if fromQuoted {
   626  				d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   627  			} else {
   628  				d.saveError(&UnmarshalTypeError{"bool", v.Type()})
   629  			}
   630  		case reflect.Bool:
   631  			v.SetBool(value)
   632  		case reflect.Interface:
   633  			v.Set(reflect.ValueOf(value))
   634  		}
   635  
   636  	case '"': // string
   637  		s, ok := unquoteBytes(item)
   638  		if !ok {
   639  			if fromQuoted {
   640  				d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   641  			} else {
   642  				d.error(errPhase)
   643  			}
   644  		}
   645  		switch v.Kind() {
   646  		default:
   647  			d.saveError(&UnmarshalTypeError{"string", v.Type()})
   648  		case reflect.Slice:
   649  			if v.Type() != byteSliceType {
   650  				d.saveError(&UnmarshalTypeError{"string", v.Type()})
   651  				break
   652  			}
   653  			b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
   654  			n, err := base64.StdEncoding.Decode(b, s)
   655  			if err != nil {
   656  				d.saveError(err)
   657  				break
   658  			}
   659  			v.Set(reflect.ValueOf(b[0:n]))
   660  		case reflect.String:
   661  			v.SetString(string(s))
   662  		case reflect.Interface:
   663  			v.Set(reflect.ValueOf(string(s)))
   664  		}
   665  
   666  	default: // number
   667  		if c != '-' && (c < '0' || c > '9') {
   668  			if fromQuoted {
   669  				d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   670  			} else {
   671  				d.error(errPhase)
   672  			}
   673  		}
   674  		s := string(item)
   675  		switch v.Kind() {
   676  		default:
   677  			if fromQuoted {
   678  				d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
   679  			} else {
   680  				d.error(&UnmarshalTypeError{"number", v.Type()})
   681  			}
   682  		case reflect.Interface:
   683  			n, err := strconv.ParseFloat(s, 64)
   684  			if err != nil {
   685  				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
   686  				break
   687  			}
   688  			v.Set(reflect.ValueOf(n))
   689  
   690  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   691  			n, err := strconv.ParseInt(s, 10, 64)
   692  			if err != nil || v.OverflowInt(n) {
   693  				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
   694  				break
   695  			}
   696  			v.SetInt(n)
   697  
   698  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   699  			n, err := strconv.ParseUint(s, 10, 64)
   700  			if err != nil || v.OverflowUint(n) {
   701  				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
   702  				break
   703  			}
   704  			v.SetUint(n)
   705  
   706  		case reflect.Float32, reflect.Float64:
   707  			n, err := strconv.ParseFloat(s, v.Type().Bits())
   708  			if err != nil || v.OverflowFloat(n) {
   709  				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
   710  				break
   711  			}
   712  			v.SetFloat(n)
   713  		}
   714  	}
   715  }
   716  
   717  // The xxxInterface routines build up a value to be stored
   718  // in an empty interface.  They are not strictly necessary,
   719  // but they avoid the weight of reflection in this common case.
   720  
   721  // valueInterface is like value but returns interface{}
   722  func (d *decodeState) valueInterface() interface{} {
   723  	switch d.scanWhile(scanSkipSpace) {
   724  	default:
   725  		d.error(errPhase)
   726  	case scanBeginArray:
   727  		return d.arrayInterface()
   728  	case scanBeginObject:
   729  		return d.objectInterface()
   730  	case scanBeginLiteral:
   731  		return d.literalInterface()
   732  	}
   733  	panic("unreachable")
   734  }
   735  
   736  // arrayInterface is like array but returns []interface{}.
   737  func (d *decodeState) arrayInterface() []interface{} {
   738  	var v []interface{}
   739  	for {
   740  		// Look ahead for ] - can only happen on first iteration.
   741  		op := d.scanWhile(scanSkipSpace)
   742  		if op == scanEndArray {
   743  			break
   744  		}
   745  
   746  		// Back up so d.value can have the byte we just read.
   747  		d.off--
   748  		d.scan.undo(op)
   749  
   750  		v = append(v, d.valueInterface())
   751  
   752  		// Next token must be , or ].
   753  		op = d.scanWhile(scanSkipSpace)
   754  		if op == scanEndArray {
   755  			break
   756  		}
   757  		if op != scanArrayValue {
   758  			d.error(errPhase)
   759  		}
   760  	}
   761  	return v
   762  }
   763  
   764  // objectInterface is like object but returns map[string]interface{}.
   765  func (d *decodeState) objectInterface() map[string]interface{} {
   766  	m := make(map[string]interface{})
   767  	for {
   768  		// Read opening " of string key or closing }.
   769  		op := d.scanWhile(scanSkipSpace)
   770  		if op == scanEndObject {
   771  			// closing } - can only happen on first iteration.
   772  			break
   773  		}
   774  		if op != scanBeginLiteral {
   775  			d.error(errPhase)
   776  		}
   777  
   778  		// Read string key.
   779  		start := d.off - 1
   780  		op = d.scanWhile(scanContinue)
   781  		item := d.data[start : d.off-1]
   782  		key, ok := unquote(item)
   783  		if !ok {
   784  			d.error(errPhase)
   785  		}
   786  
   787  		// Read : before value.
   788  		if op == scanSkipSpace {
   789  			op = d.scanWhile(scanSkipSpace)
   790  		}
   791  		if op != scanObjectKey {
   792  			d.error(errPhase)
   793  		}
   794  
   795  		// Read value.
   796  		m[key] = d.valueInterface()
   797  
   798  		// Next token must be , or }.
   799  		op = d.scanWhile(scanSkipSpace)
   800  		if op == scanEndObject {
   801  			break
   802  		}
   803  		if op != scanObjectValue {
   804  			d.error(errPhase)
   805  		}
   806  	}
   807  	return m
   808  }
   809  
   810  // literalInterface is like literal but returns an interface value.
   811  func (d *decodeState) literalInterface() interface{} {
   812  	// All bytes inside literal return scanContinue op code.
   813  	start := d.off - 1
   814  	op := d.scanWhile(scanContinue)
   815  
   816  	// Scan read one byte too far; back up.
   817  	d.off--
   818  	d.scan.undo(op)
   819  	item := d.data[start:d.off]
   820  
   821  	switch c := item[0]; c {
   822  	case 'n': // null
   823  		return nil
   824  
   825  	case 't', 'f': // true, false
   826  		return c == 't'
   827  
   828  	case '"': // string
   829  		s, ok := unquote(item)
   830  		if !ok {
   831  			d.error(errPhase)
   832  		}
   833  		return s
   834  
   835  	default: // number
   836  		if c != '-' && (c < '0' || c > '9') {
   837  			d.error(errPhase)
   838  		}
   839  		n, err := strconv.ParseFloat(string(item), 64)
   840  		if err != nil {
   841  			d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.TypeOf(0.0)})
   842  		}
   843  		return n
   844  	}
   845  	panic("unreachable")
   846  }
   847  
   848  // getu4 decodes \uXXXX from the beginning of s, returning the hex value,
   849  // or it returns -1.
   850  func getu4(s []byte) rune {
   851  	if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
   852  		return -1
   853  	}
   854  	r, err := strconv.ParseUint(string(s[2:6]), 16, 64)
   855  	if err != nil {
   856  		return -1
   857  	}
   858  	return rune(r)
   859  }
   860  
   861  // unquote converts a quoted JSON string literal s into an actual string t.
   862  // The rules are different than for Go, so cannot use strconv.Unquote.
   863  func unquote(s []byte) (t string, ok bool) {
   864  	s, ok = unquoteBytes(s)
   865  	t = string(s)
   866  	return
   867  }
   868  
   869  func unquoteBytes(s []byte) (t []byte, ok bool) {
   870  	if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
   871  		return
   872  	}
   873  	s = s[1 : len(s)-1]
   874  
   875  	// Check for unusual characters. If there are none,
   876  	// then no unquoting is needed, so return a slice of the
   877  	// original bytes.
   878  	r := 0
   879  	for r < len(s) {
   880  		c := s[r]
   881  		if c == '\\' || c == '"' || c < ' ' {
   882  			break
   883  		}
   884  		if c < utf8.RuneSelf {
   885  			r++
   886  			continue
   887  		}
   888  		rr, size := utf8.DecodeRune(s[r:])
   889  		if rr == utf8.RuneError && size == 1 {
   890  			break
   891  		}
   892  		r += size
   893  	}
   894  	if r == len(s) {
   895  		return s, true
   896  	}
   897  
   898  	b := make([]byte, len(s)+2*utf8.UTFMax)
   899  	w := copy(b, s[0:r])
   900  	for r < len(s) {
   901  		// Out of room?  Can only happen if s is full of
   902  		// malformed UTF-8 and we're replacing each
   903  		// byte with RuneError.
   904  		if w >= len(b)-2*utf8.UTFMax {
   905  			nb := make([]byte, (len(b)+utf8.UTFMax)*2)
   906  			copy(nb, b[0:w])
   907  			b = nb
   908  		}
   909  		switch c := s[r]; {
   910  		case c == '\\':
   911  			r++
   912  			if r >= len(s) {
   913  				return
   914  			}
   915  			switch s[r] {
   916  			default:
   917  				return
   918  			case '"', '\\', '/', '\'':
   919  				b[w] = s[r]
   920  				r++
   921  				w++
   922  			case 'b':
   923  				b[w] = '\b'
   924  				r++
   925  				w++
   926  			case 'f':
   927  				b[w] = '\f'
   928  				r++
   929  				w++
   930  			case 'n':
   931  				b[w] = '\n'
   932  				r++
   933  				w++
   934  			case 'r':
   935  				b[w] = '\r'
   936  				r++
   937  				w++
   938  			case 't':
   939  				b[w] = '\t'
   940  				r++
   941  				w++
   942  			case 'u':
   943  				r--
   944  				rr := getu4(s[r:])
   945  				if rr < 0 {
   946  					return
   947  				}
   948  				r += 6
   949  				if utf16.IsSurrogate(rr) {
   950  					rr1 := getu4(s[r:])
   951  					if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
   952  						// A valid pair; consume.
   953  						r += 6
   954  						w += utf8.EncodeRune(b[w:], dec)
   955  						break
   956  					}
   957  					// Invalid surrogate; fall back to replacement rune.
   958  					rr = unicode.ReplacementChar
   959  				}
   960  				w += utf8.EncodeRune(b[w:], rr)
   961  			}
   962  
   963  		// Quote, control characters are invalid.
   964  		case c == '"', c < ' ':
   965  			return
   966  
   967  		// ASCII
   968  		case c < utf8.RuneSelf:
   969  			b[w] = c
   970  			r++
   971  			w++
   972  
   973  		// Coerce to well-formed UTF-8.
   974  		default:
   975  			rr, size := utf8.DecodeRune(s[r:])
   976  			r += size
   977  			w += utf8.EncodeRune(b[w:], rr)
   978  		}
   979  	}
   980  	return b[0:w], true
   981  }
   982  
   983  // The following is issue 3069.
   984  
   985  // BUG(rsc): This package ignores anonymous (embedded) struct fields
   986  // during encoding and decoding.  A future version may assign meaning
   987  // to them.  To force an anonymous field to be ignored in all future
   988  // versions of this package, use an explicit `json:"-"` tag in the struct
   989  // definition.