| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
stdjson "encoding/json" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/goccy/go-json" |
| 8 |
) |
| 9 |
|
| 10 |
// A value of a recursive type which is the last field of a value of the same type is encoded in the frame of |
| 11 |
// that value, without a frame of its own: a list of such values must encode as encoding/json does whatever |
| 12 |
// its length, with an indent, with other values in the frame ( an interface value, another recursive type, |
| 13 |
// a slice of the type ) and after a cycle. |
| 14 |
|
| 15 |
type tailNode struct { |
| 16 |
V int `json:"v"` |
| 17 |
I any `json:"i,omitempty"` |
| 18 |
Next *tailNode `json:"next"` |
| 19 |
} |
| 20 |
|
| 21 |
type tailNodeOmitEmpty struct { |
| 22 |
V int `json:"v"` |
| 23 |
Next *tailNodeOmitEmpty `json:"next,omitempty"` |
| 24 |
} |
| 25 |
|
| 26 |
type tailTree struct { |
| 27 |
Children []*tailTree `json:"children,omitempty"` |
| 28 |
Next *tailTree `json:"next"` |
| 29 |
} |
| 30 |
|
| 31 |
type tailOther struct { |
| 32 |
V int `json:"v"` |
| 33 |
Next *tailOther `json:"next"` |
| 34 |
} |
| 35 |
|
| 36 |
type tailMixed struct { |
| 37 |
Other *tailOther `json:"other"` |
| 38 |
Next *tailMixed `json:"next"` |
| 39 |
} |
| 40 |
|
| 41 |
// the recursive field which is not the last is not a tail. |
| 42 |
type tailFirst struct { |
| 43 |
Next *tailFirst `json:"next"` |
| 44 |
V int `json:"v"` |
| 45 |
} |
| 46 |
|
| 47 |
func newTailList(n int) *tailNode { |
| 48 |
var head *tailNode |
| 49 |
for i := n; i > 0; i-- { |
| 50 |
head = &tailNode{V: i, Next: head} |
| 51 |
if i%3 == 0 { |
| 52 |
head.I = &tailNode{V: -i} |
| 53 |
} |
| 54 |
} |
| 55 |
return head |
| 56 |
} |
| 57 |
|
| 58 |
func TestEncodeTailRecursion(t *testing.T) { |
| 59 |
deep := newTailList(1500) // more than the level cycles are detected from |
| 60 |
var omit *tailNodeOmitEmpty |
| 61 |
for i := 3; i > 0; i-- { |
| 62 |
omit = &tailNodeOmitEmpty{V: i, Next: omit} |
| 63 |
} |
| 64 |
var first *tailFirst |
| 65 |
for i := 3; i > 0; i-- { |
| 66 |
first = &tailFirst{V: i, Next: first} |
| 67 |
} |
| 68 |
values := []any{ |
| 69 |
(*tailNode)(nil), |
| 70 |
&tailNode{}, |
| 71 |
newTailList(1), |
| 72 |
newTailList(2), |
| 73 |
newTailList(3), |
| 74 |
newTailList(10), |
| 75 |
deep, |
| 76 |
[]*tailNode{newTailList(2), nil, newTailList(1)}, |
| 77 |
map[string]*tailNode{"a": newTailList(2)}, |
| 78 |
struct { |
| 79 |
L *tailNode `json:"l"` |
| 80 |
N int `json:"n"` |
| 81 |
}{newTailList(2), 1}, |
| 82 |
omit, |
| 83 |
&tailTree{Children: []*tailTree{{Next: &tailTree{}}, {}}, Next: &tailTree{Children: []*tailTree{{}}}}, |
| 84 |
&tailMixed{Other: &tailOther{1, &tailOther{2, nil}}, Next: &tailMixed{Other: &tailOther{3, nil}}}, |
| 85 |
first, |
| 86 |
// a value with a tail after a tail: the interface value holds a list of its own. |
| 87 |
&tailNode{V: 1, I: newTailList(3), Next: &tailNode{V: 2, I: newTailList(2)}}, |
| 88 |
} |
| 89 |
for _, v := range values { |
| 90 |
expected, err := stdjson.Marshal(v) |
| 91 |
if err != nil { |
| 92 |
t.Fatal(err) |
| 93 |
} |
| 94 |
got, err := json.Marshal(v) |
| 95 |
if err != nil { |
| 96 |
t.Fatalf("%T: %v", v, err) |
| 97 |
} |
| 98 |
if string(got) != string(expected) { |
| 99 |
t.Errorf("%T:\n got %.300s\nwant %.300s", v, got, expected) |
| 100 |
} |
| 101 |
expected, _ = stdjson.MarshalIndent(v, "", " ") |
| 102 |
got, err = json.MarshalIndent(v, "", " ") |
| 103 |
if err != nil { |
| 104 |
t.Fatalf("%T: %v", v, err) |
| 105 |
} |
| 106 |
if string(got) != string(expected) { |
| 107 |
t.Errorf("%T with indent:\n got %.500s\nwant %.500s", v, got, expected) |
| 108 |
} |
| 109 |
} |
| 110 |
// a cycle is an error, and the context is left usable. |
| 111 |
cycle := newTailList(3) |
| 112 |
cycle.Next.Next.Next = cycle |
| 113 |
if _, err := json.Marshal(cycle); err == nil { |
| 114 |
t.Fatal("a cycle must be an error") |
| 115 |
} |
| 116 |
if _, err := json.Marshal(newTailList(3)); err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
} |