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

View as plain text