| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
stdjson "encoding/json" |
| 6 |
"fmt" |
| 7 |
"runtime" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/goccy/go-json" |
| 11 |
) |
| 12 |
|
| 13 |
type nestedFrameNode struct { |
| 14 |
Name string |
| 15 |
Tags []string |
| 16 |
Any any `json:",omitempty"` |
| 17 |
Next *nestedFrameNode `json:",omitempty"` |
| 18 |
Kids []nestedFrameNode `json:",omitempty"` |
| 19 |
Attrs map[string]*nestedFrameNode |
| 20 |
} |
| 21 |
|
| 22 |
func nestedFrameList(depth int) *nestedFrameNode { |
| 23 |
var head *nestedFrameNode |
| 24 |
for i := 0; i < depth; i++ { |
| 25 |
head = &nestedFrameNode{ |
| 26 |
Name: fmt.Sprint("node", i), |
| 27 |
Tags: []string{"a", "b"}, |
| 28 |
Next: head, |
| 29 |
} |
| 30 |
} |
| 31 |
return head |
| 32 |
} |
| 33 |
|
| 34 |
func nestedFrameTree(depth int) nestedFrameNode { |
| 35 |
node := nestedFrameNode{Name: fmt.Sprint("tree", depth), Any: depth} |
| 36 |
if depth > 0 { |
| 37 |
node.Kids = []nestedFrameNode{nestedFrameTree(depth - 1), nestedFrameTree(depth - 1)} |
| 38 |
node.Attrs = map[string]*nestedFrameNode{"attr": {Name: "leaf", Any: []any{depth, "x"}}} |
| 39 |
} |
| 40 |
return node |
| 41 |
} |
| 42 |
|
| 43 |
func nestedFrameMap(depth int) any { |
| 44 |
var v any = "leaf" |
| 45 |
for i := 0; i < depth; i++ { |
| 46 |
v = map[string]any{"v": v, "list": []any{i, nil, "s"}} |
| 47 |
} |
| 48 |
return v |
| 49 |
} |
| 50 |
|
| 51 |
// The VM allocates the frames of the interface values and of the recursive types one after another in its slots, |
| 52 |
// which grow when the next frame doesn't fit. The values here are nested deeply enough to make them grow many |
| 53 |
// times, with the frames at various positions in the slots. |
| 54 |
func TestEncodeNestedFrames(t *testing.T) { |
| 55 |
for _, test := range []struct { |
| 56 |
name string |
| 57 |
v any |
| 58 |
}{ |
| 59 |
{"recursive list", nestedFrameList(300)}, |
| 60 |
{"recursive tree", nestedFrameTree(8)}, |
| 61 |
{"map of interface", nestedFrameMap(200)}, |
| 62 |
{"recursive in interface", nestedInterfaceValue(50, nestedFrameList(50))}, |
| 63 |
{"interface in recursive", &nestedFrameNode{Name: "root", Any: nestedFrameMap(40), Next: &nestedFrameNode{Any: nestedFrameTree(4)}}}, |
| 64 |
} { |
| 65 |
t.Run(test.name, func(t *testing.T) { |
| 66 |
for depth := 0; depth < 12; depth++ { |
| 67 |
// the depth shifts the positions of the frames in the slots. |
| 68 |
v := nestedInterfaceValue(depth, test.v) |
| 69 |
t.Run("Marshal", func(t *testing.T) { |
| 70 |
expected, err := stdjson.Marshal(v) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
got, err := json.Marshal(v) |
| 75 |
if err != nil { |
| 76 |
t.Fatal(err) |
| 77 |
} |
| 78 |
if !bytes.Equal(expected, got) { |
| 79 |
t.Fatalf("depth %d: expected %d bytes but got %d bytes", depth, len(expected), len(got)) |
| 80 |
} |
| 81 |
}) |
| 82 |
t.Run("MarshalIndent", func(t *testing.T) { |
| 83 |
expected, err := stdjson.MarshalIndent(v, "", " ") |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
got, err := json.MarshalIndent(v, "", " ") |
| 88 |
if err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
if !bytes.Equal(expected, got) { |
| 92 |
t.Fatalf("depth %d: expected %d bytes but got %d bytes", depth, len(expected), len(got)) |
| 93 |
} |
| 94 |
}) |
| 95 |
} |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
type nestedTypeLeaf struct { |
| 101 |
M map[string][]int |
| 102 |
} |
| 103 |
|
| 104 |
type nestedTypeLevel4 struct{ X []nestedTypeLeaf } |
| 105 |
type nestedTypeLevel3 struct{ X []nestedTypeLevel4 } |
| 106 |
type nestedTypeLevel2 struct{ X []nestedTypeLevel3 } |
| 107 |
type nestedTypeLevel1 struct{ X []nestedTypeLevel2 } |
| 108 |
type nestedTypeLevel0 struct{ X []nestedTypeLevel1 } |
| 109 |
|
| 110 |
// The length of a frame depends on how deep the values are nested in a type, and it has no limit. |
| 111 |
func TestEncodeDeeplyNestedType(t *testing.T) { |
| 112 |
slices := [][][][][][][][][][][][][][][][]int{{{{{{{{{{{{{{{{1, 2}, {3}}}}}}}}}}}}}}}} |
| 113 |
structs := []nestedTypeLevel0{{X: []nestedTypeLevel1{{X: []nestedTypeLevel2{{X: []nestedTypeLevel3{{X: []nestedTypeLevel4{ |
| 114 |
{X: []nestedTypeLeaf{{M: map[string][]int{"a": {1, 2}, "b": nil}}, {}}}, |
| 115 |
{}, |
| 116 |
}}}}}}}}} |
| 117 |
for _, v := range []any{slices, structs} { |
| 118 |
expected, err := stdjson.Marshal(v) |
| 119 |
if err != nil { |
| 120 |
t.Fatal(err) |
| 121 |
} |
| 122 |
got, err := json.Marshal(v) |
| 123 |
if err != nil { |
| 124 |
t.Fatal(err) |
| 125 |
} |
| 126 |
if !bytes.Equal(expected, got) { |
| 127 |
t.Fatalf("expected %s but got %s", expected, got) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
type gcDuringMapValue int |
| 133 |
|
| 134 |
func (v gcDuringMapValue) MarshalJSON() ([]byte, error) { |
| 135 |
runtime.GC() |
| 136 |
return []byte(fmt.Sprint(int(v))), nil |
| 137 |
} |
| 138 |
|
| 139 |
// The VM refers to the context of a map only by a slot, which the GC doesn't see. |
| 140 |
// The context has to stay alive while the map, and the maps in it, are encoded. |
| 141 |
func TestEncodeMapWhileGCRuns(t *testing.T) { |
| 142 |
v := map[string]map[string]gcDuringMapValue{ |
| 143 |
"a": {"x": 1, "y": 2}, |
| 144 |
"b": {"z": 3}, |
| 145 |
"c": {}, |
| 146 |
} |
| 147 |
expected := `{"a":{"x":1,"y":2},"b":{"z":3},"c":{}}` |
| 148 |
for i := 0; i < 20; i++ { |
| 149 |
got, err := json.Marshal(v) |
| 150 |
if err != nil { |
| 151 |
t.Fatal(err) |
| 152 |
} |
| 153 |
if string(got) != expected { |
| 154 |
t.Fatalf("expected %s but got %s", expected, got) |
| 155 |
} |
| 156 |
} |
| 157 |
} |