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

View as plain text