github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/encoding/json/encode.go (about)

     1  // Copyright 2010 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package json implements encoding and decoding of JSON objects as defined in
     6  // RFC 4627. The mapping between JSON objects and Go values is described
     7  // in the documentation for the Marshal and Unmarshal functions.
     8  //
     9  // See "JSON and Go" for an introduction to this package:
    10  // https://golang.org/doc/articles/json_and_go.html
    11  package json
    12  
    13  import (
    14  	"bytes"
    15  	"encoding"
    16  	"encoding/base64"
    17  	"math"
    18  	"reflect"
    19  	"runtime"
    20  	"sort"
    21  	"strconv"
    22  	"strings"
    23  	"sync"
    24  	"unicode"
    25  	"unicode/utf8"
    26  )
    27  
    28  // Marshal returns the JSON encoding of v.
    29  //
    30  // Marshal traverses the value v recursively.
    31  // If an encountered value implements the Marshaler interface
    32  // and is not a nil pointer, Marshal calls its MarshalJSON method
    33  // to produce JSON. If no MarshalJSON method is present but the
    34  // value implements encoding.TextMarshaler instead, Marshal calls
    35  // its MarshalText method.
    36  // The nil pointer exception is not strictly necessary
    37  // but mimics a similar, necessary exception in the behavior of
    38  // UnmarshalJSON.
    39  //
    40  // Otherwise, Marshal uses the following type-dependent default encodings:
    41  //
    42  // Boolean values encode as JSON booleans.
    43  //
    44  // Floating point, integer, and Number values encode as JSON numbers.
    45  //
    46  // String values encode as JSON strings coerced to valid UTF-8,
    47  // replacing invalid bytes with the Unicode replacement rune.
    48  // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
    49  // to keep some browsers from misinterpreting JSON output as HTML.
    50  // Ampersand "&" is also escaped to "\u0026" for the same reason.
    51  //
    52  // Array and slice values encode as JSON arrays, except that
    53  // []byte encodes as a base64-encoded string, and a nil slice
    54  // encodes as the null JSON object.
    55  //
    56  // Struct values encode as JSON objects. Each exported struct field
    57  // becomes a member of the object unless
    58  //   - the field's tag is "-", or
    59  //   - the field is empty and its tag specifies the "omitempty" option.
    60  // The empty values are false, 0, any
    61  // nil pointer or interface value, and any array, slice, map, or string of
    62  // length zero. The object's default key string is the struct field name
    63  // but can be specified in the struct field's tag value. The "json" key in
    64  // the struct field's tag value is the key name, followed by an optional comma
    65  // and options. Examples:
    66  //
    67  //   // Field is ignored by this package.
    68  //   Field int `json:"-"`
    69  //
    70  //   // Field appears in JSON as key "myName".
    71  //   Field int `json:"myName"`
    72  //
    73  //   // Field appears in JSON as key "myName" and
    74  //   // the field is omitted from the object if its value is empty,
    75  //   // as defined above.
    76  //   Field int `json:"myName,omitempty"`
    77  //
    78  //   // Field appears in JSON as key "Field" (the default), but
    79  //   // the field is skipped if empty.
    80  //   // Note the leading comma.
    81  //   Field int `json:",omitempty"`
    82  //
    83  // The "string" option signals that a field is stored as JSON inside a
    84  // JSON-encoded string. It applies only to fields of string, floating point,
    85  // integer, or boolean types. This extra level of encoding is sometimes used
    86  // when communicating with JavaScript programs:
    87  //
    88  //    Int64String int64 `json:",string"`
    89  //
    90  // The key name will be used if it's a non-empty string consisting of
    91  // only Unicode letters, digits, dollar signs, percent signs, hyphens,
    92  // underscores and slashes.
    93  //
    94  // Anonymous struct fields are usually marshaled as if their inner exported fields
    95  // were fields in the outer struct, subject to the usual Go visibility rules amended
    96  // as described in the next paragraph.
    97  // An anonymous struct field with a name given in its JSON tag is treated as
    98  // having that name, rather than being anonymous.
    99  // An anonymous struct field of interface type is treated the same as having
   100  // that type as its name, rather than being anonymous.
   101  //
   102  // The Go visibility rules for struct fields are amended for JSON when
   103  // deciding which field to marshal or unmarshal. If there are
   104  // multiple fields at the same level, and that level is the least
   105  // nested (and would therefore be the nesting level selected by the
   106  // usual Go rules), the following extra rules apply:
   107  //
   108  // 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
   109  // even if there are multiple untagged fields that would otherwise conflict.
   110  // 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
   111  // 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
   112  //
   113  // Handling of anonymous struct fields is new in Go 1.1.
   114  // Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
   115  // an anonymous struct field in both current and earlier versions, give the field
   116  // a JSON tag of "-".
   117  //
   118  // Map values encode as JSON objects.
   119  // The map's key type must be string; the map keys are used as JSON object
   120  // keys, subject to the UTF-8 coercion described for string values above.
   121  //
   122  // Pointer values encode as the value pointed to.
   123  // A nil pointer encodes as the null JSON object.
   124  //
   125  // Interface values encode as the value contained in the interface.
   126  // A nil interface value encodes as the null JSON object.
   127  //
   128  // Channel, complex, and function values cannot be encoded in JSON.
   129  // Attempting to encode such a value causes Marshal to return
   130  // an UnsupportedTypeError.
   131  //
   132  // JSON cannot represent cyclic data structures and Marshal does not
   133  // handle them.  Passing cyclic structures to Marshal will result in
   134  // an infinite recursion.
   135  //
   136  func Marshal(v interface{}) ([]byte, error) {
   137  	e := &encodeState{}
   138  	err := e.marshal(v)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	return e.Bytes(), nil
   143  }
   144  
   145  // MarshalIndent is like Marshal but applies Indent to format the output.
   146  func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
   147  	b, err := Marshal(v)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	var buf bytes.Buffer
   152  	err = Indent(&buf, b, prefix, indent)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  	return buf.Bytes(), nil
   157  }
   158  
   159  // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
   160  // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
   161  // so that the JSON will be safe to embed inside HTML <script> tags.
   162  // For historical reasons, web browsers don't honor standard HTML
   163  // escaping within <script> tags, so an alternative JSON encoding must
   164  // be used.
   165  func HTMLEscape(dst *bytes.Buffer, src []byte) {
   166  	// The characters can only appear in string literals,
   167  	// so just scan the string one byte at a time.
   168  	start := 0
   169  	for i, c := range src {
   170  		if c == '<' || c == '>' || c == '&' {
   171  			if start < i {
   172  				dst.Write(src[start:i])
   173  			}
   174  			dst.WriteString(`\u00`)
   175  			dst.WriteByte(hex[c>>4])
   176  			dst.WriteByte(hex[c&0xF])
   177  			start = i + 1
   178  		}
   179  		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
   180  		if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
   181  			if start < i {
   182  				dst.Write(src[start:i])
   183  			}
   184  			dst.WriteString(`\u202`)
   185  			dst.WriteByte(hex[src[i+2]&0xF])
   186  			start = i + 3
   187  		}
   188  	}
   189  	if start < len(src) {
   190  		dst.Write(src[start:])
   191  	}
   192  }
   193  
   194  // Marshaler is the interface implemented by objects that
   195  // can marshal themselves into valid JSON.
   196  type Marshaler interface {
   197  	MarshalJSON() ([]byte, error)
   198  }
   199  
   200  // An UnsupportedTypeError is returned by Marshal when attempting
   201  // to encode an unsupported value type.
   202  type UnsupportedTypeError struct {
   203  	Type reflect.Type
   204  }
   205  
   206  func (e *UnsupportedTypeError) Error() string {
   207  	return "json: unsupported type: " + e.Type.String()
   208  }
   209  
   210  type UnsupportedValueError struct {
   211  	Value reflect.Value
   212  	Str   string
   213  }
   214  
   215  func (e *UnsupportedValueError) Error() string {
   216  	return "json: unsupported value: " + e.Str
   217  }
   218  
   219  // Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
   220  // attempting to encode a string value with invalid UTF-8 sequences.
   221  // As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
   222  // replacing invalid bytes with the Unicode replacement rune U+FFFD.
   223  // This error is no longer generated but is kept for backwards compatibility
   224  // with programs that might mention it.
   225  type InvalidUTF8Error struct {
   226  	S string // the whole string value that caused the error
   227  }
   228  
   229  func (e *InvalidUTF8Error) Error() string {
   230  	return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
   231  }
   232  
   233  type MarshalerError struct {
   234  	Type reflect.Type
   235  	Err  error
   236  }
   237  
   238  func (e *MarshalerError) Error() string {
   239  	return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error()
   240  }
   241  
   242  var hex = "0123456789abcdef"
   243  
   244  // An encodeState encodes JSON into a bytes.Buffer.
   245  type encodeState struct {
   246  	bytes.Buffer // accumulated output
   247  	scratch      [64]byte
   248  }
   249  
   250  var encodeStatePool sync.Pool
   251  
   252  func newEncodeState() *encodeState {
   253  	if v := encodeStatePool.Get(); v != nil {
   254  		e := v.(*encodeState)
   255  		e.Reset()
   256  		return e
   257  	}
   258  	return new(encodeState)
   259  }
   260  
   261  func (e *encodeState) marshal(v interface{}) (err error) {
   262  	defer func() {
   263  		if r := recover(); r != nil {
   264  			if _, ok := r.(runtime.Error); ok {
   265  				panic(r)
   266  			}
   267  			if s, ok := r.(string); ok {
   268  				panic(s)
   269  			}
   270  			err = r.(error)
   271  		}
   272  	}()
   273  	e.reflectValue(reflect.ValueOf(v))
   274  	return nil
   275  }
   276  
   277  func (e *encodeState) error(err error) {
   278  	panic(err)
   279  }
   280  
   281  func isEmptyValue(v reflect.Value) bool {
   282  	switch v.Kind() {
   283  	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
   284  		return v.Len() == 0
   285  	case reflect.Bool:
   286  		return !v.Bool()
   287  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   288  		return v.Int() == 0
   289  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   290  		return v.Uint() == 0
   291  	case reflect.Float32, reflect.Float64:
   292  		return v.Float() == 0
   293  	case reflect.Interface, reflect.Ptr:
   294  		return v.IsNil()
   295  	}
   296  	return false
   297  }
   298  
   299  func (e *encodeState) reflectValue(v reflect.Value) {
   300  	valueEncoder(v)(e, v, false)
   301  }
   302  
   303  type encoderFunc func(e *encodeState, v reflect.Value, quoted bool)
   304  
   305  var encoderCache struct {
   306  	sync.RWMutex
   307  	m map[reflect.Type]encoderFunc
   308  }
   309  
   310  func valueEncoder(v reflect.Value) encoderFunc {
   311  	if !v.IsValid() {
   312  		return invalidValueEncoder
   313  	}
   314  	return typeEncoder(v.Type())
   315  }
   316  
   317  func typeEncoder(t reflect.Type) encoderFunc {
   318  	encoderCache.RLock()
   319  	f := encoderCache.m[t]
   320  	encoderCache.RUnlock()
   321  	if f != nil {
   322  		return f
   323  	}
   324  
   325  	// To deal with recursive types, populate the map with an
   326  	// indirect func before we build it. This type waits on the
   327  	// real func (f) to be ready and then calls it.  This indirect
   328  	// func is only used for recursive types.
   329  	encoderCache.Lock()
   330  	if encoderCache.m == nil {
   331  		encoderCache.m = make(map[reflect.Type]encoderFunc)
   332  	}
   333  	var wg sync.WaitGroup
   334  	wg.Add(1)
   335  	encoderCache.m[t] = func(e *encodeState, v reflect.Value, quoted bool) {
   336  		wg.Wait()
   337  		f(e, v, quoted)
   338  	}
   339  	encoderCache.Unlock()
   340  
   341  	// Compute fields without lock.
   342  	// Might duplicate effort but won't hold other computations back.
   343  	f = newTypeEncoder(t, true)
   344  	wg.Done()
   345  	encoderCache.Lock()
   346  	encoderCache.m[t] = f
   347  	encoderCache.Unlock()
   348  	return f
   349  }
   350  
   351  var (
   352  	marshalerType     = reflect.TypeOf(new(Marshaler)).Elem()
   353  	textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
   354  )
   355  
   356  // newTypeEncoder constructs an encoderFunc for a type.
   357  // The returned encoder only checks CanAddr when allowAddr is true.
   358  func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
   359  	if t.Implements(marshalerType) {
   360  		return marshalerEncoder
   361  	}
   362  	if t.Kind() != reflect.Ptr && allowAddr {
   363  		if reflect.PtrTo(t).Implements(marshalerType) {
   364  			return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
   365  		}
   366  	}
   367  
   368  	if t.Implements(textMarshalerType) {
   369  		return textMarshalerEncoder
   370  	}
   371  	if t.Kind() != reflect.Ptr && allowAddr {
   372  		if reflect.PtrTo(t).Implements(textMarshalerType) {
   373  			return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
   374  		}
   375  	}
   376  
   377  	switch t.Kind() {
   378  	case reflect.Bool:
   379  		return boolEncoder
   380  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   381  		return intEncoder
   382  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   383  		return uintEncoder
   384  	case reflect.Float32:
   385  		return float32Encoder
   386  	case reflect.Float64:
   387  		return float64Encoder
   388  	case reflect.String:
   389  		return stringEncoder
   390  	case reflect.Interface:
   391  		return interfaceEncoder
   392  	case reflect.Struct:
   393  		return newStructEncoder(t)
   394  	case reflect.Map:
   395  		return newMapEncoder(t)
   396  	case reflect.Slice:
   397  		return newSliceEncoder(t)
   398  	case reflect.Array:
   399  		return newArrayEncoder(t)
   400  	case reflect.Ptr:
   401  		return newPtrEncoder(t)
   402  	default:
   403  		return unsupportedTypeEncoder
   404  	}
   405  }
   406  
   407  func invalidValueEncoder(e *encodeState, v reflect.Value, quoted bool) {
   408  	e.WriteString("null")
   409  }
   410  
   411  func marshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   412  	if v.Kind() == reflect.Ptr && v.IsNil() {
   413  		e.WriteString("null")
   414  		return
   415  	}
   416  	m := v.Interface().(Marshaler)
   417  	b, err := m.MarshalJSON()
   418  	if err == nil {
   419  		// copy JSON into buffer, checking validity.
   420  		err = compact(&e.Buffer, b, true)
   421  	}
   422  	if err != nil {
   423  		e.error(&MarshalerError{v.Type(), err})
   424  	}
   425  }
   426  
   427  func addrMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   428  	va := v.Addr()
   429  	if va.IsNil() {
   430  		e.WriteString("null")
   431  		return
   432  	}
   433  	m := va.Interface().(Marshaler)
   434  	b, err := m.MarshalJSON()
   435  	if err == nil {
   436  		// copy JSON into buffer, checking validity.
   437  		err = compact(&e.Buffer, b, true)
   438  	}
   439  	if err != nil {
   440  		e.error(&MarshalerError{v.Type(), err})
   441  	}
   442  }
   443  
   444  func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   445  	if v.Kind() == reflect.Ptr && v.IsNil() {
   446  		e.WriteString("null")
   447  		return
   448  	}
   449  	m := v.Interface().(encoding.TextMarshaler)
   450  	b, err := m.MarshalText()
   451  	if err == nil {
   452  		_, err = e.stringBytes(b)
   453  	}
   454  	if err != nil {
   455  		e.error(&MarshalerError{v.Type(), err})
   456  	}
   457  }
   458  
   459  func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   460  	va := v.Addr()
   461  	if va.IsNil() {
   462  		e.WriteString("null")
   463  		return
   464  	}
   465  	m := va.Interface().(encoding.TextMarshaler)
   466  	b, err := m.MarshalText()
   467  	if err == nil {
   468  		_, err = e.stringBytes(b)
   469  	}
   470  	if err != nil {
   471  		e.error(&MarshalerError{v.Type(), err})
   472  	}
   473  }
   474  
   475  func boolEncoder(e *encodeState, v reflect.Value, quoted bool) {
   476  	if quoted {
   477  		e.WriteByte('"')
   478  	}
   479  	if v.Bool() {
   480  		e.WriteString("true")
   481  	} else {
   482  		e.WriteString("false")
   483  	}
   484  	if quoted {
   485  		e.WriteByte('"')
   486  	}
   487  }
   488  
   489  func intEncoder(e *encodeState, v reflect.Value, quoted bool) {
   490  	b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
   491  	if quoted {
   492  		e.WriteByte('"')
   493  	}
   494  	e.Write(b)
   495  	if quoted {
   496  		e.WriteByte('"')
   497  	}
   498  }
   499  
   500  func uintEncoder(e *encodeState, v reflect.Value, quoted bool) {
   501  	b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
   502  	if quoted {
   503  		e.WriteByte('"')
   504  	}
   505  	e.Write(b)
   506  	if quoted {
   507  		e.WriteByte('"')
   508  	}
   509  }
   510  
   511  type floatEncoder int // number of bits
   512  
   513  func (bits floatEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   514  	f := v.Float()
   515  	if math.IsInf(f, 0) || math.IsNaN(f) {
   516  		e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
   517  	}
   518  	b := strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits))
   519  	if quoted {
   520  		e.WriteByte('"')
   521  	}
   522  	e.Write(b)
   523  	if quoted {
   524  		e.WriteByte('"')
   525  	}
   526  }
   527  
   528  var (
   529  	float32Encoder = (floatEncoder(32)).encode
   530  	float64Encoder = (floatEncoder(64)).encode
   531  )
   532  
   533  func stringEncoder(e *encodeState, v reflect.Value, quoted bool) {
   534  	if v.Type() == numberType {
   535  		numStr := v.String()
   536  		if numStr == "" {
   537  			numStr = "0" // Number's zero-val
   538  		}
   539  		e.WriteString(numStr)
   540  		return
   541  	}
   542  	if quoted {
   543  		sb, err := Marshal(v.String())
   544  		if err != nil {
   545  			e.error(err)
   546  		}
   547  		e.string(string(sb))
   548  	} else {
   549  		e.string(v.String())
   550  	}
   551  }
   552  
   553  func interfaceEncoder(e *encodeState, v reflect.Value, quoted bool) {
   554  	if v.IsNil() {
   555  		e.WriteString("null")
   556  		return
   557  	}
   558  	e.reflectValue(v.Elem())
   559  }
   560  
   561  func unsupportedTypeEncoder(e *encodeState, v reflect.Value, quoted bool) {
   562  	e.error(&UnsupportedTypeError{v.Type()})
   563  }
   564  
   565  type structEncoder struct {
   566  	fields    []field
   567  	fieldEncs []encoderFunc
   568  }
   569  
   570  func (se *structEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   571  	e.WriteByte('{')
   572  	first := true
   573  	for i, f := range se.fields {
   574  		fv := fieldByIndex(v, f.index)
   575  		if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) {
   576  			continue
   577  		}
   578  		if first {
   579  			first = false
   580  		} else {
   581  			e.WriteByte(',')
   582  		}
   583  		e.string(f.name)
   584  		e.WriteByte(':')
   585  		se.fieldEncs[i](e, fv, f.quoted)
   586  	}
   587  	e.WriteByte('}')
   588  }
   589  
   590  func newStructEncoder(t reflect.Type) encoderFunc {
   591  	fields := cachedTypeFields(t)
   592  	se := &structEncoder{
   593  		fields:    fields,
   594  		fieldEncs: make([]encoderFunc, len(fields)),
   595  	}
   596  	for i, f := range fields {
   597  		se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index))
   598  	}
   599  	return se.encode
   600  }
   601  
   602  type mapEncoder struct {
   603  	elemEnc encoderFunc
   604  }
   605  
   606  func (me *mapEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   607  	if v.IsNil() {
   608  		e.WriteString("null")
   609  		return
   610  	}
   611  	e.WriteByte('{')
   612  	var sv stringValues = v.MapKeys()
   613  	sort.Sort(sv)
   614  	for i, k := range sv {
   615  		if i > 0 {
   616  			e.WriteByte(',')
   617  		}
   618  		e.string(k.String())
   619  		e.WriteByte(':')
   620  		me.elemEnc(e, v.MapIndex(k), false)
   621  	}
   622  	e.WriteByte('}')
   623  }
   624  
   625  func newMapEncoder(t reflect.Type) encoderFunc {
   626  	if t.Key().Kind() != reflect.String {
   627  		return unsupportedTypeEncoder
   628  	}
   629  	me := &mapEncoder{typeEncoder(t.Elem())}
   630  	return me.encode
   631  }
   632  
   633  func encodeByteSlice(e *encodeState, v reflect.Value, _ bool) {
   634  	if v.IsNil() {
   635  		e.WriteString("null")
   636  		return
   637  	}
   638  	s := v.Bytes()
   639  	e.WriteByte('"')
   640  	if len(s) < 1024 {
   641  		// for small buffers, using Encode directly is much faster.
   642  		dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
   643  		base64.StdEncoding.Encode(dst, s)
   644  		e.Write(dst)
   645  	} else {
   646  		// for large buffers, avoid unnecessary extra temporary
   647  		// buffer space.
   648  		enc := base64.NewEncoder(base64.StdEncoding, e)
   649  		enc.Write(s)
   650  		enc.Close()
   651  	}
   652  	e.WriteByte('"')
   653  }
   654  
   655  // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
   656  type sliceEncoder struct {
   657  	arrayEnc encoderFunc
   658  }
   659  
   660  func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   661  	if v.IsNil() {
   662  		e.WriteString("null")
   663  		return
   664  	}
   665  	se.arrayEnc(e, v, false)
   666  }
   667  
   668  func newSliceEncoder(t reflect.Type) encoderFunc {
   669  	// Byte slices get special treatment; arrays don't.
   670  	if t.Elem().Kind() == reflect.Uint8 {
   671  		return encodeByteSlice
   672  	}
   673  	enc := &sliceEncoder{newArrayEncoder(t)}
   674  	return enc.encode
   675  }
   676  
   677  type arrayEncoder struct {
   678  	elemEnc encoderFunc
   679  }
   680  
   681  func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   682  	e.WriteByte('[')
   683  	n := v.Len()
   684  	for i := 0; i < n; i++ {
   685  		if i > 0 {
   686  			e.WriteByte(',')
   687  		}
   688  		ae.elemEnc(e, v.Index(i), false)
   689  	}
   690  	e.WriteByte(']')
   691  }
   692  
   693  func newArrayEncoder(t reflect.Type) encoderFunc {
   694  	enc := &arrayEncoder{typeEncoder(t.Elem())}
   695  	return enc.encode
   696  }
   697  
   698  type ptrEncoder struct {
   699  	elemEnc encoderFunc
   700  }
   701  
   702  func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   703  	if v.IsNil() {
   704  		e.WriteString("null")
   705  		return
   706  	}
   707  	pe.elemEnc(e, v.Elem(), quoted)
   708  }
   709  
   710  func newPtrEncoder(t reflect.Type) encoderFunc {
   711  	enc := &ptrEncoder{typeEncoder(t.Elem())}
   712  	return enc.encode
   713  }
   714  
   715  type condAddrEncoder struct {
   716  	canAddrEnc, elseEnc encoderFunc
   717  }
   718  
   719  func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   720  	if v.CanAddr() {
   721  		ce.canAddrEnc(e, v, quoted)
   722  	} else {
   723  		ce.elseEnc(e, v, quoted)
   724  	}
   725  }
   726  
   727  // newCondAddrEncoder returns an encoder that checks whether its value
   728  // CanAddr and delegates to canAddrEnc if so, else to elseEnc.
   729  func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
   730  	enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
   731  	return enc.encode
   732  }
   733  
   734  func isValidTag(s string) bool {
   735  	if s == "" {
   736  		return false
   737  	}
   738  	for _, c := range s {
   739  		switch {
   740  		case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
   741  			// Backslash and quote chars are reserved, but
   742  			// otherwise any punctuation chars are allowed
   743  			// in a tag name.
   744  		default:
   745  			if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
   746  				return false
   747  			}
   748  		}
   749  	}
   750  	return true
   751  }
   752  
   753  func fieldByIndex(v reflect.Value, index []int) reflect.Value {
   754  	for _, i := range index {
   755  		if v.Kind() == reflect.Ptr {
   756  			if v.IsNil() {
   757  				return reflect.Value{}
   758  			}
   759  			v = v.Elem()
   760  		}
   761  		v = v.Field(i)
   762  	}
   763  	return v
   764  }
   765  
   766  func typeByIndex(t reflect.Type, index []int) reflect.Type {
   767  	for _, i := range index {
   768  		if t.Kind() == reflect.Ptr {
   769  			t = t.Elem()
   770  		}
   771  		t = t.Field(i).Type
   772  	}
   773  	return t
   774  }
   775  
   776  // stringValues is a slice of reflect.Value holding *reflect.StringValue.
   777  // It implements the methods to sort by string.
   778  type stringValues []reflect.Value
   779  
   780  func (sv stringValues) Len() int           { return len(sv) }
   781  func (sv stringValues) Swap(i, j int)      { sv[i], sv[j] = sv[j], sv[i] }
   782  func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
   783  func (sv stringValues) get(i int) string   { return sv[i].String() }
   784  
   785  // NOTE: keep in sync with stringBytes below.
   786  func (e *encodeState) string(s string) (int, error) {
   787  	len0 := e.Len()
   788  	e.WriteByte('"')
   789  	start := 0
   790  	for i := 0; i < len(s); {
   791  		if b := s[i]; b < utf8.RuneSelf {
   792  			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
   793  				i++
   794  				continue
   795  			}
   796  			if start < i {
   797  				e.WriteString(s[start:i])
   798  			}
   799  			switch b {
   800  			case '\\', '"':
   801  				e.WriteByte('\\')
   802  				e.WriteByte(b)
   803  			case '\n':
   804  				e.WriteByte('\\')
   805  				e.WriteByte('n')
   806  			case '\r':
   807  				e.WriteByte('\\')
   808  				e.WriteByte('r')
   809  			case '\t':
   810  				e.WriteByte('\\')
   811  				e.WriteByte('t')
   812  			default:
   813  				// This encodes bytes < 0x20 except for \n and \r,
   814  				// as well as <, > and &. The latter are escaped because they
   815  				// can lead to security holes when user-controlled strings
   816  				// are rendered into JSON and served to some browsers.
   817  				e.WriteString(`\u00`)
   818  				e.WriteByte(hex[b>>4])
   819  				e.WriteByte(hex[b&0xF])
   820  			}
   821  			i++
   822  			start = i
   823  			continue
   824  		}
   825  		c, size := utf8.DecodeRuneInString(s[i:])
   826  		if c == utf8.RuneError && size == 1 {
   827  			if start < i {
   828  				e.WriteString(s[start:i])
   829  			}
   830  			e.WriteString(`\ufffd`)
   831  			i += size
   832  			start = i
   833  			continue
   834  		}
   835  		// U+2028 is LINE SEPARATOR.
   836  		// U+2029 is PARAGRAPH SEPARATOR.
   837  		// They are both technically valid characters in JSON strings,
   838  		// but don't work in JSONP, which has to be evaluated as JavaScript,
   839  		// and can lead to security holes there. It is valid JSON to
   840  		// escape them, so we do so unconditionally.
   841  		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
   842  		if c == '\u2028' || c == '\u2029' {
   843  			if start < i {
   844  				e.WriteString(s[start:i])
   845  			}
   846  			e.WriteString(`\u202`)
   847  			e.WriteByte(hex[c&0xF])
   848  			i += size
   849  			start = i
   850  			continue
   851  		}
   852  		i += size
   853  	}
   854  	if start < len(s) {
   855  		e.WriteString(s[start:])
   856  	}
   857  	e.WriteByte('"')
   858  	return e.Len() - len0, nil
   859  }
   860  
   861  // NOTE: keep in sync with string above.
   862  func (e *encodeState) stringBytes(s []byte) (int, error) {
   863  	len0 := e.Len()
   864  	e.WriteByte('"')
   865  	start := 0
   866  	for i := 0; i < len(s); {
   867  		if b := s[i]; b < utf8.RuneSelf {
   868  			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
   869  				i++
   870  				continue
   871  			}
   872  			if start < i {
   873  				e.Write(s[start:i])
   874  			}
   875  			switch b {
   876  			case '\\', '"':
   877  				e.WriteByte('\\')
   878  				e.WriteByte(b)
   879  			case '\n':
   880  				e.WriteByte('\\')
   881  				e.WriteByte('n')
   882  			case '\r':
   883  				e.WriteByte('\\')
   884  				e.WriteByte('r')
   885  			case '\t':
   886  				e.WriteByte('\\')
   887  				e.WriteByte('t')
   888  			default:
   889  				// This encodes bytes < 0x20 except for \n and \r,
   890  				// as well as <, >, and &. The latter are escaped because they
   891  				// can lead to security holes when user-controlled strings
   892  				// are rendered into JSON and served to some browsers.
   893  				e.WriteString(`\u00`)
   894  				e.WriteByte(hex[b>>4])
   895  				e.WriteByte(hex[b&0xF])
   896  			}
   897  			i++
   898  			start = i
   899  			continue
   900  		}
   901  		c, size := utf8.DecodeRune(s[i:])
   902  		if c == utf8.RuneError && size == 1 {
   903  			if start < i {
   904  				e.Write(s[start:i])
   905  			}
   906  			e.WriteString(`\ufffd`)
   907  			i += size
   908  			start = i
   909  			continue
   910  		}
   911  		// U+2028 is LINE SEPARATOR.
   912  		// U+2029 is PARAGRAPH SEPARATOR.
   913  		// They are both technically valid characters in JSON strings,
   914  		// but don't work in JSONP, which has to be evaluated as JavaScript,
   915  		// and can lead to security holes there. It is valid JSON to
   916  		// escape them, so we do so unconditionally.
   917  		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
   918  		if c == '\u2028' || c == '\u2029' {
   919  			if start < i {
   920  				e.Write(s[start:i])
   921  			}
   922  			e.WriteString(`\u202`)
   923  			e.WriteByte(hex[c&0xF])
   924  			i += size
   925  			start = i
   926  			continue
   927  		}
   928  		i += size
   929  	}
   930  	if start < len(s) {
   931  		e.Write(s[start:])
   932  	}
   933  	e.WriteByte('"')
   934  	return e.Len() - len0, nil
   935  }
   936  
   937  // A field represents a single field found in a struct.
   938  type field struct {
   939  	name      string
   940  	nameBytes []byte                 // []byte(name)
   941  	equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
   942  
   943  	tag       bool
   944  	index     []int
   945  	typ       reflect.Type
   946  	omitEmpty bool
   947  	quoted    bool
   948  }
   949  
   950  func fillField(f field) field {
   951  	f.nameBytes = []byte(f.name)
   952  	f.equalFold = foldFunc(f.nameBytes)
   953  	return f
   954  }
   955  
   956  // byName sorts field by name, breaking ties with depth,
   957  // then breaking ties with "name came from json tag", then
   958  // breaking ties with index sequence.
   959  type byName []field
   960  
   961  func (x byName) Len() int { return len(x) }
   962  
   963  func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
   964  
   965  func (x byName) Less(i, j int) bool {
   966  	if x[i].name != x[j].name {
   967  		return x[i].name < x[j].name
   968  	}
   969  	if len(x[i].index) != len(x[j].index) {
   970  		return len(x[i].index) < len(x[j].index)
   971  	}
   972  	if x[i].tag != x[j].tag {
   973  		return x[i].tag
   974  	}
   975  	return byIndex(x).Less(i, j)
   976  }
   977  
   978  // byIndex sorts field by index sequence.
   979  type byIndex []field
   980  
   981  func (x byIndex) Len() int { return len(x) }
   982  
   983  func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
   984  
   985  func (x byIndex) Less(i, j int) bool {
   986  	for k, xik := range x[i].index {
   987  		if k >= len(x[j].index) {
   988  			return false
   989  		}
   990  		if xik != x[j].index[k] {
   991  			return xik < x[j].index[k]
   992  		}
   993  	}
   994  	return len(x[i].index) < len(x[j].index)
   995  }
   996  
   997  // typeFields returns a list of fields that JSON should recognize for the given type.
   998  // The algorithm is breadth-first search over the set of structs to include - the top struct
   999  // and then any reachable anonymous structs.
  1000  func typeFields(t reflect.Type) []field {
  1001  	// Anonymous fields to explore at the current level and the next.
  1002  	current := []field{}
  1003  	next := []field{{typ: t}}
  1004  
  1005  	// Count of queued names for current level and the next.
  1006  	count := map[reflect.Type]int{}
  1007  	nextCount := map[reflect.Type]int{}
  1008  
  1009  	// Types already visited at an earlier level.
  1010  	visited := map[reflect.Type]bool{}
  1011  
  1012  	// Fields found.
  1013  	var fields []field
  1014  
  1015  	for len(next) > 0 {
  1016  		current, next = next, current[:0]
  1017  		count, nextCount = nextCount, map[reflect.Type]int{}
  1018  
  1019  		for _, f := range current {
  1020  			if visited[f.typ] {
  1021  				continue
  1022  			}
  1023  			visited[f.typ] = true
  1024  
  1025  			// Scan f.typ for fields to include.
  1026  			for i := 0; i < f.typ.NumField(); i++ {
  1027  				sf := f.typ.Field(i)
  1028  				if sf.PkgPath != "" { // unexported
  1029  					continue
  1030  				}
  1031  				tag := sf.Tag.Get("json")
  1032  				if tag == "-" {
  1033  					continue
  1034  				}
  1035  				name, opts := parseTag(tag)
  1036  				if !isValidTag(name) {
  1037  					name = ""
  1038  				}
  1039  				index := make([]int, len(f.index)+1)
  1040  				copy(index, f.index)
  1041  				index[len(f.index)] = i
  1042  
  1043  				ft := sf.Type
  1044  				if ft.Name() == "" && ft.Kind() == reflect.Ptr {
  1045  					// Follow pointer.
  1046  					ft = ft.Elem()
  1047  				}
  1048  
  1049  				// Only strings, floats, integers, and booleans can be quoted.
  1050  				quoted := false
  1051  				if opts.Contains("string") {
  1052  					switch ft.Kind() {
  1053  					case reflect.Bool,
  1054  						reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  1055  						reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  1056  						reflect.Float32, reflect.Float64,
  1057  						reflect.String:
  1058  						quoted = true
  1059  					}
  1060  				}
  1061  
  1062  				// Record found field and index sequence.
  1063  				if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
  1064  					tagged := name != ""
  1065  					if name == "" {
  1066  						name = sf.Name
  1067  					}
  1068  					fields = append(fields, fillField(field{
  1069  						name:      name,
  1070  						tag:       tagged,
  1071  						index:     index,
  1072  						typ:       ft,
  1073  						omitEmpty: opts.Contains("omitempty"),
  1074  						quoted:    quoted,
  1075  					}))
  1076  					if count[f.typ] > 1 {
  1077  						// If there were multiple instances, add a second,
  1078  						// so that the annihilation code will see a duplicate.
  1079  						// It only cares about the distinction between 1 or 2,
  1080  						// so don't bother generating any more copies.
  1081  						fields = append(fields, fields[len(fields)-1])
  1082  					}
  1083  					continue
  1084  				}
  1085  
  1086  				// Record new anonymous struct to explore in next round.
  1087  				nextCount[ft]++
  1088  				if nextCount[ft] == 1 {
  1089  					next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
  1090  				}
  1091  			}
  1092  		}
  1093  	}
  1094  
  1095  	sort.Sort(byName(fields))
  1096  
  1097  	// Delete all fields that are hidden by the Go rules for embedded fields,
  1098  	// except that fields with JSON tags are promoted.
  1099  
  1100  	// The fields are sorted in primary order of name, secondary order
  1101  	// of field index length. Loop over names; for each name, delete
  1102  	// hidden fields by choosing the one dominant field that survives.
  1103  	out := fields[:0]
  1104  	for advance, i := 0, 0; i < len(fields); i += advance {
  1105  		// One iteration per name.
  1106  		// Find the sequence of fields with the name of this first field.
  1107  		fi := fields[i]
  1108  		name := fi.name
  1109  		for advance = 1; i+advance < len(fields); advance++ {
  1110  			fj := fields[i+advance]
  1111  			if fj.name != name {
  1112  				break
  1113  			}
  1114  		}
  1115  		if advance == 1 { // Only one field with this name
  1116  			out = append(out, fi)
  1117  			continue
  1118  		}
  1119  		dominant, ok := dominantField(fields[i : i+advance])
  1120  		if ok {
  1121  			out = append(out, dominant)
  1122  		}
  1123  	}
  1124  
  1125  	fields = out
  1126  	sort.Sort(byIndex(fields))
  1127  
  1128  	return fields
  1129  }
  1130  
  1131  // dominantField looks through the fields, all of which are known to
  1132  // have the same name, to find the single field that dominates the
  1133  // others using Go's embedding rules, modified by the presence of
  1134  // JSON tags. If there are multiple top-level fields, the boolean
  1135  // will be false: This condition is an error in Go and we skip all
  1136  // the fields.
  1137  func dominantField(fields []field) (field, bool) {
  1138  	// The fields are sorted in increasing index-length order. The winner
  1139  	// must therefore be one with the shortest index length. Drop all
  1140  	// longer entries, which is easy: just truncate the slice.
  1141  	length := len(fields[0].index)
  1142  	tagged := -1 // Index of first tagged field.
  1143  	for i, f := range fields {
  1144  		if len(f.index) > length {
  1145  			fields = fields[:i]
  1146  			break
  1147  		}
  1148  		if f.tag {
  1149  			if tagged >= 0 {
  1150  				// Multiple tagged fields at the same level: conflict.
  1151  				// Return no field.
  1152  				return field{}, false
  1153  			}
  1154  			tagged = i
  1155  		}
  1156  	}
  1157  	if tagged >= 0 {
  1158  		return fields[tagged], true
  1159  	}
  1160  	// All remaining fields have the same length. If there's more than one,
  1161  	// we have a conflict (two fields named "X" at the same level) and we
  1162  	// return no field.
  1163  	if len(fields) > 1 {
  1164  		return field{}, false
  1165  	}
  1166  	return fields[0], true
  1167  }
  1168  
  1169  var fieldCache struct {
  1170  	sync.RWMutex
  1171  	m map[reflect.Type][]field
  1172  }
  1173  
  1174  // cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
  1175  func cachedTypeFields(t reflect.Type) []field {
  1176  	fieldCache.RLock()
  1177  	f := fieldCache.m[t]
  1178  	fieldCache.RUnlock()
  1179  	if f != nil {
  1180  		return f
  1181  	}
  1182  
  1183  	// Compute fields without lock.
  1184  	// Might duplicate effort but won't hold other computations back.
  1185  	f = typeFields(t)
  1186  	if f == nil {
  1187  		f = []field{}
  1188  	}
  1189  
  1190  	fieldCache.Lock()
  1191  	if fieldCache.m == nil {
  1192  		fieldCache.m = map[reflect.Type][]field{}
  1193  	}
  1194  	fieldCache.m[t] = f
  1195  	fieldCache.Unlock()
  1196  	return f
  1197  }