...
Run Format

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

View as plain text