...

Source file src/pkg/encoding/json/decode.go

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

View as plain text