| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"reflect" |
| 6 |
"regexp" |
| 7 |
"strconv" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/goccy/go-json" |
| 11 |
) |
| 12 |
|
| 13 |
// An unordered map with keys of a string kind is written as it is read when its values are scalars, or values |
| 14 |
// of interface{}: then the entries which hold a scalar come first, and the others after them by their opcodes. |
| 15 |
// Every kind of a value, and every option which changes how a value is written, must give the same object as |
| 16 |
// encoding/json, in any order. |
| 17 |
|
| 18 |
// the colors are escape sequences of the terminal around the tokens. |
| 19 |
var ansiEscapes = regexp.MustCompile(`\x1b\[[0-9;]*m`) |
| 20 |
|
| 21 |
type unorderedMapMarshaler struct{ V int } |
| 22 |
|
| 23 |
func (m unorderedMapMarshaler) MarshalJSON() ([]byte, error) { return []byte(`{"m":1}`), nil } |
| 24 |
|
| 25 |
type unorderedMapKey string |
| 26 |
|
| 27 |
var unorderedMapEncoders = []struct { |
| 28 |
name string |
| 29 |
encode func(v any) ([]byte, error) |
| 30 |
}{ |
| 31 |
{"unordered", func(v any) ([]byte, error) { return json.MarshalWithOption(v, json.UnorderedMap()) }}, |
| 32 |
{"unordered no escape", func(v any) ([]byte, error) { |
| 33 |
return json.MarshalWithOption(v, json.UnorderedMap(), json.DisableHTMLEscape(), json.DisableNormalizeUTF8()) |
| 34 |
}}, |
| 35 |
{"unordered indent", func(v any) ([]byte, error) { |
| 36 |
return json.MarshalIndentWithOption(v, "", " ", json.UnorderedMap()) |
| 37 |
}}, |
| 38 |
{"unordered colorized", func(v any) ([]byte, error) { |
| 39 |
return json.MarshalWithOption(v, json.UnorderedMap(), json.Colorize(json.DefaultColorScheme)) |
| 40 |
}}, |
| 41 |
{"unordered colorized indent", func(v any) ([]byte, error) { |
| 42 |
return json.MarshalIndentWithOption(v, "", " ", json.UnorderedMap(), json.Colorize(json.DefaultColorScheme)) |
| 43 |
}}, |
| 44 |
} |
| 45 |
|
| 46 |
// assertUnorderedMapsEqual encodes each value by every encoder of an unordered map and compares the object |
| 47 |
// with the one of encoding/json. |
| 48 |
func assertUnorderedMapsEqual(t *testing.T, values []any) { |
| 49 |
t.Helper() |
| 50 |
for _, value := range values { |
| 51 |
expected, err := stdjson.Marshal(value) |
| 52 |
if err != nil { |
| 53 |
t.Fatal(err) |
| 54 |
} |
| 55 |
var want any |
| 56 |
if err := stdjson.Unmarshal(expected, &want); err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
for _, enc := range unorderedMapEncoders { |
| 60 |
b, err := enc.encode(value) |
| 61 |
if err != nil { |
| 62 |
t.Fatalf("%s: %T: %v", enc.name, value, err) |
| 63 |
} |
| 64 |
var got any |
| 65 |
if err := stdjson.Unmarshal(ansiEscapes.ReplaceAll(b, nil), &got); err != nil { |
| 66 |
t.Fatalf("%s: %T: %v\n%s", enc.name, value, err, b) |
| 67 |
} |
| 68 |
if !reflect.DeepEqual(got, want) { |
| 69 |
t.Errorf("%s: %T:\n got %.300s\nwant %.300s", enc.name, value, b, expected) |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestEncodeUnorderedMapOfInterfaces(t *testing.T) { |
| 76 |
var nilPtr *int |
| 77 |
one := 1 |
| 78 |
scalars := map[string]any{ |
| 79 |
"int": 1, "int8": int8(-2), "uint": uint(3), "float": 1.5, "float32": float32(2.5), "string": "s<>&
", |
| 80 |
"bool": true, "nil": nil, "number": stdjson.Number("12.5"), "bytes": []byte("bytes"), |
| 81 |
} |
| 82 |
others := map[string]any{ |
| 83 |
"map": map[string]any{"a": 1, "b": map[string]any{"c": nil}}, "slice": []any{1, "s", nil}, |
| 84 |
"struct": struct{ A int }{1}, "ptr": &one, "nilptr": nilPtr, "marshaler": unorderedMapMarshaler{1}, |
| 85 |
"ptrmarshaler": &unorderedMapMarshaler{2}, "ints": []int{1, 2}, "strings": map[string]string{"k": "v"}, |
| 86 |
"empty": map[string]any{}, "emptyslice": []any{}, |
| 87 |
} |
| 88 |
both := map[string]any{} |
| 89 |
for k, v := range scalars { |
| 90 |
both[k] = v |
| 91 |
} |
| 92 |
for k, v := range others { |
| 93 |
both[k] = v |
| 94 |
} |
| 95 |
values := []any{ |
| 96 |
scalars, others, both, map[string]any{}, map[string]any(nil), |
| 97 |
map[unorderedMapKey]any{"a": 1, "b": []int{2}}, |
| 98 |
[]map[string]any{both, scalars}, |
| 99 |
struct { |
| 100 |
M map[string]any |
| 101 |
N map[string]any `json:",omitempty"` |
| 102 |
}{M: both}, |
| 103 |
} |
| 104 |
assertUnorderedMapsEqual(t, values) |
| 105 |
} |
| 106 |
|
| 107 |
type unorderedMapString string |
| 108 |
|
| 109 |
func TestEncodeUnorderedMapOfScalars(t *testing.T) { |
| 110 |
stringKey := func(i int) string { return "k" + strconv.Itoa(i*7919%1000) } |
| 111 |
one := 1 |
| 112 |
values := []any{ |
| 113 |
map[string]string{}, map[string]string(nil), map[string]bool(nil), |
| 114 |
} |
| 115 |
for _, n := range []int{1, 2, 9, 100, 1000} { |
| 116 |
values = append(values, |
| 117 |
mapOf(n, stringKey, func(i int) string { return "v<>&\u2028" + strconv.Itoa(i) }), |
| 118 |
mapOf(n, stringKey, func(i int) unorderedMapString { return unorderedMapString(strconv.Itoa(i)) }), |
| 119 |
mapOf(n, stringKey, func(i int) bool { return i%2 == 0 }), |
| 120 |
mapOf(n, stringKey, func(i int) int8 { return int8(i) }), |
| 121 |
mapOf(n, stringKey, func(i int) int16 { return int16(-i) }), |
| 122 |
mapOf(n, stringKey, func(i int) int32 { return int32(i) }), |
| 123 |
mapOf(n, stringKey, func(i int) int { return -i * 1000000 }), |
| 124 |
mapOf(n, stringKey, func(i int) uint8 { return uint8(i) }), |
| 125 |
mapOf(n, stringKey, func(i int) uint64 { return uint64(i) << 40 }), |
| 126 |
mapOf(n, stringKey, func(i int) float32 { return float32(i) + 0.25 }), |
| 127 |
mapOf(n, stringKey, func(i int) float64 { return float64(i) * 1e-7 }), |
| 128 |
mapOf(n, stringKey, func(i int) []byte { return []byte(strconv.Itoa(i)) }), |
| 129 |
mapOf(n, stringKey, func(i int) stdjson.Number { return stdjson.Number(strconv.Itoa(i)) }), |
| 130 |
mapOf(n, func(i int) unorderedMapKey { return unorderedMapKey(stringKey(i)) }, func(i int) string { return "v" }), |
| 131 |
// the values which are not written as the map is read: pointers, structs, and keys with a marshaler. |
| 132 |
mapOf(n, stringKey, func(i int) *int { return &one }), |
| 133 |
mapOf(n, stringKey, func(i int) *string { return nil }), |
| 134 |
mapOf(n, stringKey, func(i int) struct{ A int } { return struct{ A int }{i} }), |
| 135 |
mapOf(n, stringKey, func(i int) unorderedMapMarshaler { return unorderedMapMarshaler{i} }), |
| 136 |
mapOf(n, func(i int) mapTextKey { return mapTextKey{i} }, func(i int) string { return "v" }), |
| 137 |
) |
| 138 |
} |
| 139 |
values = append(values, |
| 140 |
struct { |
| 141 |
M map[string]int |
| 142 |
N map[string]string `json:",omitempty"` |
| 143 |
E map[string]bool |
| 144 |
}{M: map[string]int{"a": 1}, E: map[string]bool{}}, |
| 145 |
[]map[string]float64{{"a": 1.5}, {}}, |
| 146 |
map[string]map[string]int{"a": {"x": 1, "y": 2}, "b": {}}, |
| 147 |
) |
| 148 |
assertUnorderedMapsEqual(t, values) |
| 149 |
} |
| 150 |
|
| 151 |
// An unordered map of scalars is ranged over with one variable for its values, whose address is given to the |
| 152 |
// opcode of the value: it is allocated once for the map, not once for each entry. |
| 153 |
func TestEncodeUnorderedMapOfScalarsAllocs(t *testing.T) { |
| 154 |
if raceEnabled { |
| 155 |
t.Skip("the runtime context, which keeps the buffer to reuse, is dropped from its pool at random") |
| 156 |
} |
| 157 |
stringKey := func(i int) string { return "k" + strconv.Itoa(i) } |
| 158 |
for _, e := range unorderedMapEncoders { |
| 159 |
t.Run(e.name, func(t *testing.T) { |
| 160 |
allocs := func(v any) float64 { |
| 161 |
if _, err := e.encode(v); err != nil { |
| 162 |
t.Fatal(err) |
| 163 |
} |
| 164 |
return testing.AllocsPerRun(100, func() { _, _ = e.encode(v) }) |
| 165 |
} |
| 166 |
small := allocs(mapOf(1, stringKey, func(i int) int { return i })) |
| 167 |
large := allocs(mapOf(100, stringKey, func(i int) int { return i })) |
| 168 |
if large != small { |
| 169 |
t.Fatalf("%v allocations for 100 entries, %v for 1 entry", large, small) |
| 170 |
} |
| 171 |
}) |
| 172 |
} |
| 173 |
} |