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