...

Source file src/github.com/evanphx/json-patch/v5/internal/json/encode.go

Documentation: github.com/evanphx/json-patch/v5/internal/json

     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 7159. 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  	"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 and encodes the result as a JSON string.
    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  // So that the JSON will be safe to embed inside HTML <script> tags,
    49  // the string is encoded using HTMLEscape,
    50  // which replaces "<", ">", "&", U+2028, and U+2029 are escaped
    51  // to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029".
    52  // This replacement can be disabled when using an Encoder,
    53  // by calling SetEscapeHTML(false).
    54  //
    55  // Array and slice values encode as JSON arrays, except that
    56  // []byte encodes as a base64-encoded string, and a nil slice
    57  // encodes as the null JSON value.
    58  //
    59  // Struct values encode as JSON objects.
    60  // Each exported struct field becomes a member of the object, using the
    61  // field name as the object key, unless the field is omitted for one of the
    62  // reasons given below.
    63  //
    64  // The encoding of each struct field can be customized by the format string
    65  // stored under the "json" key in the struct field's tag.
    66  // The format string gives the name of the field, possibly followed by a
    67  // comma-separated list of options. The name may be empty in order to
    68  // specify options without overriding the default field name.
    69  //
    70  // The "omitempty" option specifies that the field should be omitted
    71  // from the encoding if the field has an empty value, defined as
    72  // false, 0, a nil pointer, a nil interface value, and any empty array,
    73  // slice, map, or string.
    74  //
    75  // As a special case, if the field tag is "-", the field is always omitted.
    76  // Note that a field with name "-" can still be generated using the tag "-,".
    77  //
    78  // Examples of struct field tags and their meanings:
    79  //
    80  //	// Field appears in JSON as key "myName".
    81  //	Field int `json:"myName"`
    82  //
    83  //	// Field appears in JSON as key "myName" and
    84  //	// the field is omitted from the object if its value is empty,
    85  //	// as defined above.
    86  //	Field int `json:"myName,omitempty"`
    87  //
    88  //	// Field appears in JSON as key "Field" (the default), but
    89  //	// the field is skipped if empty.
    90  //	// Note the leading comma.
    91  //	Field int `json:",omitempty"`
    92  //
    93  //	// Field is ignored by this package.
    94  //	Field int `json:"-"`
    95  //
    96  //	// Field appears in JSON as key "-".
    97  //	Field int `json:"-,"`
    98  //
    99  // The "string" option signals that a field is stored as JSON inside a
   100  // JSON-encoded string. It applies only to fields of string, floating point,
   101  // integer, or boolean types. This extra level of encoding is sometimes used
   102  // when communicating with JavaScript programs:
   103  //
   104  //	Int64String int64 `json:",string"`
   105  //
   106  // The key name will be used if it's a non-empty string consisting of
   107  // only Unicode letters, digits, and ASCII punctuation except quotation
   108  // marks, backslash, and comma.
   109  //
   110  // Anonymous struct fields are usually marshaled as if their inner exported fields
   111  // were fields in the outer struct, subject to the usual Go visibility rules amended
   112  // as described in the next paragraph.
   113  // An anonymous struct field with a name given in its JSON tag is treated as
   114  // having that name, rather than being anonymous.
   115  // An anonymous struct field of interface type is treated the same as having
   116  // that type as its name, rather than being anonymous.
   117  //
   118  // The Go visibility rules for struct fields are amended for JSON when
   119  // deciding which field to marshal or unmarshal. If there are
   120  // multiple fields at the same level, and that level is the least
   121  // nested (and would therefore be the nesting level selected by the
   122  // usual Go rules), the following extra rules apply:
   123  //
   124  // 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
   125  // even if there are multiple untagged fields that would otherwise conflict.
   126  //
   127  // 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
   128  //
   129  // 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
   130  //
   131  // Handling of anonymous struct fields is new in Go 1.1.
   132  // Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
   133  // an anonymous struct field in both current and earlier versions, give the field
   134  // a JSON tag of "-".
   135  //
   136  // Map values encode as JSON objects. The map's key type must either be a
   137  // string, an integer type, or implement encoding.TextMarshaler. The map keys
   138  // are sorted and used as JSON object keys by applying the following rules,
   139  // subject to the UTF-8 coercion described for string values above:
   140  //   - keys of any string type are used directly
   141  //   - encoding.TextMarshalers are marshaled
   142  //   - integer keys are converted to strings
   143  //
   144  // Pointer values encode as the value pointed to.
   145  // A nil pointer encodes as the null JSON value.
   146  //
   147  // Interface values encode as the value contained in the interface.
   148  // A nil interface value encodes as the null JSON value.
   149  //
   150  // Channel, complex, and function values cannot be encoded in JSON.
   151  // Attempting to encode such a value causes Marshal to return
   152  // an UnsupportedTypeError.
   153  //
   154  // JSON cannot represent cyclic data structures and Marshal does not
   155  // handle them. Passing cyclic structures to Marshal will result in
   156  // an error.
   157  func Marshal(v any) ([]byte, error) {
   158  	e := newEncodeState()
   159  	defer encodeStatePool.Put(e)
   160  
   161  	err := e.marshal(v, encOpts{escapeHTML: true})
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  	buf := append([]byte(nil), e.Bytes()...)
   166  
   167  	return buf, nil
   168  }
   169  
   170  func MarshalEscaped(v any, escape bool) ([]byte, error) {
   171  	e := newEncodeState()
   172  	defer encodeStatePool.Put(e)
   173  
   174  	err := e.marshal(v, encOpts{escapeHTML: escape})
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  	buf := append([]byte(nil), e.Bytes()...)
   179  
   180  	return buf, nil
   181  }
   182  
   183  // MarshalIndent is like Marshal but applies Indent to format the output.
   184  // Each JSON element in the output will begin on a new line beginning with prefix
   185  // followed by one or more copies of indent according to the indentation nesting.
   186  func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
   187  	b, err := Marshal(v)
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  	var buf bytes.Buffer
   192  	err = Indent(&buf, b, prefix, indent)
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  	return buf.Bytes(), nil
   197  }
   198  
   199  // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
   200  // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
   201  // so that the JSON will be safe to embed inside HTML <script> tags.
   202  // For historical reasons, web browsers don't honor standard HTML
   203  // escaping within <script> tags, so an alternative JSON encoding must
   204  // be used.
   205  func HTMLEscape(dst *bytes.Buffer, src []byte) {
   206  	// The characters can only appear in string literals,
   207  	// so just scan the string one byte at a time.
   208  	start := 0
   209  	for i, c := range src {
   210  		if c == '<' || c == '>' || c == '&' {
   211  			if start < i {
   212  				dst.Write(src[start:i])
   213  			}
   214  			dst.WriteString(`\u00`)
   215  			dst.WriteByte(hex[c>>4])
   216  			dst.WriteByte(hex[c&0xF])
   217  			start = i + 1
   218  		}
   219  		// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
   220  		if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
   221  			if start < i {
   222  				dst.Write(src[start:i])
   223  			}
   224  			dst.WriteString(`\u202`)
   225  			dst.WriteByte(hex[src[i+2]&0xF])
   226  			start = i + 3
   227  		}
   228  	}
   229  	if start < len(src) {
   230  		dst.Write(src[start:])
   231  	}
   232  }
   233  
   234  // Marshaler is the interface implemented by types that
   235  // can marshal themselves into valid JSON.
   236  type Marshaler interface {
   237  	MarshalJSON() ([]byte, error)
   238  }
   239  
   240  type RedirectMarshaler interface {
   241  	RedirectMarshalJSON() (any, error)
   242  }
   243  
   244  type TrustMarshaler interface {
   245  	TrustMarshalJSON(b *bytes.Buffer) error
   246  }
   247  
   248  // An UnsupportedTypeError is returned by Marshal when attempting
   249  // to encode an unsupported value type.
   250  type UnsupportedTypeError struct {
   251  	Type reflect.Type
   252  }
   253  
   254  func (e *UnsupportedTypeError) Error() string {
   255  	return "json: unsupported type: " + e.Type.String()
   256  }
   257  
   258  // An UnsupportedValueError is returned by Marshal when attempting
   259  // to encode an unsupported value.
   260  type UnsupportedValueError struct {
   261  	Value reflect.Value
   262  	Str   string
   263  }
   264  
   265  func (e *UnsupportedValueError) Error() string {
   266  	return "json: unsupported value: " + e.Str
   267  }
   268  
   269  // Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
   270  // attempting to encode a string value with invalid UTF-8 sequences.
   271  // As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
   272  // replacing invalid bytes with the Unicode replacement rune U+FFFD.
   273  //
   274  // Deprecated: No longer used; kept for compatibility.
   275  type InvalidUTF8Error struct {
   276  	S string // the whole string value that caused the error
   277  }
   278  
   279  func (e *InvalidUTF8Error) Error() string {
   280  	return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
   281  }
   282  
   283  // A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
   284  type MarshalerError struct {
   285  	Type       reflect.Type
   286  	Err        error
   287  	sourceFunc string
   288  }
   289  
   290  func (e *MarshalerError) Error() string {
   291  	srcFunc := e.sourceFunc
   292  	if srcFunc == "" {
   293  		srcFunc = "MarshalJSON"
   294  	}
   295  	return "json: error calling " + srcFunc +
   296  		" for type " + e.Type.String() +
   297  		": " + e.Err.Error()
   298  }
   299  
   300  // Unwrap returns the underlying error.
   301  func (e *MarshalerError) Unwrap() error { return e.Err }
   302  
   303  var hex = "0123456789abcdef"
   304  
   305  // An encodeState encodes JSON into a bytes.Buffer.
   306  type encodeState struct {
   307  	bytes.Buffer // accumulated output
   308  	scratch      [64]byte
   309  
   310  	// Keep track of what pointers we've seen in the current recursive call
   311  	// path, to avoid cycles that could lead to a stack overflow. Only do
   312  	// the relatively expensive map operations if ptrLevel is larger than
   313  	// startDetectingCyclesAfter, so that we skip the work if we're within a
   314  	// reasonable amount of nested pointers deep.
   315  	ptrLevel uint
   316  	ptrSeen  map[any]struct{}
   317  }
   318  
   319  const startDetectingCyclesAfter = 1000
   320  
   321  var encodeStatePool sync.Pool
   322  
   323  func newEncodeState() *encodeState {
   324  	if v := encodeStatePool.Get(); v != nil {
   325  		e := v.(*encodeState)
   326  		e.Reset()
   327  		if len(e.ptrSeen) > 0 {
   328  			panic("ptrEncoder.encode should have emptied ptrSeen via defers")
   329  		}
   330  		e.ptrLevel = 0
   331  		return e
   332  	}
   333  	return &encodeState{ptrSeen: make(map[any]struct{})}
   334  }
   335  
   336  // jsonError is an error wrapper type for internal use only.
   337  // Panics with errors are wrapped in jsonError so that the top-level recover
   338  // can distinguish intentional panics from this package.
   339  type jsonError struct{ error }
   340  
   341  func (e *encodeState) marshal(v any, opts encOpts) (err error) {
   342  	defer func() {
   343  		if r := recover(); r != nil {
   344  			if je, ok := r.(jsonError); ok {
   345  				err = je.error
   346  			} else {
   347  				panic(r)
   348  			}
   349  		}
   350  	}()
   351  	e.reflectValue(reflect.ValueOf(v), opts)
   352  	return nil
   353  }
   354  
   355  // error aborts the encoding by panicking with err wrapped in jsonError.
   356  func (e *encodeState) error(err error) {
   357  	panic(jsonError{err})
   358  }
   359  
   360  func isEmptyValue(v reflect.Value) bool {
   361  	switch v.Kind() {
   362  	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
   363  		return v.Len() == 0
   364  	case reflect.Bool:
   365  		return !v.Bool()
   366  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   367  		return v.Int() == 0
   368  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   369  		return v.Uint() == 0
   370  	case reflect.Float32, reflect.Float64:
   371  		return v.Float() == 0
   372  	case reflect.Interface, reflect.Pointer:
   373  		return v.IsNil()
   374  	}
   375  	return false
   376  }
   377  
   378  func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) {
   379  	valueEncoder(v)(e, v, opts)
   380  }
   381  
   382  type encOpts struct {
   383  	// quoted causes primitive fields to be encoded inside JSON strings.
   384  	quoted bool
   385  	// escapeHTML causes '<', '>', and '&' to be escaped in JSON strings.
   386  	escapeHTML bool
   387  }
   388  
   389  type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
   390  
   391  var encoderCache sync.Map // map[reflect.Type]encoderFunc
   392  
   393  func valueEncoder(v reflect.Value) encoderFunc {
   394  	if !v.IsValid() {
   395  		return invalidValueEncoder
   396  	}
   397  	return typeEncoder(v.Type())
   398  }
   399  
   400  func typeEncoder(t reflect.Type) encoderFunc {
   401  	if fi, ok := encoderCache.Load(t); ok {
   402  		return fi.(encoderFunc)
   403  	}
   404  
   405  	// To deal with recursive types, populate the map with an
   406  	// indirect func before we build it. This type waits on the
   407  	// real func (f) to be ready and then calls it. This indirect
   408  	// func is only used for recursive types.
   409  	var (
   410  		wg sync.WaitGroup
   411  		f  encoderFunc
   412  	)
   413  	wg.Add(1)
   414  	fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) {
   415  		wg.Wait()
   416  		f(e, v, opts)
   417  	}))
   418  	if loaded {
   419  		return fi.(encoderFunc)
   420  	}
   421  
   422  	// Compute the real encoder and replace the indirect func with it.
   423  	f = newTypeEncoder(t, true)
   424  	wg.Done()
   425  	encoderCache.Store(t, f)
   426  	return f
   427  }
   428  
   429  var (
   430  	marshalerType      = reflect.TypeOf((*Marshaler)(nil)).Elem()
   431  	redirMarshalerType = reflect.TypeOf((*RedirectMarshaler)(nil)).Elem()
   432  	trustMarshalerType = reflect.TypeOf((*TrustMarshaler)(nil)).Elem()
   433  	textMarshalerType  = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
   434  )
   435  
   436  // newTypeEncoder constructs an encoderFunc for a type.
   437  // The returned encoder only checks CanAddr when allowAddr is true.
   438  func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
   439  	if t.Implements(redirMarshalerType) {
   440  		return redirMarshalerEncoder
   441  	}
   442  	if t.Implements(trustMarshalerType) {
   443  		return marshalerTrustEncoder
   444  	}
   445  	// If we have a non-pointer value whose type implements
   446  	// Marshaler with a value receiver, then we're better off taking
   447  	// the address of the value - otherwise we end up with an
   448  	// allocation as we cast the value to an interface.
   449  	if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(marshalerType) {
   450  		return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
   451  	}
   452  	if t.Implements(marshalerType) {
   453  		return marshalerEncoder
   454  	}
   455  	if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(textMarshalerType) {
   456  		return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
   457  	}
   458  	if t.Implements(textMarshalerType) {
   459  		return textMarshalerEncoder
   460  	}
   461  
   462  	switch t.Kind() {
   463  	case reflect.Bool:
   464  		return boolEncoder
   465  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   466  		return intEncoder
   467  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   468  		return uintEncoder
   469  	case reflect.Float32:
   470  		return float32Encoder
   471  	case reflect.Float64:
   472  		return float64Encoder
   473  	case reflect.String:
   474  		return stringEncoder
   475  	case reflect.Interface:
   476  		return interfaceEncoder
   477  	case reflect.Struct:
   478  		return newStructEncoder(t)
   479  	case reflect.Map:
   480  		return newMapEncoder(t)
   481  	case reflect.Slice:
   482  		return newSliceEncoder(t)
   483  	case reflect.Array:
   484  		return newArrayEncoder(t)
   485  	case reflect.Pointer:
   486  		return newPtrEncoder(t)
   487  	default:
   488  		return unsupportedTypeEncoder
   489  	}
   490  }
   491  
   492  func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) {
   493  	e.WriteString("null")
   494  }
   495  
   496  func redirMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   497  	if v.Kind() == reflect.Pointer && v.IsNil() {
   498  		e.WriteString("null")
   499  		return
   500  	}
   501  	m, ok := v.Interface().(RedirectMarshaler)
   502  	if !ok {
   503  		e.WriteString("null")
   504  		return
   505  	}
   506  
   507  	iv, err := m.RedirectMarshalJSON()
   508  	if err != nil {
   509  		e.error(&MarshalerError{v.Type(), err, "RedirectMarshalJSON"})
   510  		return
   511  	}
   512  
   513  	e.marshal(iv, opts)
   514  }
   515  
   516  func marshalerTrustEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   517  	if v.Kind() == reflect.Pointer && v.IsNil() {
   518  		e.WriteString("null")
   519  		return
   520  	}
   521  	m, ok := v.Interface().(TrustMarshaler)
   522  	if !ok {
   523  		e.WriteString("null")
   524  		return
   525  	}
   526  	err := m.TrustMarshalJSON(&e.Buffer)
   527  	if err == nil {
   528  		//_, err = e.Buffer.Write(b)
   529  		// copy JSON into buffer, checking validity.
   530  		//err = compact(&e.Buffer, b, opts.escapeHTML)
   531  	}
   532  	if err != nil {
   533  		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
   534  	}
   535  }
   536  func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   537  	if v.Kind() == reflect.Pointer && v.IsNil() {
   538  		e.WriteString("null")
   539  		return
   540  	}
   541  	m, ok := v.Interface().(Marshaler)
   542  	if !ok {
   543  		e.WriteString("null")
   544  		return
   545  	}
   546  	b, err := m.MarshalJSON()
   547  	if err == nil {
   548  		// copy JSON into buffer, checking validity.
   549  		err = compact(&e.Buffer, b, opts.escapeHTML)
   550  	}
   551  	if err != nil {
   552  		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
   553  	}
   554  }
   555  
   556  func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   557  	va := v.Addr()
   558  	if va.IsNil() {
   559  		e.WriteString("null")
   560  		return
   561  	}
   562  	m := va.Interface().(Marshaler)
   563  	b, err := m.MarshalJSON()
   564  	if err == nil {
   565  		// copy JSON into buffer, checking validity.
   566  		err = compact(&e.Buffer, b, opts.escapeHTML)
   567  	}
   568  	if err != nil {
   569  		e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
   570  	}
   571  }
   572  
   573  func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   574  	if v.Kind() == reflect.Pointer && v.IsNil() {
   575  		e.WriteString("null")
   576  		return
   577  	}
   578  	m, ok := v.Interface().(encoding.TextMarshaler)
   579  	if !ok {
   580  		e.WriteString("null")
   581  		return
   582  	}
   583  	b, err := m.MarshalText()
   584  	if err != nil {
   585  		e.error(&MarshalerError{v.Type(), err, "MarshalText"})
   586  	}
   587  	e.stringBytes(b, opts.escapeHTML)
   588  }
   589  
   590  func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   591  	va := v.Addr()
   592  	if va.IsNil() {
   593  		e.WriteString("null")
   594  		return
   595  	}
   596  	m := va.Interface().(encoding.TextMarshaler)
   597  	b, err := m.MarshalText()
   598  	if err != nil {
   599  		e.error(&MarshalerError{v.Type(), err, "MarshalText"})
   600  	}
   601  	e.stringBytes(b, opts.escapeHTML)
   602  }
   603  
   604  func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   605  	if opts.quoted {
   606  		e.WriteByte('"')
   607  	}
   608  	if v.Bool() {
   609  		e.WriteString("true")
   610  	} else {
   611  		e.WriteString("false")
   612  	}
   613  	if opts.quoted {
   614  		e.WriteByte('"')
   615  	}
   616  }
   617  
   618  func intEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   619  	b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
   620  	if opts.quoted {
   621  		e.WriteByte('"')
   622  	}
   623  	e.Write(b)
   624  	if opts.quoted {
   625  		e.WriteByte('"')
   626  	}
   627  }
   628  
   629  func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   630  	b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
   631  	if opts.quoted {
   632  		e.WriteByte('"')
   633  	}
   634  	e.Write(b)
   635  	if opts.quoted {
   636  		e.WriteByte('"')
   637  	}
   638  }
   639  
   640  type floatEncoder int // number of bits
   641  
   642  func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
   643  	f := v.Float()
   644  	if math.IsInf(f, 0) || math.IsNaN(f) {
   645  		e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
   646  	}
   647  
   648  	// Convert as if by ES6 number to string conversion.
   649  	// This matches most other JSON generators.
   650  	// See golang.org/issue/6384 and golang.org/issue/14135.
   651  	// Like fmt %g, but the exponent cutoffs are different
   652  	// and exponents themselves are not padded to two digits.
   653  	b := e.scratch[:0]
   654  	abs := math.Abs(f)
   655  	fmt := byte('f')
   656  	// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
   657  	if abs != 0 {
   658  		if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
   659  			fmt = 'e'
   660  		}
   661  	}
   662  	b = strconv.AppendFloat(b, f, fmt, -1, int(bits))
   663  	if fmt == 'e' {
   664  		// clean up e-09 to e-9
   665  		n := len(b)
   666  		if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' {
   667  			b[n-2] = b[n-1]
   668  			b = b[:n-1]
   669  		}
   670  	}
   671  
   672  	if opts.quoted {
   673  		e.WriteByte('"')
   674  	}
   675  	e.Write(b)
   676  	if opts.quoted {
   677  		e.WriteByte('"')
   678  	}
   679  }
   680  
   681  var (
   682  	float32Encoder = (floatEncoder(32)).encode
   683  	float64Encoder = (floatEncoder(64)).encode
   684  )
   685  
   686  func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   687  	if v.Type() == numberType {
   688  		numStr := v.String()
   689  		// In Go1.5 the empty string encodes to "0", while this is not a valid number literal
   690  		// we keep compatibility so check validity after this.
   691  		if numStr == "" {
   692  			numStr = "0" // Number's zero-val
   693  		}
   694  		if !isValidNumber(numStr) {
   695  			e.error(fmt.Errorf("json: invalid number literal %q", numStr))
   696  		}
   697  		if opts.quoted {
   698  			e.WriteByte('"')
   699  		}
   700  		e.WriteString(numStr)
   701  		if opts.quoted {
   702  			e.WriteByte('"')
   703  		}
   704  		return
   705  	}
   706  	if opts.quoted {
   707  		e2 := newEncodeState()
   708  		// Since we encode the string twice, we only need to escape HTML
   709  		// the first time.
   710  		e2.string(v.String(), opts.escapeHTML)
   711  		e.stringBytes(e2.Bytes(), false)
   712  		encodeStatePool.Put(e2)
   713  	} else {
   714  		e.string(v.String(), opts.escapeHTML)
   715  	}
   716  }
   717  
   718  // isValidNumber reports whether s is a valid JSON number literal.
   719  func isValidNumber(s string) bool {
   720  	// This function implements the JSON numbers grammar.
   721  	// See https://tools.ietf.org/html/rfc7159#section-6
   722  	// and https://www.json.org/img/number.png
   723  
   724  	if s == "" {
   725  		return false
   726  	}
   727  
   728  	// Optional -
   729  	if s[0] == '-' {
   730  		s = s[1:]
   731  		if s == "" {
   732  			return false
   733  		}
   734  	}
   735  
   736  	// Digits
   737  	switch {
   738  	default:
   739  		return false
   740  
   741  	case s[0] == '0':
   742  		s = s[1:]
   743  
   744  	case '1' <= s[0] && s[0] <= '9':
   745  		s = s[1:]
   746  		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
   747  			s = s[1:]
   748  		}
   749  	}
   750  
   751  	// . followed by 1 or more digits.
   752  	if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
   753  		s = s[2:]
   754  		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
   755  			s = s[1:]
   756  		}
   757  	}
   758  
   759  	// e or E followed by an optional - or + and
   760  	// 1 or more digits.
   761  	if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
   762  		s = s[1:]
   763  		if s[0] == '+' || s[0] == '-' {
   764  			s = s[1:]
   765  			if s == "" {
   766  				return false
   767  			}
   768  		}
   769  		for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
   770  			s = s[1:]
   771  		}
   772  	}
   773  
   774  	// Make sure we are at the end.
   775  	return s == ""
   776  }
   777  
   778  func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) {
   779  	if v.IsNil() {
   780  		e.WriteString("null")
   781  		return
   782  	}
   783  	e.reflectValue(v.Elem(), opts)
   784  }
   785  
   786  func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) {
   787  	e.error(&UnsupportedTypeError{v.Type()})
   788  }
   789  
   790  type structEncoder struct {
   791  	fields structFields
   792  }
   793  
   794  type structFields struct {
   795  	list      []field
   796  	nameIndex map[string]int
   797  }
   798  
   799  func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
   800  	next := byte('{')
   801  FieldLoop:
   802  	for i := range se.fields.list {
   803  		f := &se.fields.list[i]
   804  
   805  		// Find the nested struct field by following f.index.
   806  		fv := v
   807  		for _, i := range f.index {
   808  			if fv.Kind() == reflect.Pointer {
   809  				if fv.IsNil() {
   810  					continue FieldLoop
   811  				}
   812  				fv = fv.Elem()
   813  			}
   814  			fv = fv.Field(i)
   815  		}
   816  
   817  		if f.omitEmpty && isEmptyValue(fv) {
   818  			continue
   819  		}
   820  		e.WriteByte(next)
   821  		next = ','
   822  		if opts.escapeHTML {
   823  			e.WriteString(f.nameEscHTML)
   824  		} else {
   825  			e.WriteString(f.nameNonEsc)
   826  		}
   827  		opts.quoted = f.quoted
   828  		f.encoder(e, fv, opts)
   829  	}
   830  	if next == '{' {
   831  		e.WriteString("{}")
   832  	} else {
   833  		e.WriteByte('}')
   834  	}
   835  }
   836  
   837  func newStructEncoder(t reflect.Type) encoderFunc {
   838  	se := structEncoder{fields: cachedTypeFields(t)}
   839  	return se.encode
   840  }
   841  
   842  type mapEncoder struct {
   843  	elemEnc encoderFunc
   844  }
   845  
   846  func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
   847  	if v.IsNil() {
   848  		e.WriteString("null")
   849  		return
   850  	}
   851  	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
   852  		// We're a large number of nested ptrEncoder.encode calls deep;
   853  		// start checking if we've run into a pointer cycle.
   854  		ptr := v.UnsafePointer()
   855  		if _, ok := e.ptrSeen[ptr]; ok {
   856  			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
   857  		}
   858  		e.ptrSeen[ptr] = struct{}{}
   859  		defer delete(e.ptrSeen, ptr)
   860  	}
   861  	e.WriteByte('{')
   862  
   863  	// Extract and sort the keys.
   864  	sv := make([]reflectWithString, v.Len())
   865  	mi := v.MapRange()
   866  	for i := 0; mi.Next(); i++ {
   867  		sv[i].k = mi.Key()
   868  		sv[i].v = mi.Value()
   869  		if err := sv[i].resolve(); err != nil {
   870  			e.error(fmt.Errorf("json: encoding error for type %q: %q", v.Type().String(), err.Error()))
   871  		}
   872  	}
   873  	sort.Slice(sv, func(i, j int) bool { return sv[i].ks < sv[j].ks })
   874  
   875  	for i, kv := range sv {
   876  		if i > 0 {
   877  			e.WriteByte(',')
   878  		}
   879  		e.string(kv.ks, opts.escapeHTML)
   880  		e.WriteByte(':')
   881  		me.elemEnc(e, kv.v, opts)
   882  	}
   883  	e.WriteByte('}')
   884  	e.ptrLevel--
   885  }
   886  
   887  func newMapEncoder(t reflect.Type) encoderFunc {
   888  	switch t.Key().Kind() {
   889  	case reflect.String,
   890  		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
   891  		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   892  	default:
   893  		if !t.Key().Implements(textMarshalerType) {
   894  			return unsupportedTypeEncoder
   895  		}
   896  	}
   897  	me := mapEncoder{typeEncoder(t.Elem())}
   898  	return me.encode
   899  }
   900  
   901  func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) {
   902  	if v.IsNil() {
   903  		e.WriteString("null")
   904  		return
   905  	}
   906  	s := v.Bytes()
   907  	e.WriteByte('"')
   908  	encodedLen := base64.StdEncoding.EncodedLen(len(s))
   909  	if encodedLen <= len(e.scratch) {
   910  		// If the encoded bytes fit in e.scratch, avoid an extra
   911  		// allocation and use the cheaper Encoding.Encode.
   912  		dst := e.scratch[:encodedLen]
   913  		base64.StdEncoding.Encode(dst, s)
   914  		e.Write(dst)
   915  	} else if encodedLen <= 1024 {
   916  		// The encoded bytes are short enough to allocate for, and
   917  		// Encoding.Encode is still cheaper.
   918  		dst := make([]byte, encodedLen)
   919  		base64.StdEncoding.Encode(dst, s)
   920  		e.Write(dst)
   921  	} else {
   922  		// The encoded bytes are too long to cheaply allocate, and
   923  		// Encoding.Encode is no longer noticeably cheaper.
   924  		enc := base64.NewEncoder(base64.StdEncoding, e)
   925  		enc.Write(s)
   926  		enc.Close()
   927  	}
   928  	e.WriteByte('"')
   929  }
   930  
   931  // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
   932  type sliceEncoder struct {
   933  	arrayEnc encoderFunc
   934  }
   935  
   936  func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
   937  	if v.IsNil() {
   938  		e.WriteString("null")
   939  		return
   940  	}
   941  	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
   942  		// We're a large number of nested ptrEncoder.encode calls deep;
   943  		// start checking if we've run into a pointer cycle.
   944  		// Here we use a struct to memorize the pointer to the first element of the slice
   945  		// and its length.
   946  		ptr := struct {
   947  			ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe
   948  			len int
   949  		}{v.UnsafePointer(), v.Len()}
   950  		if _, ok := e.ptrSeen[ptr]; ok {
   951  			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
   952  		}
   953  		e.ptrSeen[ptr] = struct{}{}
   954  		defer delete(e.ptrSeen, ptr)
   955  	}
   956  	se.arrayEnc(e, v, opts)
   957  	e.ptrLevel--
   958  }
   959  
   960  func newSliceEncoder(t reflect.Type) encoderFunc {
   961  	// Byte slices get special treatment; arrays don't.
   962  	if t.Elem().Kind() == reflect.Uint8 {
   963  		p := reflect.PointerTo(t.Elem())
   964  		if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) {
   965  			return encodeByteSlice
   966  		}
   967  	}
   968  	enc := sliceEncoder{newArrayEncoder(t)}
   969  	return enc.encode
   970  }
   971  
   972  type arrayEncoder struct {
   973  	elemEnc encoderFunc
   974  }
   975  
   976  func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
   977  	e.WriteByte('[')
   978  	n := v.Len()
   979  	for i := 0; i < n; i++ {
   980  		if i > 0 {
   981  			e.WriteByte(',')
   982  		}
   983  		ae.elemEnc(e, v.Index(i), opts)
   984  	}
   985  	e.WriteByte(']')
   986  }
   987  
   988  func newArrayEncoder(t reflect.Type) encoderFunc {
   989  	enc := arrayEncoder{typeEncoder(t.Elem())}
   990  	return enc.encode
   991  }
   992  
   993  type ptrEncoder struct {
   994  	elemEnc encoderFunc
   995  }
   996  
   997  func (pe ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
   998  	if v.IsNil() {
   999  		e.WriteString("null")
  1000  		return
  1001  	}
  1002  	if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
  1003  		// We're a large number of nested ptrEncoder.encode calls deep;
  1004  		// start checking if we've run into a pointer cycle.
  1005  		ptr := v.Interface()
  1006  		if _, ok := e.ptrSeen[ptr]; ok {
  1007  			e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
  1008  		}
  1009  		e.ptrSeen[ptr] = struct{}{}
  1010  		defer delete(e.ptrSeen, ptr)
  1011  	}
  1012  	pe.elemEnc(e, v.Elem(), opts)
  1013  	e.ptrLevel--
  1014  }
  1015  
  1016  func newPtrEncoder(t reflect.Type) encoderFunc {
  1017  	enc := ptrEncoder{typeEncoder(t.Elem())}
  1018  	return enc.encode
  1019  }
  1020  
  1021  type condAddrEncoder struct {
  1022  	canAddrEnc, elseEnc encoderFunc
  1023  }
  1024  
  1025  func (ce condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  1026  	if v.CanAddr() {
  1027  		ce.canAddrEnc(e, v, opts)
  1028  	} else {
  1029  		ce.elseEnc(e, v, opts)
  1030  	}
  1031  }
  1032  
  1033  // newCondAddrEncoder returns an encoder that checks whether its value
  1034  // CanAddr and delegates to canAddrEnc if so, else to elseEnc.
  1035  func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
  1036  	enc := condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
  1037  	return enc.encode
  1038  }
  1039  
  1040  func isValidTag(s string) bool {
  1041  	if s == "" {
  1042  		return false
  1043  	}
  1044  	for _, c := range s {
  1045  		switch {
  1046  		case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
  1047  			// Backslash and quote chars are reserved, but
  1048  			// otherwise any punctuation chars are allowed
  1049  			// in a tag name.
  1050  		case !unicode.IsLetter(c) && !unicode.IsDigit(c):
  1051  			return false
  1052  		}
  1053  	}
  1054  	return true
  1055  }
  1056  
  1057  func typeByIndex(t reflect.Type, index []int) reflect.Type {
  1058  	for _, i := range index {
  1059  		if t.Kind() == reflect.Pointer {
  1060  			t = t.Elem()
  1061  		}
  1062  		t = t.Field(i).Type
  1063  	}
  1064  	return t
  1065  }
  1066  
  1067  type reflectWithString struct {
  1068  	k  reflect.Value
  1069  	v  reflect.Value
  1070  	ks string
  1071  }
  1072  
  1073  func (w *reflectWithString) resolve() error {
  1074  	if w.k.Kind() == reflect.String {
  1075  		w.ks = w.k.String()
  1076  		return nil
  1077  	}
  1078  	if tm, ok := w.k.Interface().(encoding.TextMarshaler); ok {
  1079  		if w.k.Kind() == reflect.Pointer && w.k.IsNil() {
  1080  			return nil
  1081  		}
  1082  		buf, err := tm.MarshalText()
  1083  		w.ks = string(buf)
  1084  		return err
  1085  	}
  1086  	switch w.k.Kind() {
  1087  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1088  		w.ks = strconv.FormatInt(w.k.Int(), 10)
  1089  		return nil
  1090  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1091  		w.ks = strconv.FormatUint(w.k.Uint(), 10)
  1092  		return nil
  1093  	}
  1094  	panic("unexpected map key type")
  1095  }
  1096  
  1097  // NOTE: keep in sync with stringBytes below.
  1098  func (e *encodeState) string(s string, escapeHTML bool) {
  1099  	e.WriteByte('"')
  1100  	start := 0
  1101  	for i := 0; i < len(s); {
  1102  		if b := s[i]; b < utf8.RuneSelf {
  1103  			if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
  1104  				i++
  1105  				continue
  1106  			}
  1107  			if start < i {
  1108  				e.WriteString(s[start:i])
  1109  			}
  1110  			e.WriteByte('\\')
  1111  			switch b {
  1112  			case '\\', '"':
  1113  				e.WriteByte(b)
  1114  			case '\n':
  1115  				e.WriteByte('n')
  1116  			case '\r':
  1117  				e.WriteByte('r')
  1118  			case '\t':
  1119  				e.WriteByte('t')
  1120  			default:
  1121  				// This encodes bytes < 0x20 except for \t, \n and \r.
  1122  				// If escapeHTML is set, it also escapes <, >, and &
  1123  				// because they can lead to security holes when
  1124  				// user-controlled strings are rendered into JSON
  1125  				// and served to some browsers.
  1126  				e.WriteString(`u00`)
  1127  				e.WriteByte(hex[b>>4])
  1128  				e.WriteByte(hex[b&0xF])
  1129  			}
  1130  			i++
  1131  			start = i
  1132  			continue
  1133  		}
  1134  		c, size := utf8.DecodeRuneInString(s[i:])
  1135  		if c == utf8.RuneError && size == 1 {
  1136  			if start < i {
  1137  				e.WriteString(s[start:i])
  1138  			}
  1139  			e.WriteString(`\ufffd`)
  1140  			i += size
  1141  			start = i
  1142  			continue
  1143  		}
  1144  		// U+2028 is LINE SEPARATOR.
  1145  		// U+2029 is PARAGRAPH SEPARATOR.
  1146  		// They are both technically valid characters in JSON strings,
  1147  		// but don't work in JSONP, which has to be evaluated as JavaScript,
  1148  		// and can lead to security holes there. It is valid JSON to
  1149  		// escape them, so we do so unconditionally.
  1150  		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
  1151  		if c == '\u2028' || c == '\u2029' {
  1152  			if start < i {
  1153  				e.WriteString(s[start:i])
  1154  			}
  1155  			e.WriteString(`\u202`)
  1156  			e.WriteByte(hex[c&0xF])
  1157  			i += size
  1158  			start = i
  1159  			continue
  1160  		}
  1161  		i += size
  1162  	}
  1163  	if start < len(s) {
  1164  		e.WriteString(s[start:])
  1165  	}
  1166  	e.WriteByte('"')
  1167  }
  1168  
  1169  // NOTE: keep in sync with string above.
  1170  func (e *encodeState) stringBytes(s []byte, escapeHTML bool) {
  1171  	e.WriteByte('"')
  1172  	start := 0
  1173  	for i := 0; i < len(s); {
  1174  		if b := s[i]; b < utf8.RuneSelf {
  1175  			if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
  1176  				i++
  1177  				continue
  1178  			}
  1179  			if start < i {
  1180  				e.Write(s[start:i])
  1181  			}
  1182  			e.WriteByte('\\')
  1183  			switch b {
  1184  			case '\\', '"':
  1185  				e.WriteByte(b)
  1186  			case '\n':
  1187  				e.WriteByte('n')
  1188  			case '\r':
  1189  				e.WriteByte('r')
  1190  			case '\t':
  1191  				e.WriteByte('t')
  1192  			default:
  1193  				// This encodes bytes < 0x20 except for \t, \n and \r.
  1194  				// If escapeHTML is set, it also escapes <, >, and &
  1195  				// because they can lead to security holes when
  1196  				// user-controlled strings are rendered into JSON
  1197  				// and served to some browsers.
  1198  				e.WriteString(`u00`)
  1199  				e.WriteByte(hex[b>>4])
  1200  				e.WriteByte(hex[b&0xF])
  1201  			}
  1202  			i++
  1203  			start = i
  1204  			continue
  1205  		}
  1206  		c, size := utf8.DecodeRune(s[i:])
  1207  		if c == utf8.RuneError && size == 1 {
  1208  			if start < i {
  1209  				e.Write(s[start:i])
  1210  			}
  1211  			e.WriteString(`\ufffd`)
  1212  			i += size
  1213  			start = i
  1214  			continue
  1215  		}
  1216  		// U+2028 is LINE SEPARATOR.
  1217  		// U+2029 is PARAGRAPH SEPARATOR.
  1218  		// They are both technically valid characters in JSON strings,
  1219  		// but don't work in JSONP, which has to be evaluated as JavaScript,
  1220  		// and can lead to security holes there. It is valid JSON to
  1221  		// escape them, so we do so unconditionally.
  1222  		// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
  1223  		if c == '\u2028' || c == '\u2029' {
  1224  			if start < i {
  1225  				e.Write(s[start:i])
  1226  			}
  1227  			e.WriteString(`\u202`)
  1228  			e.WriteByte(hex[c&0xF])
  1229  			i += size
  1230  			start = i
  1231  			continue
  1232  		}
  1233  		i += size
  1234  	}
  1235  	if start < len(s) {
  1236  		e.Write(s[start:])
  1237  	}
  1238  	e.WriteByte('"')
  1239  }
  1240  
  1241  // A field represents a single field found in a struct.
  1242  type field struct {
  1243  	name      string
  1244  	nameBytes []byte                 // []byte(name)
  1245  	equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
  1246  
  1247  	nameNonEsc  string // `"` + name + `":`
  1248  	nameEscHTML string // `"` + HTMLEscape(name) + `":`
  1249  
  1250  	tag       bool
  1251  	index     []int
  1252  	typ       reflect.Type
  1253  	omitEmpty bool
  1254  	quoted    bool
  1255  
  1256  	encoder encoderFunc
  1257  }
  1258  
  1259  // byIndex sorts field by index sequence.
  1260  type byIndex []field
  1261  
  1262  func (x byIndex) Len() int { return len(x) }
  1263  
  1264  func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  1265  
  1266  func (x byIndex) Less(i, j int) bool {
  1267  	for k, xik := range x[i].index {
  1268  		if k >= len(x[j].index) {
  1269  			return false
  1270  		}
  1271  		if xik != x[j].index[k] {
  1272  			return xik < x[j].index[k]
  1273  		}
  1274  	}
  1275  	return len(x[i].index) < len(x[j].index)
  1276  }
  1277  
  1278  // typeFields returns a list of fields that JSON should recognize for the given type.
  1279  // The algorithm is breadth-first search over the set of structs to include - the top struct
  1280  // and then any reachable anonymous structs.
  1281  func typeFields(t reflect.Type) structFields {
  1282  	// Anonymous fields to explore at the current level and the next.
  1283  	current := []field{}
  1284  	next := []field{{typ: t}}
  1285  
  1286  	// Count of queued names for current level and the next.
  1287  	var count, nextCount map[reflect.Type]int
  1288  
  1289  	// Types already visited at an earlier level.
  1290  	visited := map[reflect.Type]bool{}
  1291  
  1292  	// Fields found.
  1293  	var fields []field
  1294  
  1295  	// Buffer to run HTMLEscape on field names.
  1296  	var nameEscBuf bytes.Buffer
  1297  
  1298  	for len(next) > 0 {
  1299  		current, next = next, current[:0]
  1300  		count, nextCount = nextCount, map[reflect.Type]int{}
  1301  
  1302  		for _, f := range current {
  1303  			if visited[f.typ] {
  1304  				continue
  1305  			}
  1306  			visited[f.typ] = true
  1307  
  1308  			// Scan f.typ for fields to include.
  1309  			for i := 0; i < f.typ.NumField(); i++ {
  1310  				sf := f.typ.Field(i)
  1311  				if sf.Anonymous {
  1312  					t := sf.Type
  1313  					if t.Kind() == reflect.Pointer {
  1314  						t = t.Elem()
  1315  					}
  1316  					if !sf.IsExported() && t.Kind() != reflect.Struct {
  1317  						// Ignore embedded fields of unexported non-struct types.
  1318  						continue
  1319  					}
  1320  					// Do not ignore embedded fields of unexported struct types
  1321  					// since they may have exported fields.
  1322  				} else if !sf.IsExported() {
  1323  					// Ignore unexported non-embedded fields.
  1324  					continue
  1325  				}
  1326  				tag := sf.Tag.Get("json")
  1327  				if tag == "-" {
  1328  					continue
  1329  				}
  1330  				name, opts := parseTag(tag)
  1331  				if !isValidTag(name) {
  1332  					name = ""
  1333  				}
  1334  				index := make([]int, len(f.index)+1)
  1335  				copy(index, f.index)
  1336  				index[len(f.index)] = i
  1337  
  1338  				ft := sf.Type
  1339  				if ft.Name() == "" && ft.Kind() == reflect.Pointer {
  1340  					// Follow pointer.
  1341  					ft = ft.Elem()
  1342  				}
  1343  
  1344  				// Only strings, floats, integers, and booleans can be quoted.
  1345  				quoted := false
  1346  				if opts.Contains("string") {
  1347  					switch ft.Kind() {
  1348  					case reflect.Bool,
  1349  						reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  1350  						reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
  1351  						reflect.Float32, reflect.Float64,
  1352  						reflect.String:
  1353  						quoted = true
  1354  					}
  1355  				}
  1356  
  1357  				// Record found field and index sequence.
  1358  				if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
  1359  					tagged := name != ""
  1360  					if name == "" {
  1361  						name = sf.Name
  1362  					}
  1363  					field := field{
  1364  						name:      name,
  1365  						tag:       tagged,
  1366  						index:     index,
  1367  						typ:       ft,
  1368  						omitEmpty: opts.Contains("omitempty"),
  1369  						quoted:    quoted,
  1370  					}
  1371  					field.nameBytes = []byte(field.name)
  1372  					field.equalFold = foldFunc(field.nameBytes)
  1373  
  1374  					// Build nameEscHTML and nameNonEsc ahead of time.
  1375  					nameEscBuf.Reset()
  1376  					nameEscBuf.WriteString(`"`)
  1377  					HTMLEscape(&nameEscBuf, field.nameBytes)
  1378  					nameEscBuf.WriteString(`":`)
  1379  					field.nameEscHTML = nameEscBuf.String()
  1380  					field.nameNonEsc = `"` + field.name + `":`
  1381  
  1382  					fields = append(fields, field)
  1383  					if count[f.typ] > 1 {
  1384  						// If there were multiple instances, add a second,
  1385  						// so that the annihilation code will see a duplicate.
  1386  						// It only cares about the distinction between 1 or 2,
  1387  						// so don't bother generating any more copies.
  1388  						fields = append(fields, fields[len(fields)-1])
  1389  					}
  1390  					continue
  1391  				}
  1392  
  1393  				// Record new anonymous struct to explore in next round.
  1394  				nextCount[ft]++
  1395  				if nextCount[ft] == 1 {
  1396  					next = append(next, field{name: ft.Name(), index: index, typ: ft})
  1397  				}
  1398  			}
  1399  		}
  1400  	}
  1401  
  1402  	sort.Slice(fields, func(i, j int) bool {
  1403  		x := fields
  1404  		// sort field by name, breaking ties with depth, then
  1405  		// breaking ties with "name came from json tag", then
  1406  		// breaking ties with index sequence.
  1407  		if x[i].name != x[j].name {
  1408  			return x[i].name < x[j].name
  1409  		}
  1410  		if len(x[i].index) != len(x[j].index) {
  1411  			return len(x[i].index) < len(x[j].index)
  1412  		}
  1413  		if x[i].tag != x[j].tag {
  1414  			return x[i].tag
  1415  		}
  1416  		return byIndex(x).Less(i, j)
  1417  	})
  1418  
  1419  	// Delete all fields that are hidden by the Go rules for embedded fields,
  1420  	// except that fields with JSON tags are promoted.
  1421  
  1422  	// The fields are sorted in primary order of name, secondary order
  1423  	// of field index length. Loop over names; for each name, delete
  1424  	// hidden fields by choosing the one dominant field that survives.
  1425  	out := fields[:0]
  1426  	for advance, i := 0, 0; i < len(fields); i += advance {
  1427  		// One iteration per name.
  1428  		// Find the sequence of fields with the name of this first field.
  1429  		fi := fields[i]
  1430  		name := fi.name
  1431  		for advance = 1; i+advance < len(fields); advance++ {
  1432  			fj := fields[i+advance]
  1433  			if fj.name != name {
  1434  				break
  1435  			}
  1436  		}
  1437  		if advance == 1 { // Only one field with this name
  1438  			out = append(out, fi)
  1439  			continue
  1440  		}
  1441  		dominant, ok := dominantField(fields[i : i+advance])
  1442  		if ok {
  1443  			out = append(out, dominant)
  1444  		}
  1445  	}
  1446  
  1447  	fields = out
  1448  	sort.Sort(byIndex(fields))
  1449  
  1450  	for i := range fields {
  1451  		f := &fields[i]
  1452  		f.encoder = typeEncoder(typeByIndex(t, f.index))
  1453  	}
  1454  	nameIndex := make(map[string]int, len(fields))
  1455  	for i, field := range fields {
  1456  		nameIndex[field.name] = i
  1457  	}
  1458  	return structFields{fields, nameIndex}
  1459  }
  1460  
  1461  // dominantField looks through the fields, all of which are known to
  1462  // have the same name, to find the single field that dominates the
  1463  // others using Go's embedding rules, modified by the presence of
  1464  // JSON tags. If there are multiple top-level fields, the boolean
  1465  // will be false: This condition is an error in Go and we skip all
  1466  // the fields.
  1467  func dominantField(fields []field) (field, bool) {
  1468  	// The fields are sorted in increasing index-length order, then by presence of tag.
  1469  	// That means that the first field is the dominant one. We need only check
  1470  	// for error cases: two fields at top level, either both tagged or neither tagged.
  1471  	if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
  1472  		return field{}, false
  1473  	}
  1474  	return fields[0], true
  1475  }
  1476  
  1477  var fieldCache sync.Map // map[reflect.Type]structFields
  1478  
  1479  // cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
  1480  func cachedTypeFields(t reflect.Type) structFields {
  1481  	if f, ok := fieldCache.Load(t); ok {
  1482  		return f.(structFields)
  1483  	}
  1484  	f, _ := fieldCache.LoadOrStore(t, typeFields(t))
  1485  	return f.(structFields)
  1486  }
  1487  

View as plain text