| 1 |
//go:build go1.27 && goexperiment.jsonv2 |
| 2 |
|
| 3 |
package encoder |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding" |
| 7 |
jsonv1 "encoding/json" |
| 8 |
jsonv2 "encoding/json/v2" |
| 9 |
"math" |
| 10 |
"reflect" |
| 11 |
"strconv" |
| 12 |
"unsafe" |
| 13 |
|
| 14 |
"github.com/goccy/go-json/internal/errors" |
| 15 |
"github.com/goccy/go-json/internal/runtime" |
| 16 |
) |
| 17 |
|
| 18 |
// The keys of a map of an interface type, which encoding/json encodes when it is built on encoding/json/v2 |
| 19 |
// ( GOEXPERIMENT=jsonv2, the default since Go 1.27; this file uses the API of Go 1.27 ): the name of a key is |
| 20 |
// made from its dynamic value as a value in the place of a name is, and a key whose value is not written as a |
| 21 |
// string is an error. |
| 22 |
// |
| 23 |
// - a string, an integer, a float32 and a float64 are their text; a float64 held by a key of interface{} |
| 24 |
// is not, as interface{} writes it as a number. |
| 25 |
// - a value with AppendText or MarshalText of its value, or of its pointer through a pointer, is its text; |
| 26 |
// MarshalJSON is not called for a name. |
| 27 |
// - a pointer is the name of its value. |
| 28 |
// - nil, a bool, a nil pointer, a struct, an array, a slice or a map is not a name; a complex number, a |
| 29 |
// channel or a function is an unsupported type. |
| 30 |
// |
| 31 |
// The keys are sorted by their names, as the keys of the maps whose keys are not strings are ( see OpMapEnd ). |
| 32 |
|
| 33 |
// interfaceMapKeys is whether a map of an interface type as its key is encoded. |
| 34 |
const interfaceMapKeys = true |
| 35 |
|
| 36 |
var ( |
| 37 |
emptyInterfaceType = reflect.TypeOf((*any)(nil)).Elem() |
| 38 |
float64Type = reflect.TypeOf(float64(0)) |
| 39 |
textAppenderType = reflect.TypeOf((*encoding.TextAppender)(nil)).Elem() |
| 40 |
jsonMarshalerType = reflect.TypeOf((*jsonv1.Marshaler)(nil)).Elem() |
| 41 |
jsonMarshalerToType = reflect.TypeOf((*jsonv2.MarshalerTo)(nil)).Elem() |
| 42 |
) |
| 43 |
|
| 44 |
// appendInterfaceMapKey appends the name of the key of a map of an interface type as a string. p is the address |
| 45 |
// of the key as the map is read into the context: an interface{}, whatever the interface type of the key is |
| 46 |
// ( see newReflectCollector ). |
| 47 |
func appendInterfaceMapKey(ctx *RuntimeContext, code *Opcode, b []byte, p unsafe.Pointer) ([]byte, error) { |
| 48 |
keyType := runtime.TypeOfPtr(code.Type) |
| 49 |
name, err := interfaceMapKeyName(keyType, reflect.NewAt(emptyInterfaceType, p).Elem()) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return appendText(ctx, b, name), nil |
| 54 |
} |
| 55 |
|
| 56 |
// interfaceMapKeyName returns the name of the key, which is a value of the interface type keyType. |
| 57 |
func interfaceMapKeyName(keyType reflect.Type, key reflect.Value) (string, error) { |
| 58 |
// A key of an interface type with methods is set to the interface{} as the value of its interface type, |
| 59 |
// which holds the dynamic value. |
| 60 |
v := key |
| 61 |
for v.Kind() == reflect.Interface { |
| 62 |
if v.IsNil() { |
| 63 |
return "", notStringMapKey(v) |
| 64 |
} |
| 65 |
v = v.Elem() |
| 66 |
} |
| 67 |
if v.Kind() == reflect.Pointer && v.IsNil() { |
| 68 |
// The method of the marshaler which the interface type of the key is, if any, is called with the nil |
| 69 |
// pointer. A MarshalJSON is not called for a name. |
| 70 |
switch { |
| 71 |
case keyType.Implements(jsonMarshalerToType), keyType.Implements(jsonMarshalerType): |
| 72 |
return "", notStringMapKey(v) |
| 73 |
case keyType.Implements(textAppenderType): |
| 74 |
return appendTextName(v.Interface().(encoding.TextAppender)) |
| 75 |
case keyType.Implements(marshalTextType): |
| 76 |
return marshalTextName(v.Interface().(encoding.TextMarshaler)) |
| 77 |
} |
| 78 |
return "", notStringMapKey(v) |
| 79 |
} |
| 80 |
if keyType == emptyInterfaceType && v.Type() == float64Type { |
| 81 |
// interface{} writes a float64 as a number, which is not a name. |
| 82 |
if f := v.Float(); math.IsNaN(f) || math.IsInf(f, 0) { |
| 83 |
return "", nonFiniteMapKey(v, f) |
| 84 |
} |
| 85 |
return "", notStringMapKey(v) |
| 86 |
} |
| 87 |
return mapKeyNameOf(v, false) |
| 88 |
} |
| 89 |
|
| 90 |
// mapKeyNameOf returns the name of the value of a key. byPointer is whether the value is the one of a pointer, |
| 91 |
// whose methods on the pointer are called. |
| 92 |
func mapKeyNameOf(v reflect.Value, byPointer bool) (string, error) { |
| 93 |
t := v.Type() |
| 94 |
if t.Kind() != reflect.Pointer { |
| 95 |
switch { |
| 96 |
case t.Implements(textAppenderType): |
| 97 |
return appendTextName(v.Interface().(encoding.TextAppender)) |
| 98 |
case byPointer && reflect.PointerTo(t).Implements(textAppenderType): |
| 99 |
return appendTextName(v.Addr().Interface().(encoding.TextAppender)) |
| 100 |
case t.Implements(marshalTextType): |
| 101 |
return marshalTextName(v.Interface().(encoding.TextMarshaler)) |
| 102 |
case byPointer && reflect.PointerTo(t).Implements(marshalTextType): |
| 103 |
return marshalTextName(v.Addr().Interface().(encoding.TextMarshaler)) |
| 104 |
} |
| 105 |
} |
| 106 |
switch t.Kind() { |
| 107 |
case reflect.String: |
| 108 |
return v.String(), nil |
| 109 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 110 |
return strconv.FormatInt(v.Int(), 10), nil |
| 111 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 112 |
return strconv.FormatUint(v.Uint(), 10), nil |
| 113 |
case reflect.Float32, reflect.Float64: |
| 114 |
f := v.Float() |
| 115 |
if math.IsNaN(f) || math.IsInf(f, 0) { |
| 116 |
return "", nonFiniteMapKey(v, f) |
| 117 |
} |
| 118 |
bits := 64 |
| 119 |
if t.Kind() == reflect.Float32 { |
| 120 |
bits = 32 |
| 121 |
} |
| 122 |
return string(appendFloatOfBits(nil, f, bits)), nil |
| 123 |
case reflect.Pointer: |
| 124 |
if v.IsNil() { |
| 125 |
return "", notStringMapKey(v) |
| 126 |
} |
| 127 |
return mapKeyNameOf(v.Elem(), true) |
| 128 |
case reflect.Complex64, reflect.Complex128, reflect.Chan, reflect.Func, reflect.UnsafePointer: |
| 129 |
return "", &errors.UnsupportedTypeError{Type: t} |
| 130 |
} |
| 131 |
return "", notStringMapKey(v) |
| 132 |
} |
| 133 |
|
| 134 |
// appendFloatOfBits appends the float as AppendFloat32 or AppendFloat64 does. |
| 135 |
func appendFloatOfBits(b []byte, f float64, bits int) []byte { |
| 136 |
if bits == 32 { |
| 137 |
return AppendFloat32(nil, b, float32(f)) |
| 138 |
} |
| 139 |
return AppendFloat64(nil, b, f) |
| 140 |
} |
| 141 |
|
| 142 |
func appendTextName(m encoding.TextAppender) (string, error) { |
| 143 |
b, err := m.AppendText(nil) |
| 144 |
if err != nil { |
| 145 |
return "", errors.ErrMarshaler(reflect.TypeOf(m), err, "AppendText") |
| 146 |
} |
| 147 |
return string(b), nil |
| 148 |
} |
| 149 |
|
| 150 |
func marshalTextName(m encoding.TextMarshaler) (string, error) { |
| 151 |
b, err := m.MarshalText() |
| 152 |
if err != nil { |
| 153 |
return "", errors.ErrMarshaler(reflect.TypeOf(m), err, "MarshalText") |
| 154 |
} |
| 155 |
return string(b), nil |
| 156 |
} |
| 157 |
|
| 158 |
// notStringMapKey is the error of a key whose value is not written as a string, as encoding/json reports it. |
| 159 |
func notStringMapKey(v reflect.Value) error { |
| 160 |
return &errors.UnsupportedValueError{Value: v, Str: "object member name must be a string"} |
| 161 |
} |
| 162 |
|
| 163 |
// nonFiniteMapKey is the error of a key of a float which is not finite, as encoding/json reports it. |
| 164 |
func nonFiniteMapKey(v reflect.Value, f float64) error { |
| 165 |
return &errors.UnsupportedValueError{Value: v, Str: strconv.FormatFloat(f, 'g', -1, 64)} |
| 166 |
} |