| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
stdjson "encoding/json" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"math" |
| 9 |
"net/netip" |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
json "github.com/goccy/go-json" |
| 15 |
) |
| 16 |
|
| 17 |
type ifaceKeyName string |
| 18 |
|
| 19 |
type ifaceKeyInt int16 |
| 20 |
|
| 21 |
type ifaceKeyText struct{ v string } |
| 22 |
|
| 23 |
func (t ifaceKeyText) MarshalText() ([]byte, error) { return []byte("text:" + t.v), nil } |
| 24 |
|
| 25 |
type ifaceKeyPtrText struct{ v string } |
| 26 |
|
| 27 |
func (t *ifaceKeyPtrText) MarshalText() ([]byte, error) { |
| 28 |
if t == nil { |
| 29 |
return []byte("nil"), nil |
| 30 |
} |
| 31 |
return []byte("ptr:" + t.v), nil |
| 32 |
} |
| 33 |
|
| 34 |
type ifaceKeyTextError struct{} |
| 35 |
|
| 36 |
func (ifaceKeyTextError) MarshalText() ([]byte, error) { return nil, fmt.Errorf("no text") } |
| 37 |
|
| 38 |
type ifaceKeyJSON struct{} |
| 39 |
|
| 40 |
func (ifaceKeyJSON) MarshalJSON() ([]byte, error) { return []byte(`"json"`), nil } |
| 41 |
|
| 42 |
type ifaceKeyNamedText string |
| 43 |
|
| 44 |
func (ifaceKeyNamedText) MarshalText() ([]byte, error) { return []byte("named-text"), nil } |
| 45 |
|
| 46 |
type ifaceKeyEscapedText string |
| 47 |
|
| 48 |
func (t ifaceKeyEscapedText) MarshalText() ([]byte, error) { return []byte(t), nil } |
| 49 |
|
| 50 |
type ifaceKeyStringer interface{ String() string } |
| 51 |
|
| 52 |
type ifaceKeyStringerValue string |
| 53 |
|
| 54 |
func (s ifaceKeyStringerValue) String() string { return string(s) } |
| 55 |
|
| 56 |
// ifaceKeyErrorKind is the kind of an error, which the errors of go-json and of encoding/json are compared by. |
| 57 |
func ifaceKeyErrorKind(err error, std bool) string { |
| 58 |
switch { |
| 59 |
case err == nil: |
| 60 |
return "" |
| 61 |
case std && errors.As(err, new(*stdjson.UnsupportedTypeError)), !std && errors.As(err, new(*json.UnsupportedTypeError)): |
| 62 |
return "unsupported type" |
| 63 |
case std && errors.As(err, new(*stdjson.UnsupportedValueError)), !std && errors.As(err, new(*json.UnsupportedValueError)): |
| 64 |
return "unsupported value" |
| 65 |
case std && errors.As(err, new(*stdjson.MarshalerError)), !std && errors.As(err, new(*json.MarshalerError)): |
| 66 |
return "marshaler" |
| 67 |
} |
| 68 |
return fmt.Sprintf("%T", err) |
| 69 |
} |
| 70 |
|
| 71 |
func TestEncodeMapInterfaceKeys(t *testing.T) { |
| 72 |
// A map whose keys are of an interface type is encoded as encoding/json of the Go it is built with encodes |
| 73 |
// it: refused, or, where encoding/json is built on encoding/json/v2, with the names of the dynamic values of |
| 74 |
// its keys, whatever they hold, by Marshal, MarshalIndent and an Encoder. |
| 75 |
var nilKey any |
| 76 |
var nilPtr *string |
| 77 |
var nilText *ifaceKeyPtrText |
| 78 |
s := "pointed" |
| 79 |
ps := &s |
| 80 |
cases := []struct { |
| 81 |
name string |
| 82 |
value any |
| 83 |
}{ |
| 84 |
{"string", map[any]any{"x": 1}}, |
| 85 |
{"invalid UTF-8 string", map[any]any{"a\xffb": 1, "\xe3\x81": 2, "<\u2028": 3}}, |
| 86 |
{"named string", map[any]any{ifaceKeyName("n"): 1}}, |
| 87 |
{"int", map[any]any{1: "a"}}, |
| 88 |
{"int kinds", map[any]any{int8(-8): 1, int16(-16): 2, int32(-32): 3, int64(math.MinInt64): 4, ifaceKeyInt(5): 5}}, |
| 89 |
{"uint kinds", map[any]any{uint(1): 1, uint8(8): 2, uint16(16): 3, uint32(32): 4, uint64(math.MaxUint64): 5, uintptr(9): 6}}, |
| 90 |
{"float32", map[any]any{float32(1.5): 1, float32(2): 2, float32(1e21): 3, float32(1e-7): 4}}, |
| 91 |
{"float64", map[any]any{1.5: 1}}, |
| 92 |
{"float64 nan", map[any]any{math.NaN(): 1}}, |
| 93 |
{"bool", map[any]any{true: 1}}, |
| 94 |
{"nil", map[any]any{nilKey: 1}}, |
| 95 |
{"struct", map[any]any{struct{}{}: 1}}, |
| 96 |
{"array", map[any]any{[2]int{1, 2}: 1}}, |
| 97 |
{"complex", map[any]any{complex(1, 2): 1}}, |
| 98 |
{"pointer", map[any]any{ps: 1}}, |
| 99 |
{"pointer to pointer", map[any]any{&ps: 1}}, |
| 100 |
{"nil pointer", map[any]any{nilPtr: 1}}, |
| 101 |
{"text marshaler", map[any]any{ifaceKeyText{"a"}: 1}}, |
| 102 |
{"pointer text marshaler", map[any]any{&ifaceKeyPtrText{"b"}: 1}}, |
| 103 |
{"value of pointer text marshaler", map[any]any{ifaceKeyPtrText{"c"}: 1}}, |
| 104 |
{"nil pointer text marshaler", map[any]any{nilText: 1}}, |
| 105 |
{"text marshaler error", map[any]any{ifaceKeyTextError{}: 1}}, |
| 106 |
{"json marshaler", map[any]any{ifaceKeyJSON{}: 1}}, |
| 107 |
{"named text marshaler", map[any]any{ifaceKeyNamedText("x"): 1}}, |
| 108 |
{"time", map[any]any{time.Date(2026, 9, 25, 12, 0, 0, 5, time.UTC): 1}}, |
| 109 |
{"netip", map[any]any{netip.MustParseAddr("192.0.2.1"): 1}}, |
| 110 |
{"sorted", map[any]any{"b": 1, 10: 2, "a": 3, 2: 4, ifaceKeyName("c"): 5, -1: 6, float32(0.5): 7}}, |
| 111 |
{"escaped", map[any]any{"<&>": 1, "\u2028": 2, "a\"b": 3, "\n": 4, "\u00e9": 5, "\\": 6}}, |
| 112 |
{"values", map[any]string{"a": "x", 2: "y"}}, |
| 113 |
{"struct values", map[any]struct{ A int }{"a": {1}, 2: {2}}}, |
| 114 |
{"nested", map[string]any{"m": map[any]any{"x": map[any]any{3: []any{true, nil}}}}}, |
| 115 |
{"in a struct", struct { |
| 116 |
M map[any]int `json:"m"` |
| 117 |
N int |
| 118 |
}{M: map[any]int{"k": 1}, N: 2}}, |
| 119 |
{"empty", map[any]any{}}, |
| 120 |
{"nil map", map[any]any(nil)}, |
| 121 |
{"text marshaler keys with escapes", map[ifaceKeyEscapedText]int{"<&>": 1, "\u2028": 2, "a\"b": 3, "\t": 4, "b": 5}}, |
| 122 |
{"stringer keys", map[ifaceKeyStringer]int{ifaceKeyStringerValue("s"): 1}}, |
| 123 |
{"stringer float64 keys", map[fmt.Stringer]int{time.Duration(3): 1}}, |
| 124 |
{"text marshaler keys", map[interface{ MarshalText() ([]byte, error) }]int{ifaceKeyText{"t"}: 1, &ifaceKeyPtrText{"p"}: 2}}, |
| 125 |
} |
| 126 |
for _, tc := range cases { |
| 127 |
t.Run(tc.name, func(t *testing.T) { |
| 128 |
want, wantErr := stdjson.Marshal(tc.value) |
| 129 |
got, gotErr := json.Marshal(tc.value) |
| 130 |
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) { |
| 131 |
t.Fatalf("Marshal: got the error %v, want %v", gotErr, wantErr) |
| 132 |
} |
| 133 |
if wantErr == nil && !bytes.Equal(got, want) { |
| 134 |
t.Fatalf("Marshal: got %s, want %s", got, want) |
| 135 |
} |
| 136 |
|
| 137 |
want, wantErr = stdjson.MarshalIndent(tc.value, ">", " ") |
| 138 |
got, gotErr = json.MarshalIndent(tc.value, ">", " ") |
| 139 |
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) { |
| 140 |
t.Fatalf("MarshalIndent: got the error %v, want %v", gotErr, wantErr) |
| 141 |
} |
| 142 |
if wantErr == nil && !bytes.Equal(got, want) { |
| 143 |
t.Fatalf("MarshalIndent: got %s, want %s", got, want) |
| 144 |
} |
| 145 |
|
| 146 |
// the VMs of the colors and of the options: the same entries, in no order when they are not sorted. |
| 147 |
for _, opts := range [][]json.EncodeOptionFunc{ |
| 148 |
{json.Colorize(json.DefaultColorScheme)}, |
| 149 |
{json.UnorderedMap()}, |
| 150 |
} { |
| 151 |
_, gotErr := json.MarshalWithOption(tc.value, opts...) |
| 152 |
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) { |
| 153 |
t.Fatalf("MarshalWithOption: got the error %v, want %v", gotErr, wantErr) |
| 154 |
} |
| 155 |
if wantErr != nil || len(opts) == 0 { |
| 156 |
continue |
| 157 |
} |
| 158 |
if _, err := json.MarshalIndentWithOption(tc.value, "", " ", opts...); err != nil { |
| 159 |
t.Fatalf("MarshalIndentWithOption: %v", err) |
| 160 |
} |
| 161 |
} |
| 162 |
if wantErr == nil { |
| 163 |
want, _ := stdjson.Marshal(tc.value) |
| 164 |
got, err := json.MarshalWithOption(tc.value, json.UnorderedMap()) |
| 165 |
if err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
var wantValue, gotValue any |
| 169 |
if err := stdjson.Unmarshal(want, &wantValue); err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
if err := stdjson.Unmarshal(got, &gotValue); err != nil { |
| 173 |
t.Fatalf("UnorderedMap: %s: %v", got, err) |
| 174 |
} |
| 175 |
if !reflect.DeepEqual(gotValue, wantValue) { |
| 176 |
t.Fatalf("UnorderedMap: got %s, want %s", got, want) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
var wantBuf, gotBuf bytes.Buffer |
| 181 |
wantEnc, gotEnc := stdjson.NewEncoder(&wantBuf), json.NewEncoder(&gotBuf) |
| 182 |
wantEnc.SetEscapeHTML(false) |
| 183 |
gotEnc.SetEscapeHTML(false) |
| 184 |
wantErr, gotErr = wantEnc.Encode(tc.value), gotEnc.Encode(tc.value) |
| 185 |
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) { |
| 186 |
t.Fatalf("Encoder: got the error %v, want %v", gotErr, wantErr) |
| 187 |
} |
| 188 |
if wantErr == nil && !bytes.Equal(gotBuf.Bytes(), wantBuf.Bytes()) { |
| 189 |
t.Fatalf("Encoder: got %s, want %s", gotBuf.Bytes(), wantBuf.Bytes()) |
| 190 |
} |
| 191 |
}) |
| 192 |
} |
| 193 |
} |