Source file src/encoding/json/encode.go

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

View as plain text