diff --git a/json/codec.go b/json/codec.go index 32c078f..4774dcb 100644 --- a/json/codec.go +++ b/json/codec.go @@ -4,6 +4,7 @@ import ( "encoding" "encoding/json" "fmt" + "maps" "math/big" "reflect" "sort" @@ -73,12 +74,9 @@ func cacheLoad() map[unsafe.Pointer]codec { func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec) { newCodecs := make(map[unsafe.Pointer]codec, len(oldCodecs)+1) + maps.Copy(newCodecs, oldCodecs) newCodecs[typeid(typ)] = cod - for t, c := range oldCodecs { - newCodecs[t] = c - } - cache.Store(&newCodecs) } @@ -205,7 +203,7 @@ func constructCodec(t reflect.Type, seen map[reflect.Type]*structType, canAddr b c = constructUnsupportedTypeCodec(t) } - p := reflect.PtrTo(t) + p := reflect.PointerTo(t) if canAddr { switch { @@ -291,7 +289,7 @@ func constructSliceCodec(t reflect.Type, seen map[reflect.Type]*structType) code // Go 1.7+ behavior: slices of byte types (and aliases) may override the // default encoding and decoding behaviors by implementing marshaler and // unmarshaler interfaces. - p := reflect.PtrTo(e) + p := reflect.PointerTo(e) c := codec{} switch { @@ -391,7 +389,7 @@ func constructMapCodec(t reflect.Type, seen map[reflect.Type]*structType) codec kc := codec{} vc := constructCodec(v, seen, false) - if k.Implements(textMarshalerType) || reflect.PtrTo(k).Implements(textUnmarshalerType) { + if k.Implements(textMarshalerType) || reflect.PointerTo(k).Implements(textUnmarshalerType) { kc.encode = constructTextMarshalerEncodeFunc(k, false) kc.decode = constructTextUnmarshalerDecodeFunc(k, true) @@ -1068,51 +1066,51 @@ type sliceHeader struct { var ( nullType = reflect.TypeOf(nil) - boolType = reflect.TypeOf(false) - - intType = reflect.TypeOf(int(0)) - int8Type = reflect.TypeOf(int8(0)) - int16Type = reflect.TypeOf(int16(0)) - int32Type = reflect.TypeOf(int32(0)) - int64Type = reflect.TypeOf(int64(0)) - - uintType = reflect.TypeOf(uint(0)) - uint8Type = reflect.TypeOf(uint8(0)) - uint16Type = reflect.TypeOf(uint16(0)) - uint32Type = reflect.TypeOf(uint32(0)) - uint64Type = reflect.TypeOf(uint64(0)) - uintptrType = reflect.TypeOf(uintptr(0)) - - float32Type = reflect.TypeOf(float32(0)) - float64Type = reflect.TypeOf(float64(0)) - - bigIntType = reflect.TypeOf(new(big.Int)) - numberType = reflect.TypeOf(json.Number("")) - stringType = reflect.TypeOf("") - stringsType = reflect.TypeOf([]string(nil)) - bytesType = reflect.TypeOf(([]byte)(nil)) - durationType = reflect.TypeOf(time.Duration(0)) - timeType = reflect.TypeOf(time.Time{}) - rawMessageType = reflect.TypeOf(RawMessage(nil)) - - numberPtrType = reflect.PtrTo(numberType) - durationPtrType = reflect.PtrTo(durationType) - timePtrType = reflect.PtrTo(timeType) - rawMessagePtrType = reflect.PtrTo(rawMessageType) - - sliceInterfaceType = reflect.TypeOf(([]any)(nil)) - sliceStringType = reflect.TypeOf(([]any)(nil)) - mapStringInterfaceType = reflect.TypeOf((map[string]any)(nil)) - mapStringRawMessageType = reflect.TypeOf((map[string]RawMessage)(nil)) - mapStringStringType = reflect.TypeOf((map[string]string)(nil)) - mapStringStringSliceType = reflect.TypeOf((map[string][]string)(nil)) - mapStringBoolType = reflect.TypeOf((map[string]bool)(nil)) - - interfaceType = reflect.TypeOf((*any)(nil)).Elem() - jsonMarshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() - jsonUnmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() - textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() - textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() + boolType = reflect.TypeFor[bool]() + + intType = reflect.TypeFor[int]() + int8Type = reflect.TypeFor[int8]() + int16Type = reflect.TypeFor[int16]() + int32Type = reflect.TypeFor[int32]() + int64Type = reflect.TypeFor[int64]() + + uintType = reflect.TypeFor[uint]() + uint8Type = reflect.TypeFor[uint8]() + uint16Type = reflect.TypeFor[uint16]() + uint32Type = reflect.TypeFor[uint32]() + uint64Type = reflect.TypeFor[uint64]() + uintptrType = reflect.TypeFor[uintptr]() + + float32Type = reflect.TypeFor[float32]() + float64Type = reflect.TypeFor[float64]() + + bigIntType = reflect.TypeFor[*big.Int]() + numberType = reflect.TypeFor[json.Number]() + stringType = reflect.TypeFor[string]() + stringsType = reflect.TypeFor[[]string]() + bytesType = reflect.TypeFor[[]byte]() + durationType = reflect.TypeFor[time.Duration]() + timeType = reflect.TypeFor[time.Time]() + rawMessageType = reflect.TypeFor[RawMessage]() + + numberPtrType = reflect.PointerTo(numberType) + durationPtrType = reflect.PointerTo(durationType) + timePtrType = reflect.PointerTo(timeType) + rawMessagePtrType = reflect.PointerTo(rawMessageType) + + sliceInterfaceType = reflect.TypeFor[[]any]() + sliceStringType = reflect.TypeFor[[]any]() + mapStringInterfaceType = reflect.TypeFor[map[string]any]() + mapStringRawMessageType = reflect.TypeFor[map[string]RawMessage]() + mapStringStringType = reflect.TypeFor[map[string]string]() + mapStringStringSliceType = reflect.TypeFor[map[string][]string]() + mapStringBoolType = reflect.TypeFor[map[string]bool]() + + interfaceType = reflect.TypeFor[any]() + jsonMarshalerType = reflect.TypeFor[Marshaler]() + jsonUnmarshalerType = reflect.TypeFor[Unmarshaler]() + textMarshalerType = reflect.TypeFor[encoding.TextMarshaler]() + textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]() bigIntDecoder = constructJSONUnmarshalerDecodeFunc(bigIntType, false) )