k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/internal/third_party/go-json-experiment/json/arshal_default.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package json
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/base32"
    10  	"encoding/base64"
    11  	"encoding/hex"
    12  	"errors"
    13  	"fmt"
    14  	"math"
    15  	"reflect"
    16  	"sort"
    17  	"strconv"
    18  	"sync"
    19  )
    20  
    21  // optimizeCommon specifies whether to use optimizations targeted for certain
    22  // common patterns, rather than using the slower, but more general logic.
    23  // All tests should pass regardless of whether this is true or not.
    24  const optimizeCommon = true
    25  
    26  var (
    27  	// Most natural Go type that correspond with each JSON type.
    28  	anyType          = reflect.TypeOf((*any)(nil)).Elem()            // JSON value
    29  	boolType         = reflect.TypeOf((*bool)(nil)).Elem()           // JSON bool
    30  	stringType       = reflect.TypeOf((*string)(nil)).Elem()         // JSON string
    31  	float64Type      = reflect.TypeOf((*float64)(nil)).Elem()        // JSON number
    32  	mapStringAnyType = reflect.TypeOf((*map[string]any)(nil)).Elem() // JSON object
    33  	sliceAnyType     = reflect.TypeOf((*[]any)(nil)).Elem()          // JSON array
    34  
    35  	bytesType       = reflect.TypeOf((*[]byte)(nil)).Elem()
    36  	emptyStructType = reflect.TypeOf((*struct{})(nil)).Elem()
    37  )
    38  
    39  const startDetectingCyclesAfter = 1000
    40  
    41  type seenPointers map[typedPointer]struct{}
    42  
    43  type typedPointer struct {
    44  	typ reflect.Type
    45  	ptr any // always stores unsafe.Pointer, but avoids depending on unsafe
    46  }
    47  
    48  // visit visits pointer p of type t, reporting an error if seen before.
    49  // If successfully visited, then the caller must eventually call leave.
    50  func (m *seenPointers) visit(v reflect.Value) error {
    51  	p := typedPointer{v.Type(), v.UnsafePointer()}
    52  	if _, ok := (*m)[p]; ok {
    53  		return &SemanticError{action: "marshal", GoType: p.typ, Err: errors.New("encountered a cycle")}
    54  	}
    55  	if *m == nil {
    56  		*m = make(map[typedPointer]struct{})
    57  	}
    58  	(*m)[p] = struct{}{}
    59  	return nil
    60  }
    61  func (m *seenPointers) leave(v reflect.Value) {
    62  	p := typedPointer{v.Type(), v.UnsafePointer()}
    63  	delete(*m, p)
    64  }
    65  
    66  func makeDefaultArshaler(t reflect.Type) *arshaler {
    67  	switch t.Kind() {
    68  	case reflect.Bool:
    69  		return makeBoolArshaler(t)
    70  	case reflect.String:
    71  		return makeStringArshaler(t)
    72  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    73  		return makeIntArshaler(t)
    74  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    75  		return makeUintArshaler(t)
    76  	case reflect.Float32, reflect.Float64:
    77  		return makeFloatArshaler(t)
    78  	case reflect.Map:
    79  		return makeMapArshaler(t)
    80  	case reflect.Struct:
    81  		return makeStructArshaler(t)
    82  	case reflect.Slice:
    83  		fncs := makeSliceArshaler(t)
    84  		if t.AssignableTo(bytesType) {
    85  			return makeBytesArshaler(t, fncs)
    86  		}
    87  		return fncs
    88  	case reflect.Array:
    89  		fncs := makeArrayArshaler(t)
    90  		if reflect.SliceOf(t.Elem()).AssignableTo(bytesType) {
    91  			return makeBytesArshaler(t, fncs)
    92  		}
    93  		return fncs
    94  	case reflect.Pointer:
    95  		return makePointerArshaler(t)
    96  	case reflect.Interface:
    97  		return makeInterfaceArshaler(t)
    98  	default:
    99  		return makeInvalidArshaler(t)
   100  	}
   101  }
   102  
   103  func makeBoolArshaler(t reflect.Type) *arshaler {
   104  	var fncs arshaler
   105  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   106  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   107  			return newInvalidFormatError("marshal", t, mo.format)
   108  		}
   109  
   110  		// Optimize for marshaling without preceding whitespace.
   111  		if optimizeCommon && !enc.options.multiline && !enc.tokens.last.needObjectName() {
   112  			enc.buf = enc.tokens.mayAppendDelim(enc.buf, 't')
   113  			if va.Bool() {
   114  				enc.buf = append(enc.buf, "true"...)
   115  			} else {
   116  				enc.buf = append(enc.buf, "false"...)
   117  			}
   118  			enc.tokens.last.increment()
   119  			if enc.needFlush() {
   120  				return enc.flush()
   121  			}
   122  			return nil
   123  		}
   124  
   125  		return enc.WriteToken(Bool(va.Bool()))
   126  	}
   127  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   128  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   129  			return newInvalidFormatError("unmarshal", t, uo.format)
   130  		}
   131  		tok, err := dec.ReadToken()
   132  		if err != nil {
   133  			return err
   134  		}
   135  		k := tok.Kind()
   136  		switch k {
   137  		case 'n':
   138  			va.SetBool(false)
   139  			return nil
   140  		case 't', 'f':
   141  			va.SetBool(tok.Bool())
   142  			return nil
   143  		}
   144  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   145  	}
   146  	return &fncs
   147  }
   148  
   149  func makeStringArshaler(t reflect.Type) *arshaler {
   150  	var fncs arshaler
   151  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   152  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   153  			return newInvalidFormatError("marshal", t, mo.format)
   154  		}
   155  		return enc.WriteToken(String(va.String()))
   156  	}
   157  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   158  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   159  			return newInvalidFormatError("unmarshal", t, uo.format)
   160  		}
   161  		var flags valueFlags
   162  		val, err := dec.readValue(&flags)
   163  		if err != nil {
   164  			return err
   165  		}
   166  		k := val.Kind()
   167  		switch k {
   168  		case 'n':
   169  			va.SetString("")
   170  			return nil
   171  		case '"':
   172  			val = unescapeStringMayCopy(val, flags.isVerbatim())
   173  			if dec.stringCache == nil {
   174  				dec.stringCache = new(stringCache)
   175  			}
   176  			str := dec.stringCache.make(val)
   177  			va.SetString(str)
   178  			return nil
   179  		}
   180  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   181  	}
   182  	return &fncs
   183  }
   184  
   185  var (
   186  	encodeBase16        = func(dst, src []byte) { hex.Encode(dst, src) }
   187  	encodeBase32        = base32.StdEncoding.Encode
   188  	encodeBase32Hex     = base32.HexEncoding.Encode
   189  	encodeBase64        = base64.StdEncoding.Encode
   190  	encodeBase64URL     = base64.URLEncoding.Encode
   191  	encodedLenBase16    = hex.EncodedLen
   192  	encodedLenBase32    = base32.StdEncoding.EncodedLen
   193  	encodedLenBase32Hex = base32.HexEncoding.EncodedLen
   194  	encodedLenBase64    = base64.StdEncoding.EncodedLen
   195  	encodedLenBase64URL = base64.URLEncoding.EncodedLen
   196  	decodeBase16        = hex.Decode
   197  	decodeBase32        = base32.StdEncoding.Decode
   198  	decodeBase32Hex     = base32.HexEncoding.Decode
   199  	decodeBase64        = base64.StdEncoding.Decode
   200  	decodeBase64URL     = base64.URLEncoding.Decode
   201  	decodedLenBase16    = hex.DecodedLen
   202  	decodedLenBase32    = base32.StdEncoding.WithPadding(base32.NoPadding).DecodedLen
   203  	decodedLenBase32Hex = base32.HexEncoding.WithPadding(base32.NoPadding).DecodedLen
   204  	decodedLenBase64    = base64.StdEncoding.WithPadding(base64.NoPadding).DecodedLen
   205  	decodedLenBase64URL = base64.URLEncoding.WithPadding(base64.NoPadding).DecodedLen
   206  )
   207  
   208  func makeBytesArshaler(t reflect.Type, fncs *arshaler) *arshaler {
   209  	// NOTE: This handles both []byte and [N]byte.
   210  	marshalDefault := fncs.marshal
   211  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   212  		encode, encodedLen := encodeBase64, encodedLenBase64
   213  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   214  			switch mo.format {
   215  			case "base64":
   216  				encode, encodedLen = encodeBase64, encodedLenBase64
   217  			case "base64url":
   218  				encode, encodedLen = encodeBase64URL, encodedLenBase64URL
   219  			case "base32":
   220  				encode, encodedLen = encodeBase32, encodedLenBase32
   221  			case "base32hex":
   222  				encode, encodedLen = encodeBase32Hex, encodedLenBase32Hex
   223  			case "base16", "hex":
   224  				encode, encodedLen = encodeBase16, encodedLenBase16
   225  			case "array":
   226  				mo.format = ""
   227  				return marshalDefault(mo, enc, va)
   228  			default:
   229  				return newInvalidFormatError("marshal", t, mo.format)
   230  			}
   231  		}
   232  		val := enc.UnusedBuffer()
   233  		b := va.Bytes()
   234  		n := len(`"`) + encodedLen(len(b)) + len(`"`)
   235  		if cap(val) < n {
   236  			val = make([]byte, n)
   237  		} else {
   238  			val = val[:n]
   239  		}
   240  		val[0] = '"'
   241  		encode(val[len(`"`):len(val)-len(`"`)], b)
   242  		val[len(val)-1] = '"'
   243  		return enc.WriteValue(val)
   244  	}
   245  	unmarshalDefault := fncs.unmarshal
   246  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   247  		decode, decodedLen, encodedLen := decodeBase64, decodedLenBase64, encodedLenBase64
   248  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   249  			switch uo.format {
   250  			case "base64":
   251  				decode, decodedLen, encodedLen = decodeBase64, decodedLenBase64, encodedLenBase64
   252  			case "base64url":
   253  				decode, decodedLen, encodedLen = decodeBase64URL, decodedLenBase64URL, encodedLenBase64URL
   254  			case "base32":
   255  				decode, decodedLen, encodedLen = decodeBase32, decodedLenBase32, encodedLenBase32
   256  			case "base32hex":
   257  				decode, decodedLen, encodedLen = decodeBase32Hex, decodedLenBase32Hex, encodedLenBase32Hex
   258  			case "base16", "hex":
   259  				decode, decodedLen, encodedLen = decodeBase16, decodedLenBase16, encodedLenBase16
   260  			case "array":
   261  				uo.format = ""
   262  				return unmarshalDefault(uo, dec, va)
   263  			default:
   264  				return newInvalidFormatError("unmarshal", t, uo.format)
   265  			}
   266  		}
   267  		var flags valueFlags
   268  		val, err := dec.readValue(&flags)
   269  		if err != nil {
   270  			return err
   271  		}
   272  		k := val.Kind()
   273  		switch k {
   274  		case 'n':
   275  			va.Set(reflect.Zero(t))
   276  			return nil
   277  		case '"':
   278  			val = unescapeStringMayCopy(val, flags.isVerbatim())
   279  
   280  			// For base64 and base32, decodedLen computes the maximum output size
   281  			// when given the original input size. To compute the exact size,
   282  			// adjust the input size by excluding trailing padding characters.
   283  			// This is unnecessary for base16, but also harmless.
   284  			n := len(val)
   285  			for n > 0 && val[n-1] == '=' {
   286  				n--
   287  			}
   288  			n = decodedLen(n)
   289  			b := va.Bytes()
   290  			if va.Kind() == reflect.Array {
   291  				if n != len(b) {
   292  					err := fmt.Errorf("decoded base64 length of %d mismatches array length of %d", n, len(b))
   293  					return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   294  				}
   295  			} else {
   296  				if b == nil || cap(b) < n {
   297  					b = make([]byte, n)
   298  				} else {
   299  					b = b[:n]
   300  				}
   301  			}
   302  			n2, err := decode(b, val)
   303  			if err == nil && len(val) != encodedLen(n2) {
   304  				// TODO(https://go.dev/issue/53845): RFC 4648, section 3.3,
   305  				// specifies that non-alphabet characters must be rejected.
   306  				// Unfortunately, the "base32" and "base64" packages allow
   307  				// '\r' and '\n' characters by default.
   308  				err = errors.New("illegal data at input byte " + strconv.Itoa(bytes.IndexAny(val, "\r\n")))
   309  			}
   310  			if err != nil {
   311  				return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   312  			}
   313  			if va.Kind() == reflect.Slice {
   314  				va.SetBytes(b)
   315  			}
   316  			return nil
   317  		}
   318  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   319  	}
   320  	return fncs
   321  }
   322  
   323  func makeIntArshaler(t reflect.Type) *arshaler {
   324  	var fncs arshaler
   325  	bits := t.Bits()
   326  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   327  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   328  			return newInvalidFormatError("marshal", t, mo.format)
   329  		}
   330  
   331  		// Optimize for marshaling without preceding whitespace or string escaping.
   332  		if optimizeCommon && !enc.options.multiline && !mo.StringifyNumbers && !enc.tokens.last.needObjectName() {
   333  			enc.buf = enc.tokens.mayAppendDelim(enc.buf, '0')
   334  			enc.buf = strconv.AppendInt(enc.buf, va.Int(), 10)
   335  			enc.tokens.last.increment()
   336  			if enc.needFlush() {
   337  				return enc.flush()
   338  			}
   339  			return nil
   340  		}
   341  
   342  		x := math.Float64frombits(uint64(va.Int()))
   343  		return enc.writeNumber(x, rawIntNumber, mo.StringifyNumbers)
   344  	}
   345  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   346  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   347  			return newInvalidFormatError("unmarshal", t, uo.format)
   348  		}
   349  		var flags valueFlags
   350  		val, err := dec.readValue(&flags)
   351  		if err != nil {
   352  			return err
   353  		}
   354  		k := val.Kind()
   355  		switch k {
   356  		case 'n':
   357  			va.SetInt(0)
   358  			return nil
   359  		case '"':
   360  			if !uo.StringifyNumbers {
   361  				break
   362  			}
   363  			val = unescapeStringMayCopy(val, flags.isVerbatim())
   364  			fallthrough
   365  		case '0':
   366  			var negOffset int
   367  			neg := val[0] == '-'
   368  			if neg {
   369  				negOffset = 1
   370  			}
   371  			n, ok := parseDecUint(val[negOffset:])
   372  			maxInt := uint64(1) << (bits - 1)
   373  			overflow := (neg && n > maxInt) || (!neg && n > maxInt-1)
   374  			if !ok {
   375  				if n != math.MaxUint64 {
   376  					err := fmt.Errorf("cannot parse %q as signed integer: %w", val, strconv.ErrSyntax)
   377  					return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   378  				}
   379  				overflow = true
   380  			}
   381  			if overflow {
   382  				err := fmt.Errorf("cannot parse %q as signed integer: %w", val, strconv.ErrRange)
   383  				return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   384  			}
   385  			if neg {
   386  				va.SetInt(int64(-n))
   387  			} else {
   388  				va.SetInt(int64(+n))
   389  			}
   390  			return nil
   391  		}
   392  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   393  	}
   394  	return &fncs
   395  }
   396  
   397  func makeUintArshaler(t reflect.Type) *arshaler {
   398  	var fncs arshaler
   399  	bits := t.Bits()
   400  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   401  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   402  			return newInvalidFormatError("marshal", t, mo.format)
   403  		}
   404  
   405  		// Optimize for marshaling without preceding whitespace or string escaping.
   406  		if optimizeCommon && !enc.options.multiline && !mo.StringifyNumbers && !enc.tokens.last.needObjectName() {
   407  			enc.buf = enc.tokens.mayAppendDelim(enc.buf, '0')
   408  			enc.buf = strconv.AppendUint(enc.buf, va.Uint(), 10)
   409  			enc.tokens.last.increment()
   410  			if enc.needFlush() {
   411  				return enc.flush()
   412  			}
   413  			return nil
   414  		}
   415  
   416  		x := math.Float64frombits(va.Uint())
   417  		return enc.writeNumber(x, rawUintNumber, mo.StringifyNumbers)
   418  	}
   419  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   420  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   421  			return newInvalidFormatError("unmarshal", t, uo.format)
   422  		}
   423  		var flags valueFlags
   424  		val, err := dec.readValue(&flags)
   425  		if err != nil {
   426  			return err
   427  		}
   428  		k := val.Kind()
   429  		switch k {
   430  		case 'n':
   431  			va.SetUint(0)
   432  			return nil
   433  		case '"':
   434  			if !uo.StringifyNumbers {
   435  				break
   436  			}
   437  			val = unescapeStringMayCopy(val, flags.isVerbatim())
   438  			fallthrough
   439  		case '0':
   440  			n, ok := parseDecUint(val)
   441  			maxUint := uint64(1) << bits
   442  			overflow := n > maxUint-1
   443  			if !ok {
   444  				if n != math.MaxUint64 {
   445  					err := fmt.Errorf("cannot parse %q as unsigned integer: %w", val, strconv.ErrSyntax)
   446  					return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   447  				}
   448  				overflow = true
   449  			}
   450  			if overflow {
   451  				err := fmt.Errorf("cannot parse %q as unsigned integer: %w", val, strconv.ErrRange)
   452  				return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   453  			}
   454  			va.SetUint(n)
   455  			return nil
   456  		}
   457  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   458  	}
   459  	return &fncs
   460  }
   461  
   462  func makeFloatArshaler(t reflect.Type) *arshaler {
   463  	var fncs arshaler
   464  	bits := t.Bits()
   465  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   466  		var allowNonFinite bool
   467  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   468  			if mo.format == "nonfinite" {
   469  				allowNonFinite = true
   470  			} else {
   471  				return newInvalidFormatError("marshal", t, mo.format)
   472  			}
   473  		}
   474  
   475  		fv := va.Float()
   476  		if math.IsNaN(fv) || math.IsInf(fv, 0) {
   477  			if !allowNonFinite {
   478  				err := fmt.Errorf("invalid value: %v", fv)
   479  				return &SemanticError{action: "marshal", GoType: t, Err: err}
   480  			}
   481  			return enc.WriteToken(Float(fv))
   482  		}
   483  
   484  		// Optimize for marshaling without preceding whitespace or string escaping.
   485  		if optimizeCommon && !enc.options.multiline && !mo.StringifyNumbers && !enc.tokens.last.needObjectName() {
   486  			enc.buf = enc.tokens.mayAppendDelim(enc.buf, '0')
   487  			enc.buf = appendNumber(enc.buf, fv, bits)
   488  			enc.tokens.last.increment()
   489  			if enc.needFlush() {
   490  				return enc.flush()
   491  			}
   492  			return nil
   493  		}
   494  
   495  		return enc.writeNumber(fv, bits, mo.StringifyNumbers)
   496  	}
   497  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   498  		var allowNonFinite bool
   499  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   500  			if uo.format == "nonfinite" {
   501  				allowNonFinite = true
   502  			} else {
   503  				return newInvalidFormatError("unmarshal", t, uo.format)
   504  			}
   505  		}
   506  		var flags valueFlags
   507  		val, err := dec.readValue(&flags)
   508  		if err != nil {
   509  			return err
   510  		}
   511  		k := val.Kind()
   512  		switch k {
   513  		case 'n':
   514  			va.SetFloat(0)
   515  			return nil
   516  		case '"':
   517  			val = unescapeStringMayCopy(val, flags.isVerbatim())
   518  			if allowNonFinite {
   519  				switch string(val) {
   520  				case "NaN":
   521  					va.SetFloat(math.NaN())
   522  					return nil
   523  				case "Infinity":
   524  					va.SetFloat(math.Inf(+1))
   525  					return nil
   526  				case "-Infinity":
   527  					va.SetFloat(math.Inf(-1))
   528  					return nil
   529  				}
   530  			}
   531  			if !uo.StringifyNumbers {
   532  				break
   533  			}
   534  			if n, err := consumeNumber(val); n != len(val) || err != nil {
   535  				err := fmt.Errorf("cannot parse %q as JSON number: %w", val, strconv.ErrSyntax)
   536  				return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
   537  			}
   538  			fallthrough
   539  		case '0':
   540  			// NOTE: Floating-point parsing is by nature a lossy operation.
   541  			// We never report an overflow condition since we can always
   542  			// round the input to the closest representable finite value.
   543  			// For extremely large numbers, the closest value is ±MaxFloat.
   544  			fv, _ := parseFloat(val, bits)
   545  			va.SetFloat(fv)
   546  			return nil
   547  		}
   548  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   549  	}
   550  	return &fncs
   551  }
   552  
   553  func makeMapArshaler(t reflect.Type) *arshaler {
   554  	// NOTE: The logic below disables namespaces for tracking duplicate names
   555  	// when handling map keys with a unique representation.
   556  
   557  	// NOTE: Values retrieved from a map are not addressable,
   558  	// so we shallow copy the values to make them addressable and
   559  	// store them back into the map afterwards.
   560  
   561  	var fncs arshaler
   562  	var (
   563  		once    sync.Once
   564  		keyFncs *arshaler
   565  		valFncs *arshaler
   566  	)
   567  	init := func() {
   568  		keyFncs = lookupArshaler(t.Key())
   569  		valFncs = lookupArshaler(t.Elem())
   570  	}
   571  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   572  		// Check for cycles.
   573  		if enc.tokens.depth() > startDetectingCyclesAfter {
   574  			if err := enc.seenPointers.visit(va.Value); err != nil {
   575  				return err
   576  			}
   577  			defer enc.seenPointers.leave(va.Value)
   578  		}
   579  
   580  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   581  			if mo.format == "emitnull" {
   582  				if va.IsNil() {
   583  					return enc.WriteToken(Null)
   584  				}
   585  				mo.format = ""
   586  			} else {
   587  				return newInvalidFormatError("marshal", t, mo.format)
   588  			}
   589  		}
   590  
   591  		// Optimize for marshaling an empty map without any preceding whitespace.
   592  		n := va.Len()
   593  		if optimizeCommon && n == 0 && !enc.options.multiline && !enc.tokens.last.needObjectName() {
   594  			enc.buf = enc.tokens.mayAppendDelim(enc.buf, '{')
   595  			enc.buf = append(enc.buf, "{}"...)
   596  			enc.tokens.last.increment()
   597  			if enc.needFlush() {
   598  				return enc.flush()
   599  			}
   600  			return nil
   601  		}
   602  
   603  		once.Do(init)
   604  		if err := enc.WriteToken(ObjectStart); err != nil {
   605  			return err
   606  		}
   607  		if n > 0 {
   608  			// Handle maps with numeric key types by stringifying them.
   609  			mko := mo
   610  			mko.StringifyNumbers = true
   611  
   612  			nonDefaultKey := keyFncs.nonDefault
   613  			marshalKey := keyFncs.marshal
   614  			marshalVal := valFncs.marshal
   615  			if mo.Marshalers != nil {
   616  				var ok bool
   617  				marshalKey, ok = mo.Marshalers.lookup(marshalKey, t.Key())
   618  				marshalVal, _ = mo.Marshalers.lookup(marshalVal, t.Elem())
   619  				nonDefaultKey = nonDefaultKey || ok
   620  			}
   621  			k := newAddressableValue(t.Key())
   622  			v := newAddressableValue(t.Elem())
   623  
   624  			// A Go map guarantees that each entry has a unique key.
   625  			// As such, disable the expensive duplicate name check if we know
   626  			// that every Go key will serialize as a unique JSON string.
   627  			if !nonDefaultKey && mapKeyWithUniqueRepresentation(k.Kind(), enc.options.AllowInvalidUTF8) {
   628  				enc.tokens.last.disableNamespace()
   629  			}
   630  
   631  			switch {
   632  			case !mo.Deterministic || n <= 1:
   633  				for iter := va.Value.MapRange(); iter.Next(); {
   634  					k.SetIterKey(iter)
   635  					if err := marshalKey(mko, enc, k); err != nil {
   636  						// TODO: If err is errMissingName, then wrap it as a
   637  						// SemanticError since this key type cannot be serialized
   638  						// as a JSON string.
   639  						return err
   640  					}
   641  					v.SetIterValue(iter)
   642  					if err := marshalVal(mo, enc, v); err != nil {
   643  						return err
   644  					}
   645  				}
   646  			case !nonDefaultKey && t.Key().Kind() == reflect.String:
   647  				names := getStrings(n)
   648  				for i, iter := 0, va.Value.MapRange(); i < n && iter.Next(); i++ {
   649  					k.SetIterKey(iter)
   650  					(*names)[i] = k.String()
   651  				}
   652  				names.Sort()
   653  				for _, name := range *names {
   654  					if err := enc.WriteToken(String(name)); err != nil {
   655  						return err
   656  					}
   657  					// TODO(https://go.dev/issue/57061): Use v.SetMapIndexOf.
   658  					k.SetString(name)
   659  					v.Set(va.MapIndex(k.Value))
   660  					if err := marshalVal(mo, enc, v); err != nil {
   661  						return err
   662  					}
   663  				}
   664  				putStrings(names)
   665  			default:
   666  				type member struct {
   667  					name string // unquoted name
   668  					key  addressableValue
   669  				}
   670  				members := make([]member, n)
   671  				keys := reflect.MakeSlice(reflect.SliceOf(t.Key()), n, n)
   672  				for i, iter := 0, va.Value.MapRange(); i < n && iter.Next(); i++ {
   673  					// Marshal the member name.
   674  					k := addressableValue{keys.Index(i)} // indexed slice element is always addressable
   675  					k.SetIterKey(iter)
   676  					if err := marshalKey(mko, enc, k); err != nil {
   677  						// TODO: If err is errMissingName, then wrap it as a
   678  						// SemanticError since this key type cannot be serialized
   679  						// as a JSON string.
   680  						return err
   681  					}
   682  					name := enc.unwriteOnlyObjectMemberName()
   683  					members[i] = member{name, k}
   684  				}
   685  				// TODO: If AllowDuplicateNames is enabled, then sort according
   686  				// to reflect.Value as well if the names are equal.
   687  				// See internal/fmtsort.
   688  				// TODO(https://go.dev/issue/47619): Use slices.SortFunc instead.
   689  				sort.Slice(members, func(i, j int) bool {
   690  					return lessUTF16(members[i].name, members[j].name)
   691  				})
   692  				for _, member := range members {
   693  					if err := enc.WriteToken(String(member.name)); err != nil {
   694  						return err
   695  					}
   696  					// TODO(https://go.dev/issue/57061): Use v.SetMapIndexOf.
   697  					v.Set(va.MapIndex(member.key.Value))
   698  					if err := marshalVal(mo, enc, v); err != nil {
   699  						return err
   700  					}
   701  				}
   702  			}
   703  		}
   704  		if err := enc.WriteToken(ObjectEnd); err != nil {
   705  			return err
   706  		}
   707  		return nil
   708  	}
   709  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   710  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   711  			if uo.format == "emitnull" {
   712  				uo.format = "" // only relevant for marshaling
   713  			} else {
   714  				return newInvalidFormatError("unmarshal", t, uo.format)
   715  			}
   716  		}
   717  		tok, err := dec.ReadToken()
   718  		if err != nil {
   719  			return err
   720  		}
   721  		k := tok.Kind()
   722  		switch k {
   723  		case 'n':
   724  			va.Set(reflect.Zero(t))
   725  			return nil
   726  		case '{':
   727  			once.Do(init)
   728  			if va.IsNil() {
   729  				va.Set(reflect.MakeMap(t))
   730  			}
   731  
   732  			// Handle maps with numeric key types by stringifying them.
   733  			uko := uo
   734  			uko.StringifyNumbers = true
   735  
   736  			nonDefaultKey := keyFncs.nonDefault
   737  			unmarshalKey := keyFncs.unmarshal
   738  			unmarshalVal := valFncs.unmarshal
   739  			if uo.Unmarshalers != nil {
   740  				var ok bool
   741  				unmarshalKey, ok = uo.Unmarshalers.lookup(unmarshalKey, t.Key())
   742  				unmarshalVal, _ = uo.Unmarshalers.lookup(unmarshalVal, t.Elem())
   743  				nonDefaultKey = nonDefaultKey || ok
   744  			}
   745  			k := newAddressableValue(t.Key())
   746  			v := newAddressableValue(t.Elem())
   747  
   748  			// Manually check for duplicate entries by virtue of whether the
   749  			// unmarshaled key already exists in the destination Go map.
   750  			// Consequently, syntactically different names (e.g., "0" and "-0")
   751  			// will be rejected as duplicates since they semantically refer
   752  			// to the same Go value. This is an unusual interaction
   753  			// between syntax and semantics, but is more correct.
   754  			if !nonDefaultKey && mapKeyWithUniqueRepresentation(k.Kind(), dec.options.AllowInvalidUTF8) {
   755  				dec.tokens.last.disableNamespace()
   756  			}
   757  
   758  			// In the rare case where the map is not already empty,
   759  			// then we need to manually track which keys we already saw
   760  			// since existing presence alone is insufficient to indicate
   761  			// whether the input had a duplicate name.
   762  			var seen reflect.Value
   763  			if !dec.options.AllowDuplicateNames && va.Len() > 0 {
   764  				seen = reflect.MakeMap(reflect.MapOf(k.Type(), emptyStructType))
   765  			}
   766  
   767  			for dec.PeekKind() != '}' {
   768  				k.Set(reflect.Zero(t.Key()))
   769  				if err := unmarshalKey(uko, dec, k); err != nil {
   770  					return err
   771  				}
   772  				if k.Kind() == reflect.Interface && !k.IsNil() && !k.Elem().Type().Comparable() {
   773  					err := fmt.Errorf("invalid incomparable key type %v", k.Elem().Type())
   774  					return &SemanticError{action: "unmarshal", GoType: t, Err: err}
   775  				}
   776  
   777  				if v2 := va.MapIndex(k.Value); v2.IsValid() {
   778  					if !dec.options.AllowDuplicateNames && (!seen.IsValid() || seen.MapIndex(k.Value).IsValid()) {
   779  						// TODO: Unread the object name.
   780  						name := dec.previousBuffer()
   781  						err := &SyntacticError{str: "duplicate name " + string(name) + " in object"}
   782  						return err.withOffset(dec.InputOffset() - int64(len(name)))
   783  					}
   784  					v.Set(v2)
   785  				} else {
   786  					v.Set(reflect.Zero(v.Type()))
   787  				}
   788  				err := unmarshalVal(uo, dec, v)
   789  				va.SetMapIndex(k.Value, v.Value)
   790  				if seen.IsValid() {
   791  					seen.SetMapIndex(k.Value, reflect.Zero(emptyStructType))
   792  				}
   793  				if err != nil {
   794  					return err
   795  				}
   796  			}
   797  			if _, err := dec.ReadToken(); err != nil {
   798  				return err
   799  			}
   800  			return nil
   801  		}
   802  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
   803  	}
   804  	return &fncs
   805  }
   806  
   807  // mapKeyWithUniqueRepresentation reports whether all possible values of k
   808  // marshal to a different JSON value, and whether all possible JSON values
   809  // that can unmarshal into k unmarshal to different Go values.
   810  // In other words, the representation must be a bijective.
   811  func mapKeyWithUniqueRepresentation(k reflect.Kind, allowInvalidUTF8 bool) bool {
   812  	switch k {
   813  	case reflect.Bool,
   814  		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
   815  		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
   816  		return true
   817  	case reflect.String:
   818  		// For strings, we have to be careful since names with invalid UTF-8
   819  		// maybe unescape to the same Go string value.
   820  		return !allowInvalidUTF8
   821  	default:
   822  		// Floating-point kinds are not listed above since NaNs
   823  		// can appear multiple times and all serialize as "NaN".
   824  		return false
   825  	}
   826  }
   827  
   828  func makeStructArshaler(t reflect.Type) *arshaler {
   829  	// NOTE: The logic below disables namespaces for tracking duplicate names
   830  	// and does the tracking locally with an efficient bit-set based on which
   831  	// Go struct fields were seen.
   832  
   833  	var fncs arshaler
   834  	var (
   835  		once    sync.Once
   836  		fields  structFields
   837  		errInit *SemanticError
   838  	)
   839  	init := func() {
   840  		fields, errInit = makeStructFields(t)
   841  	}
   842  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
   843  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
   844  			return newInvalidFormatError("marshal", t, mo.format)
   845  		}
   846  		once.Do(init)
   847  		if errInit != nil {
   848  			err := *errInit // shallow copy SemanticError
   849  			err.action = "marshal"
   850  			return &err
   851  		}
   852  		if err := enc.WriteToken(ObjectStart); err != nil {
   853  			return err
   854  		}
   855  		var seenIdxs uintSet
   856  		prevIdx := -1
   857  		enc.tokens.last.disableNamespace() // we manually ensure unique names below
   858  		for i := range fields.flattened {
   859  			f := &fields.flattened[i]
   860  			v := addressableValue{va.Field(f.index[0])} // addressable if struct value is addressable
   861  			if len(f.index) > 1 {
   862  				v = v.fieldByIndex(f.index[1:], false)
   863  				if !v.IsValid() {
   864  					continue // implies a nil inlined field
   865  				}
   866  			}
   867  
   868  			// OmitZero skips the field if the Go value is zero,
   869  			// which we can determine up front without calling the marshaler.
   870  			if f.omitzero && ((f.isZero == nil && v.IsZero()) || (f.isZero != nil && f.isZero(v))) {
   871  				continue
   872  			}
   873  
   874  			marshal := f.fncs.marshal
   875  			nonDefault := f.fncs.nonDefault
   876  			if mo.Marshalers != nil {
   877  				var ok bool
   878  				marshal, ok = mo.Marshalers.lookup(marshal, f.typ)
   879  				nonDefault = nonDefault || ok
   880  			}
   881  
   882  			// OmitEmpty skips the field if the marshaled JSON value is empty,
   883  			// which we can know up front if there are no custom marshalers,
   884  			// otherwise we must marshal the value and unwrite it if empty.
   885  			if f.omitempty && !nonDefault && f.isEmpty != nil && f.isEmpty(v) {
   886  				continue // fast path for omitempty
   887  			}
   888  
   889  			// Write the object member name.
   890  			//
   891  			// The logic below is semantically equivalent to:
   892  			//	enc.WriteToken(String(f.name))
   893  			// but specialized and simplified because:
   894  			//	1. The Encoder must be expecting an object name.
   895  			//	2. The object namespace is guaranteed to be disabled.
   896  			//	3. The object name is guaranteed to be valid and pre-escaped.
   897  			//	4. There is no need to flush the buffer (for unwrite purposes).
   898  			//	5. There is no possibility of an error occurring.
   899  			if optimizeCommon {
   900  				// Append any delimiters or optional whitespace.
   901  				if enc.tokens.last.length() > 0 {
   902  					enc.buf = append(enc.buf, ',')
   903  				}
   904  				if enc.options.multiline {
   905  					enc.buf = enc.appendIndent(enc.buf, enc.tokens.needIndent('"'))
   906  				}
   907  
   908  				// Append the token to the output and to the state machine.
   909  				n0 := len(enc.buf) // offset before calling appendString
   910  				if enc.options.EscapeRune == nil {
   911  					enc.buf = append(enc.buf, f.quotedName...)
   912  				} else {
   913  					enc.buf, _ = appendString(enc.buf, f.name, false, enc.options.EscapeRune)
   914  				}
   915  				if !enc.options.AllowDuplicateNames {
   916  					enc.names.replaceLastQuotedOffset(n0)
   917  				}
   918  				enc.tokens.last.increment()
   919  			} else {
   920  				if err := enc.WriteToken(String(f.name)); err != nil {
   921  					return err
   922  				}
   923  			}
   924  
   925  			// Write the object member value.
   926  			mo2 := mo
   927  			if f.string {
   928  				mo2.StringifyNumbers = true
   929  			}
   930  			if f.format != "" {
   931  				mo2.formatDepth = enc.tokens.depth()
   932  				mo2.format = f.format
   933  			}
   934  			if err := marshal(mo2, enc, v); err != nil {
   935  				return err
   936  			}
   937  
   938  			// Try unwriting the member if empty (slow path for omitempty).
   939  			if f.omitempty {
   940  				var prevName *string
   941  				if prevIdx >= 0 {
   942  					prevName = &fields.flattened[prevIdx].name
   943  				}
   944  				if enc.unwriteEmptyObjectMember(prevName) {
   945  					continue
   946  				}
   947  			}
   948  
   949  			// Remember the previous written object member.
   950  			// The set of seen fields only needs to be updated to detect
   951  			// duplicate names with those from the inlined fallback.
   952  			if !enc.options.AllowDuplicateNames && fields.inlinedFallback != nil {
   953  				seenIdxs.insert(uint(f.id))
   954  			}
   955  			prevIdx = f.id
   956  		}
   957  		if fields.inlinedFallback != nil && !(mo.DiscardUnknownMembers && fields.inlinedFallback.unknown) {
   958  			var insertUnquotedName func([]byte) bool
   959  			if !enc.options.AllowDuplicateNames {
   960  				insertUnquotedName = func(name []byte) bool {
   961  					// Check that the name from inlined fallback does not match
   962  					// one of the previously marshaled names from known fields.
   963  					if foldedFields := fields.byFoldedName[string(foldName(name))]; len(foldedFields) > 0 {
   964  						if f := fields.byActualName[string(name)]; f != nil {
   965  							return seenIdxs.insert(uint(f.id))
   966  						}
   967  						for _, f := range foldedFields {
   968  							if f.nocase {
   969  								return seenIdxs.insert(uint(f.id))
   970  							}
   971  						}
   972  					}
   973  
   974  					// Check that the name does not match any other name
   975  					// previously marshaled from the inlined fallback.
   976  					return enc.namespaces.last().insertUnquoted(name)
   977  				}
   978  			}
   979  			if err := marshalInlinedFallbackAll(mo, enc, va, fields.inlinedFallback, insertUnquotedName); err != nil {
   980  				return err
   981  			}
   982  		}
   983  		if err := enc.WriteToken(ObjectEnd); err != nil {
   984  			return err
   985  		}
   986  		return nil
   987  	}
   988  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
   989  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
   990  			return newInvalidFormatError("unmarshal", t, uo.format)
   991  		}
   992  		tok, err := dec.ReadToken()
   993  		if err != nil {
   994  			return err
   995  		}
   996  		k := tok.Kind()
   997  		switch k {
   998  		case 'n':
   999  			va.Set(reflect.Zero(t))
  1000  			return nil
  1001  		case '{':
  1002  			once.Do(init)
  1003  			if errInit != nil {
  1004  				err := *errInit // shallow copy SemanticError
  1005  				err.action = "unmarshal"
  1006  				return &err
  1007  			}
  1008  			var seenIdxs uintSet
  1009  			dec.tokens.last.disableNamespace()
  1010  			for dec.PeekKind() != '}' {
  1011  				// Process the object member name.
  1012  				var flags valueFlags
  1013  				val, err := dec.readValue(&flags)
  1014  				if err != nil {
  1015  					return err
  1016  				}
  1017  				name := unescapeStringMayCopy(val, flags.isVerbatim())
  1018  				f := fields.byActualName[string(name)]
  1019  				if f == nil {
  1020  					for _, f2 := range fields.byFoldedName[string(foldName(name))] {
  1021  						if f2.nocase {
  1022  							f = f2
  1023  							break
  1024  						}
  1025  					}
  1026  					if f == nil {
  1027  						if uo.RejectUnknownMembers && (fields.inlinedFallback == nil || fields.inlinedFallback.unknown) {
  1028  							return &SemanticError{action: "unmarshal", GoType: t, Err: fmt.Errorf("unknown name %s", val)}
  1029  						}
  1030  						if !dec.options.AllowDuplicateNames && !dec.namespaces.last().insertUnquoted(name) {
  1031  							// TODO: Unread the object name.
  1032  							err := &SyntacticError{str: "duplicate name " + string(val) + " in object"}
  1033  							return err.withOffset(dec.InputOffset() - int64(len(val)))
  1034  						}
  1035  
  1036  						if fields.inlinedFallback == nil {
  1037  							// Skip unknown value since we have no place to store it.
  1038  							if err := dec.SkipValue(); err != nil {
  1039  								return err
  1040  							}
  1041  						} else {
  1042  							// Marshal into value capable of storing arbitrary object members.
  1043  							if err := unmarshalInlinedFallbackNext(uo, dec, va, fields.inlinedFallback, val, name); err != nil {
  1044  								return err
  1045  							}
  1046  						}
  1047  						continue
  1048  					}
  1049  				}
  1050  				if !dec.options.AllowDuplicateNames && !seenIdxs.insert(uint(f.id)) {
  1051  					// TODO: Unread the object name.
  1052  					err := &SyntacticError{str: "duplicate name " + string(val) + " in object"}
  1053  					return err.withOffset(dec.InputOffset() - int64(len(val)))
  1054  				}
  1055  
  1056  				// Process the object member value.
  1057  				unmarshal := f.fncs.unmarshal
  1058  				if uo.Unmarshalers != nil {
  1059  					unmarshal, _ = uo.Unmarshalers.lookup(unmarshal, f.typ)
  1060  				}
  1061  				uo2 := uo
  1062  				if f.string {
  1063  					uo2.StringifyNumbers = true
  1064  				}
  1065  				if f.format != "" {
  1066  					uo2.formatDepth = dec.tokens.depth()
  1067  					uo2.format = f.format
  1068  				}
  1069  				v := addressableValue{va.Field(f.index[0])} // addressable if struct value is addressable
  1070  				if len(f.index) > 1 {
  1071  					v = v.fieldByIndex(f.index[1:], true)
  1072  				}
  1073  				if err := unmarshal(uo2, dec, v); err != nil {
  1074  					return err
  1075  				}
  1076  			}
  1077  			if _, err := dec.ReadToken(); err != nil {
  1078  				return err
  1079  			}
  1080  			return nil
  1081  		}
  1082  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
  1083  	}
  1084  	return &fncs
  1085  }
  1086  
  1087  func (va addressableValue) fieldByIndex(index []int, mayAlloc bool) addressableValue {
  1088  	for _, i := range index {
  1089  		va = va.indirect(mayAlloc)
  1090  		if !va.IsValid() {
  1091  			return va
  1092  		}
  1093  		va = addressableValue{va.Field(i)} // addressable if struct value is addressable
  1094  	}
  1095  	return va
  1096  }
  1097  
  1098  func (va addressableValue) indirect(mayAlloc bool) addressableValue {
  1099  	if va.Kind() == reflect.Pointer {
  1100  		if va.IsNil() {
  1101  			if !mayAlloc {
  1102  				return addressableValue{}
  1103  			}
  1104  			va.Set(reflect.New(va.Type().Elem()))
  1105  		}
  1106  		va = addressableValue{va.Elem()} // dereferenced pointer is always addressable
  1107  	}
  1108  	return va
  1109  }
  1110  
  1111  func makeSliceArshaler(t reflect.Type) *arshaler {
  1112  	var fncs arshaler
  1113  	var (
  1114  		once    sync.Once
  1115  		valFncs *arshaler
  1116  	)
  1117  	init := func() {
  1118  		valFncs = lookupArshaler(t.Elem())
  1119  	}
  1120  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
  1121  		// Check for cycles.
  1122  		if enc.tokens.depth() > startDetectingCyclesAfter {
  1123  			if err := enc.seenPointers.visit(va.Value); err != nil {
  1124  				return err
  1125  			}
  1126  			defer enc.seenPointers.leave(va.Value)
  1127  		}
  1128  
  1129  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
  1130  			if mo.format == "emitnull" {
  1131  				if va.IsNil() {
  1132  					return enc.WriteToken(Null)
  1133  				}
  1134  				mo.format = ""
  1135  			} else {
  1136  				return newInvalidFormatError("marshal", t, mo.format)
  1137  			}
  1138  		}
  1139  
  1140  		// Optimize for marshaling an empty slice without any preceding whitespace.
  1141  		n := va.Len()
  1142  		if optimizeCommon && n == 0 && !enc.options.multiline && !enc.tokens.last.needObjectName() {
  1143  			enc.buf = enc.tokens.mayAppendDelim(enc.buf, '[')
  1144  			enc.buf = append(enc.buf, "[]"...)
  1145  			enc.tokens.last.increment()
  1146  			if enc.needFlush() {
  1147  				return enc.flush()
  1148  			}
  1149  			return nil
  1150  		}
  1151  
  1152  		once.Do(init)
  1153  		if err := enc.WriteToken(ArrayStart); err != nil {
  1154  			return err
  1155  		}
  1156  		marshal := valFncs.marshal
  1157  		if mo.Marshalers != nil {
  1158  			marshal, _ = mo.Marshalers.lookup(marshal, t.Elem())
  1159  		}
  1160  		for i := 0; i < n; i++ {
  1161  			v := addressableValue{va.Index(i)} // indexed slice element is always addressable
  1162  			if err := marshal(mo, enc, v); err != nil {
  1163  				return err
  1164  			}
  1165  		}
  1166  		if err := enc.WriteToken(ArrayEnd); err != nil {
  1167  			return err
  1168  		}
  1169  		return nil
  1170  	}
  1171  	emptySlice := reflect.MakeSlice(t, 0, 0)
  1172  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
  1173  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
  1174  			if uo.format == "emitnull" {
  1175  				uo.format = "" // only relevant for marshaling
  1176  			} else {
  1177  				return newInvalidFormatError("unmarshal", t, uo.format)
  1178  			}
  1179  		}
  1180  
  1181  		tok, err := dec.ReadToken()
  1182  		if err != nil {
  1183  			return err
  1184  		}
  1185  		k := tok.Kind()
  1186  		switch k {
  1187  		case 'n':
  1188  			va.Set(reflect.Zero(t))
  1189  			return nil
  1190  		case '[':
  1191  			once.Do(init)
  1192  			unmarshal := valFncs.unmarshal
  1193  			if uo.Unmarshalers != nil {
  1194  				unmarshal, _ = uo.Unmarshalers.lookup(unmarshal, t.Elem())
  1195  			}
  1196  			mustZero := true // we do not know the cleanliness of unused capacity
  1197  			cap := va.Cap()
  1198  			if cap > 0 {
  1199  				va.SetLen(cap)
  1200  			}
  1201  			var i int
  1202  			for dec.PeekKind() != ']' {
  1203  				if i == cap {
  1204  					// TODO(https://go.dev/issue/48000): Use reflect.Value.Append.
  1205  					va.Set(reflect.Append(va.Value, reflect.Zero(t.Elem())))
  1206  					cap = va.Cap()
  1207  					va.SetLen(cap)
  1208  					mustZero = false // append guarantees that unused capacity is zero-initialized
  1209  				}
  1210  				v := addressableValue{va.Index(i)} // indexed slice element is always addressable
  1211  				i++
  1212  				if mustZero {
  1213  					v.Set(reflect.Zero(t.Elem()))
  1214  				}
  1215  				if err := unmarshal(uo, dec, v); err != nil {
  1216  					va.SetLen(i)
  1217  					return err
  1218  				}
  1219  			}
  1220  			if i == 0 {
  1221  				va.Set(emptySlice)
  1222  			} else {
  1223  				va.SetLen(i)
  1224  			}
  1225  			if _, err := dec.ReadToken(); err != nil {
  1226  				return err
  1227  			}
  1228  			return nil
  1229  		}
  1230  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
  1231  	}
  1232  	return &fncs
  1233  }
  1234  
  1235  func makeArrayArshaler(t reflect.Type) *arshaler {
  1236  	var fncs arshaler
  1237  	var (
  1238  		once    sync.Once
  1239  		valFncs *arshaler
  1240  	)
  1241  	init := func() {
  1242  		valFncs = lookupArshaler(t.Elem())
  1243  	}
  1244  	n := t.Len()
  1245  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
  1246  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
  1247  			return newInvalidFormatError("marshal", t, mo.format)
  1248  		}
  1249  		once.Do(init)
  1250  		if err := enc.WriteToken(ArrayStart); err != nil {
  1251  			return err
  1252  		}
  1253  		marshal := valFncs.marshal
  1254  		if mo.Marshalers != nil {
  1255  			marshal, _ = mo.Marshalers.lookup(marshal, t.Elem())
  1256  		}
  1257  		for i := 0; i < n; i++ {
  1258  			v := addressableValue{va.Index(i)} // indexed array element is addressable if array is addressable
  1259  			if err := marshal(mo, enc, v); err != nil {
  1260  				return err
  1261  			}
  1262  		}
  1263  		if err := enc.WriteToken(ArrayEnd); err != nil {
  1264  			return err
  1265  		}
  1266  		return nil
  1267  	}
  1268  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
  1269  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
  1270  			return newInvalidFormatError("unmarshal", t, uo.format)
  1271  		}
  1272  		tok, err := dec.ReadToken()
  1273  		if err != nil {
  1274  			return err
  1275  		}
  1276  		k := tok.Kind()
  1277  		switch k {
  1278  		case 'n':
  1279  			va.Set(reflect.Zero(t))
  1280  			return nil
  1281  		case '[':
  1282  			once.Do(init)
  1283  			unmarshal := valFncs.unmarshal
  1284  			if uo.Unmarshalers != nil {
  1285  				unmarshal, _ = uo.Unmarshalers.lookup(unmarshal, t.Elem())
  1286  			}
  1287  			var i int
  1288  			for dec.PeekKind() != ']' {
  1289  				if i >= n {
  1290  					err := errors.New("too many array elements")
  1291  					return &SemanticError{action: "unmarshal", GoType: t, Err: err}
  1292  				}
  1293  				v := addressableValue{va.Index(i)} // indexed array element is addressable if array is addressable
  1294  				v.Set(reflect.Zero(v.Type()))
  1295  				if err := unmarshal(uo, dec, v); err != nil {
  1296  					return err
  1297  				}
  1298  				i++
  1299  			}
  1300  			if _, err := dec.ReadToken(); err != nil {
  1301  				return err
  1302  			}
  1303  			if i < n {
  1304  				err := errors.New("too few array elements")
  1305  				return &SemanticError{action: "unmarshal", GoType: t, Err: err}
  1306  			}
  1307  			return nil
  1308  		}
  1309  		return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t}
  1310  	}
  1311  	return &fncs
  1312  }
  1313  
  1314  func makePointerArshaler(t reflect.Type) *arshaler {
  1315  	var fncs arshaler
  1316  	var (
  1317  		once    sync.Once
  1318  		valFncs *arshaler
  1319  	)
  1320  	init := func() {
  1321  		valFncs = lookupArshaler(t.Elem())
  1322  	}
  1323  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
  1324  		// Check for cycles.
  1325  		if enc.tokens.depth() > startDetectingCyclesAfter {
  1326  			if err := enc.seenPointers.visit(va.Value); err != nil {
  1327  				return err
  1328  			}
  1329  			defer enc.seenPointers.leave(va.Value)
  1330  		}
  1331  
  1332  		// NOTE: MarshalOptions.format is forwarded to underlying marshal.
  1333  		if va.IsNil() {
  1334  			return enc.WriteToken(Null)
  1335  		}
  1336  		once.Do(init)
  1337  		marshal := valFncs.marshal
  1338  		if mo.Marshalers != nil {
  1339  			marshal, _ = mo.Marshalers.lookup(marshal, t.Elem())
  1340  		}
  1341  		v := addressableValue{va.Elem()} // dereferenced pointer is always addressable
  1342  		return marshal(mo, enc, v)
  1343  	}
  1344  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
  1345  		// NOTE: UnmarshalOptions.format is forwarded to underlying unmarshal.
  1346  		if dec.PeekKind() == 'n' {
  1347  			if _, err := dec.ReadToken(); err != nil {
  1348  				return err
  1349  			}
  1350  			va.Set(reflect.Zero(t))
  1351  			return nil
  1352  		}
  1353  		once.Do(init)
  1354  		unmarshal := valFncs.unmarshal
  1355  		if uo.Unmarshalers != nil {
  1356  			unmarshal, _ = uo.Unmarshalers.lookup(unmarshal, t.Elem())
  1357  		}
  1358  		if va.IsNil() {
  1359  			va.Set(reflect.New(t.Elem()))
  1360  		}
  1361  		v := addressableValue{va.Elem()} // dereferenced pointer is always addressable
  1362  		return unmarshal(uo, dec, v)
  1363  	}
  1364  	return &fncs
  1365  }
  1366  
  1367  func makeInterfaceArshaler(t reflect.Type) *arshaler {
  1368  	// NOTE: Values retrieved from an interface are not addressable,
  1369  	// so we shallow copy the values to make them addressable and
  1370  	// store them back into the interface afterwards.
  1371  
  1372  	var fncs arshaler
  1373  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
  1374  		if mo.format != "" && mo.formatDepth == enc.tokens.depth() {
  1375  			return newInvalidFormatError("marshal", t, mo.format)
  1376  		}
  1377  		if va.IsNil() {
  1378  			return enc.WriteToken(Null)
  1379  		}
  1380  		v := newAddressableValue(va.Elem().Type())
  1381  		v.Set(va.Elem())
  1382  		marshal := lookupArshaler(v.Type()).marshal
  1383  		if mo.Marshalers != nil {
  1384  			marshal, _ = mo.Marshalers.lookup(marshal, v.Type())
  1385  		}
  1386  		// Optimize for the any type if there are no special options.
  1387  		if optimizeCommon && t == anyType && !mo.StringifyNumbers && mo.format == "" && (mo.Marshalers == nil || !mo.Marshalers.fromAny) {
  1388  			return marshalValueAny(mo, enc, va.Elem().Interface())
  1389  		}
  1390  		return marshal(mo, enc, v)
  1391  	}
  1392  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
  1393  		if uo.format != "" && uo.formatDepth == dec.tokens.depth() {
  1394  			return newInvalidFormatError("unmarshal", t, uo.format)
  1395  		}
  1396  		if dec.PeekKind() == 'n' {
  1397  			if _, err := dec.ReadToken(); err != nil {
  1398  				return err
  1399  			}
  1400  			va.Set(reflect.Zero(t))
  1401  			return nil
  1402  		}
  1403  		var v addressableValue
  1404  		if va.IsNil() {
  1405  			// Optimize for the any type if there are no special options.
  1406  			// We do not care about stringified numbers since JSON strings
  1407  			// are always unmarshaled into an any value as Go strings.
  1408  			// Duplicate name check must be enforced since unmarshalValueAny
  1409  			// does not implement merge semantics.
  1410  			if optimizeCommon && t == anyType && uo.format == "" && (uo.Unmarshalers == nil || !uo.Unmarshalers.fromAny) && !dec.options.AllowDuplicateNames {
  1411  				v, err := unmarshalValueAny(uo, dec)
  1412  				// We must check for nil interface values up front.
  1413  				// See https://go.dev/issue/52310.
  1414  				if v != nil {
  1415  					va.Set(reflect.ValueOf(v))
  1416  				}
  1417  				return err
  1418  			}
  1419  
  1420  			k := dec.PeekKind()
  1421  			if !isAnyType(t) {
  1422  				err := errors.New("cannot derive concrete type for non-empty interface")
  1423  				return &SemanticError{action: "unmarshal", JSONKind: k, GoType: t, Err: err}
  1424  			}
  1425  			switch k {
  1426  			case 'f', 't':
  1427  				v = newAddressableValue(boolType)
  1428  			case '"':
  1429  				v = newAddressableValue(stringType)
  1430  			case '0':
  1431  				v = newAddressableValue(float64Type)
  1432  			case '{':
  1433  				v = newAddressableValue(mapStringAnyType)
  1434  			case '[':
  1435  				v = newAddressableValue(sliceAnyType)
  1436  			default:
  1437  				// If k is invalid (e.g., due to an I/O or syntax error), then
  1438  				// that will be cached by PeekKind and returned by ReadValue.
  1439  				// If k is '}' or ']', then ReadValue must error since
  1440  				// those are invalid kinds at the start of a JSON value.
  1441  				_, err := dec.ReadValue()
  1442  				return err
  1443  			}
  1444  		} else {
  1445  			// Shallow copy the existing value to keep it addressable.
  1446  			// Any mutations at the top-level of the value will be observable
  1447  			// since we always store this value back into the interface value.
  1448  			v = newAddressableValue(va.Elem().Type())
  1449  			v.Set(va.Elem())
  1450  		}
  1451  		unmarshal := lookupArshaler(v.Type()).unmarshal
  1452  		if uo.Unmarshalers != nil {
  1453  			unmarshal, _ = uo.Unmarshalers.lookup(unmarshal, v.Type())
  1454  		}
  1455  		err := unmarshal(uo, dec, v)
  1456  		va.Set(v.Value)
  1457  		return err
  1458  	}
  1459  	return &fncs
  1460  }
  1461  
  1462  // isAnyType reports wether t is equivalent to the any interface type.
  1463  func isAnyType(t reflect.Type) bool {
  1464  	// This is forward compatible if the Go language permits type sets within
  1465  	// ordinary interfaces where an interface with zero methods does not
  1466  	// necessarily mean it can hold every possible Go type.
  1467  	// See https://go.dev/issue/45346.
  1468  	return t == anyType || anyType.Implements(t)
  1469  }
  1470  
  1471  func makeInvalidArshaler(t reflect.Type) *arshaler {
  1472  	var fncs arshaler
  1473  	fncs.marshal = func(mo MarshalOptions, enc *Encoder, va addressableValue) error {
  1474  		return &SemanticError{action: "marshal", GoType: t}
  1475  	}
  1476  	fncs.unmarshal = func(uo UnmarshalOptions, dec *Decoder, va addressableValue) error {
  1477  		return &SemanticError{action: "unmarshal", GoType: t}
  1478  	}
  1479  	return &fncs
  1480  }
  1481  
  1482  func newInvalidFormatError(action string, t reflect.Type, format string) error {
  1483  	err := fmt.Errorf("invalid format flag: %q", format)
  1484  	return &SemanticError{action: action, GoType: t, Err: err}
  1485  }