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