| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"fmt" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/goccy/go-json" |
| 10 |
) |
| 11 |
|
| 12 |
// The entries of a map with keys of a string kind are read by ranging over the map as a map of the same |
| 13 |
// layout, by the size of the value; a map with keys of another kind is read by reflect.MapIter. Every size of |
| 14 |
// a value around the words, values with pointers, values too large to be in the map, keys of every kind, and |
| 15 |
// maps large enough to have grown, must encode as encoding/json does. |
| 16 |
|
| 17 |
type mapValueTwoWords struct { |
| 18 |
A int |
| 19 |
B string |
| 20 |
} |
| 21 |
|
| 22 |
type mapValue12 struct { |
| 23 |
A, B, C int32 |
| 24 |
} |
| 25 |
|
| 26 |
type mapValue136 struct { |
| 27 |
A [17]int64 |
| 28 |
} |
| 29 |
|
| 30 |
// mapValueLargeWithPointers is a value stored out of the map by the runtime, with pointers. |
| 31 |
type mapValueLargeWithPointers struct { |
| 32 |
S string |
| 33 |
P *int |
| 34 |
M map[string]int |
| 35 |
I any |
| 36 |
A [14]int64 |
| 37 |
} |
| 38 |
|
| 39 |
type mapValueWithPointers struct { |
| 40 |
P *int |
| 41 |
S []string |
| 42 |
M map[string]int |
| 43 |
I any |
| 44 |
} |
| 45 |
|
| 46 |
type mapTextKey struct{ V int } |
| 47 |
|
| 48 |
func (k mapTextKey) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("k%d", k.V)), nil } |
| 49 |
|
| 50 |
func mapOf[K comparable, V any](n int, key func(int) K, value func(int) V) map[K]V { |
| 51 |
m := make(map[K]V, 0) |
| 52 |
for i := 0; i < n; i++ { |
| 53 |
m[key(i)] = value(i) |
| 54 |
} |
| 55 |
return m |
| 56 |
} |
| 57 |
|
| 58 |
func TestEncodeMapLayouts(t *testing.T) { |
| 59 |
one := 1 |
| 60 |
stringKey := func(i int) string { return "k" + strconv.Itoa(i*7919%1000) } |
| 61 |
values := []any{} |
| 62 |
for _, n := range []int{1, 2, 8, 9, 50, 1000} { |
| 63 |
values = append(values, |
| 64 |
mapOf(n, stringKey, func(i int) struct{} { return struct{}{} }), |
| 65 |
mapOf(n, stringKey, func(i int) bool { return i%2 == 0 }), |
| 66 |
mapOf(n, stringKey, func(i int) int8 { return int8(i) }), |
| 67 |
mapOf(n, stringKey, func(i int) int32 { return int32(-i) }), |
| 68 |
mapOf(n, stringKey, func(i int) int { return i }), |
| 69 |
mapOf(n, stringKey, func(i int) float64 { return float64(i) + 0.5 }), |
| 70 |
mapOf(n, stringKey, func(i int) mapValue12 { return mapValue12{int32(i), 2, 3} }), |
| 71 |
mapOf(n, stringKey, func(i int) string { return strconv.Itoa(i) }), |
| 72 |
mapOf(n, stringKey, func(i int) any { return []any{i, "s", nil}[i%3] }), |
| 73 |
mapOf(n, stringKey, func(i int) mapValueTwoWords { return mapValueTwoWords{i, "s"} }), |
| 74 |
mapOf(n, stringKey, func(i int) [3]int { return [3]int{i, i, i} }), |
| 75 |
mapOf(n, stringKey, func(i int) mapValueWithPointers { |
| 76 |
return mapValueWithPointers{P: &one, S: []string{"a"}, M: map[string]int{"x": i}, I: i} |
| 77 |
}), |
| 78 |
mapOf(n, stringKey, func(i int) [16]int64 { return [16]int64{15: int64(i)} }), |
| 79 |
mapOf(n, stringKey, func(i int) mapValue136 { return mapValue136{[17]int64{16: int64(i)}} }), |
| 80 |
mapOf(n, stringKey, func(i int) mapValueLargeWithPointers { |
| 81 |
return mapValueLargeWithPointers{S: "s" + strconv.Itoa(i), P: &one, M: map[string]int{"x": i}, I: []any{i}, A: [14]int64{13: int64(i)}} |
| 82 |
}), |
| 83 |
mapOf(n, stringKey, func(i int) [32]int64 { return [32]int64{0: 1, 31: int64(i)} }), |
| 84 |
mapOf(n, stringKey, func(i int) [33]int64 { return [33]int64{0: 1, 32: int64(i)} }), |
| 85 |
mapOf(n, stringKey, func(i int) *int { return &one }), |
| 86 |
mapOf(n, stringKey, func(i int) map[string]int { return map[string]int{"n": i} }), |
| 87 |
mapOf(n, func(i int) mapKeyString { return mapKeyString(stringKey(i)) }, func(i int) int { return i }), |
| 88 |
mapOf(n, func(i int) int { return i - n/2 }, func(i int) string { return "v" }), |
| 89 |
mapOf(n, func(i int) int64 { return int64(i) * 1000000000 }, func(i int) int { return i }), |
| 90 |
mapOf(n, func(i int) uint8 { return uint8(i) }, func(i int) any { return i }), |
| 91 |
mapOf(n, func(i int) mapTextKey { return mapTextKey{i} }, func(i int) mapValueTwoWords { return mapValueTwoWords{i, "s"} }), |
| 92 |
) |
| 93 |
} |
| 94 |
values = append(values, |
| 95 |
map[string]int{}, |
| 96 |
map[string]int(nil), |
| 97 |
struct { |
| 98 |
M map[string]int `json:"m,omitempty"` |
| 99 |
N map[string]int `json:"n,omitempty"` |
| 100 |
E map[int]int `json:"e,omitempty"` |
| 101 |
}{M: map[string]int{"a": 1}, N: map[string]int{}, E: map[int]int{}}, |
| 102 |
[]map[string]any{{"a": map[string]any{"b": []any{map[string]int{"c": 1}}}}}, |
| 103 |
) |
| 104 |
for _, v := range values { |
| 105 |
expected, err := stdjson.Marshal(v) |
| 106 |
if err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
got, err := json.Marshal(v) |
| 110 |
if err != nil { |
| 111 |
t.Fatalf("%T: %v", v, err) |
| 112 |
} |
| 113 |
if string(got) != string(expected) { |
| 114 |
t.Errorf("%T:\n got %.300s\nwant %.300s", v, got, expected) |
| 115 |
} |
| 116 |
expected, _ = stdjson.MarshalIndent(v, "", " ") |
| 117 |
got, err = json.MarshalIndent(v, "", " ") |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("%T: %v", v, err) |
| 120 |
} |
| 121 |
if string(got) != string(expected) { |
| 122 |
t.Errorf("%T with indent:\n got %.300s\nwant %.300s", v, got, expected) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// the entries of an unordered map are indented as the entries of a sorted one. |
| 128 |
func TestEncodeMapUnorderedIndent(t *testing.T) { |
| 129 |
v := map[string]map[string]int{"a": {"x": 1}} |
| 130 |
expected, _ := stdjson.MarshalIndent(v, "", " ") |
| 131 |
got, err := json.MarshalIndentWithOption(v, "", " ", json.UnorderedMap()) |
| 132 |
if err != nil { |
| 133 |
t.Fatal(err) |
| 134 |
} |
| 135 |
if string(got) != string(expected) { |
| 136 |
t.Fatalf("\n got %s\nwant %s", got, expected) |
| 137 |
} |
| 138 |
} |