Black Lives Matter. Support the Equal Justice Initiative.

Source file src/encoding/json/decode.go

Documentation: encoding/json

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

View as plain text