Debian Codegraph @

decode_container_test.go

raw
1 package json_test
2
3 import (
4 "reflect"
5 "strings"
6 "testing"
7
8 json "github.com/goccy/go-json"
9 )
10
11 // The decoders of arrays, slices, maps and interface{} allocate and assign through the reflect package.
12 // These tests fix what they do with the value which is decoded into.
13
14 func TestDecodeArrayZeroesTheRest(t *testing.T) {
15 // A shorter JSON array zeroes the rest of the array, and nothing after it.
16 var v struct {
17 A [4]uint8
18 B uint32
19 }
20 v.A = [4]uint8{9, 9, 9, 9}
21 v.B = 0xdeadbeef
22 if err := json.Unmarshal([]byte(`{"A":[1]}`), &v); err != nil {
23 t.Fatal(err)
24 }
25 if v.A != [4]uint8{1, 0, 0, 0} || v.B != 0xdeadbeef {
26 t.Fatalf("got %v %x", v.A, v.B)
27 }
28 type pair struct{ X, Y int }
29 w := [2]pair{{1, 7}, {2, 7}}
30 if err := json.Unmarshal([]byte(`[]`), &w); err != nil {
31 t.Fatal(err)
32 }
33 if w != [2]pair{} {
34 t.Fatalf("got %v", w)
35 }
36 s := [3]*int{new(int), new(int), new(int)}
37 if err := json.Unmarshal([]byte(`[1]`), &s); err != nil {
38 t.Fatal(err)
39 }
40 if *s[0] != 1 || s[1] != nil || s[2] != nil {
41 t.Fatalf("got %v", s)
42 }
43 }
44
45 func TestDecodeSliceIntoExisting(t *testing.T) {
46 type elem struct{ A, B int }
47 // The elements of the slice are decoded into its existing elements.
48 v := []elem{{A: 1, B: 1}, {A: 2, B: 2}}
49 if err := json.Unmarshal([]byte(`[{"A":10},{"B":20},{"A":30}]`), &v); err != nil {
50 t.Fatal(err)
51 }
52 if want := []elem{{10, 1}, {2, 20}, {30, 0}}; !reflect.DeepEqual(v, want) {
53 t.Fatalf("got %v, want %v", v, want)
54 }
55 // The array of the slice is reused when it is large enough; the elements after
56 // its length are decoded into, as encoding/json decodes them.
57 backing := make([]elem, 4)
58 backing[1] = elem{A: 5, B: 5}
59 v = backing[:1]
60 if err := json.Unmarshal([]byte(`[{"A":1},{"A":2}]`), &v); err != nil {
61 t.Fatal(err)
62 }
63 if want := []elem{{1, 0}, {2, 5}}; !reflect.DeepEqual(v, want) {
64 t.Fatalf("got %v, want %v", v, want)
65 }
66 if &v[0] != &backing[0] {
67 t.Fatal("the array of the slice is not reused")
68 }
69 // A shorter array truncates.
70 v = []elem{{1, 1}, {2, 2}, {3, 3}}
71 if err := json.Unmarshal([]byte(`[{"A":9}]`), &v); err != nil {
72 t.Fatal(err)
73 }
74 if want := []elem{{9, 1}}; !reflect.DeepEqual(v, want) {
75 t.Fatalf("got %v, want %v", v, want)
76 }
77 // An empty array is an empty slice, not nil; null is nil.
78 var e []int
79 if err := json.Unmarshal([]byte(`[]`), &e); err != nil {
80 t.Fatal(err)
81 }
82 if e == nil || len(e) != 0 {
83 t.Fatalf("got %#v", e)
84 }
85 if err := json.Unmarshal([]byte(`null`), &e); err != nil {
86 t.Fatal(err)
87 }
88 if e != nil {
89 t.Fatalf("got %#v", e)
90 }
91 }
92
93 func TestDecodeSliceOfPointersIsNotShared(t *testing.T) {
94 // Every element of a new slice gets a pointer of its own, also when the decoder reuses its buffers.
95 for i := 0; i < 3; i++ {
96 var v []*struct{ A int }
97 if err := json.Unmarshal([]byte(`[{"A":1},{"A":2},{"A":3},{"A":4},{"A":5}]`), &v); err != nil {
98 t.Fatal(err)
99 }
100 for j, e := range v {
101 if e.A != j+1 {
102 t.Fatalf("element %d: got %d", j, e.A)
103 }
104 for k := 0; k < j; k++ {
105 if v[k] == e {
106 t.Fatalf("elements %d and %d share a pointer", k, j)
107 }
108 }
109 }
110 }
111 }
112
113 func TestDecodeSliceAfterError(t *testing.T) {
114 // An error in the middle of an array leaves nothing behind for the next decode.
115 type elem struct {
116 P *int
117 S string
118 }
119 var v []elem
120 if err := json.Unmarshal([]byte(`[{"P":1,"S":"a"},{"P":"x"}]`), &v); err == nil {
121 t.Fatal("expected an error")
122 }
123 v = nil
124 if err := json.Unmarshal([]byte(`[{"S":"b"},{"S":"c"}]`), &v); err != nil {
125 t.Fatal(err)
126 }
127 if len(v) != 2 || v[0].P != nil || v[1].P != nil || v[0].S != "b" || v[1].S != "c" {
128 t.Fatalf("got %+v", v)
129 }
130 }
131
132 func TestDecodeMapValuesAreNotShared(t *testing.T) {
133 // Every value of a map is decoded into a zero value of its own.
134 var m map[string]*struct{ A, B int }
135 if err := json.Unmarshal([]byte(`{"x":{"A":1},"y":{"B":2}}`), &m); err != nil {
136 t.Fatal(err)
137 }
138 if m["x"] == m["y"] || m["x"].A != 1 || m["x"].B != 0 || m["y"].A != 0 || m["y"].B != 2 {
139 t.Fatalf("got x=%+v y=%+v", m["x"], m["y"])
140 }
141 var s map[string]struct{ A, B int }
142 if err := json.Unmarshal([]byte(`{"x":{"A":1},"y":{"B":2}}`), &s); err != nil {
143 t.Fatal(err)
144 }
145 if s["x"].B != 0 || s["y"].A != 0 {
146 t.Fatalf("got %+v", s)
147 }
148 var sl map[string][]int
149 if err := json.Unmarshal([]byte(`{"x":[1,2,3],"y":[4]}`), &sl); err != nil {
150 t.Fatal(err)
151 }
152 if !reflect.DeepEqual(sl, map[string][]int{"x": {1, 2, 3}, "y": {4}}) {
153 t.Fatalf("got %v", sl)
154 }
155 // A map which exists is added to.
156 im := map[int]string{1: "a"}
157 if err := json.Unmarshal([]byte(`{"2":"b"}`), &im); err != nil {
158 t.Fatal(err)
159 }
160 if !reflect.DeepEqual(im, map[int]string{1: "a", 2: "b"}) {
161 t.Fatalf("got %v", im)
162 }
163 }
164
165 func TestDecodeInterfaceNested(t *testing.T) {
166 // The values nested in interface{} are decoded through a shared slot and stack: each of them
167 // must end up where it belongs.
168 in := `{"a":[1,"s",[true,null,{"b":[2,3]}],{}],"c":{"d":{"e":[[],[[4]]]}},"f":"g","h":1.5}`
169 var got any
170 if err := json.Unmarshal([]byte(in), &got); err != nil {
171 t.Fatal(err)
172 }
173 want := map[string]any{
174 "a": []any{1.0, "s", []any{true, nil, map[string]any{"b": []any{2.0, 3.0}}}, map[string]any{}},
175 "c": map[string]any{"d": map[string]any{"e": []any{[]any{}, []any{[]any{4.0}}}}},
176 "f": "g",
177 "h": 1.5,
178 }
179 if !reflect.DeepEqual(got, want) {
180 t.Fatalf("got %#v", got)
181 }
182 // The same, through a field of map[string]interface{} and of []interface{}.
183 var typed struct {
184 M map[string]any `json:"m"`
185 S []any `json:"s"`
186 }
187 typed.M = map[string]any{"old": true}
188 if err := json.Unmarshal([]byte(`{"m":`+in+`,"s":[`+in+`,2]}`), &typed); err != nil {
189 t.Fatal(err)
190 }
191 wantM := map[string]any{"old": true}
192 for k, v := range want {
193 wantM[k] = v
194 }
195 if !reflect.DeepEqual(typed.M, wantM) {
196 t.Fatalf("got %#v", typed.M)
197 }
198 if !reflect.DeepEqual(typed.S, []any{want, 2.0}) {
199 t.Fatalf("got %#v", typed.S)
200 }
201 }
202
203 func TestDecodeInterfaceManyValues(t *testing.T) {
204 // More numbers and strings than a slab holds, over several calls: every value keeps its own.
205 var b strings.Builder
206 b.WriteString("[")
207 for i := 0; i < 100; i++ {
208 if i > 0 {
209 b.WriteString(",")
210 }
211 b.WriteString(`{"n":`)
212 b.WriteString(strings.Repeat("1", i%9+1))
213 b.WriteString(`,"s":"`)
214 b.WriteString(strings.Repeat("x", i))
215 b.WriteString(`"}`)
216 }
217 b.WriteString("]")
218 var results [][]any
219 for call := 0; call < 3; call++ {
220 var v []any
221 if err := json.Unmarshal([]byte(b.String()), &v); err != nil {
222 t.Fatal(err)
223 }
224 results = append(results, v)
225 }
226 for _, v := range results {
227 for i, e := range v {
228 m := e.(map[string]any)
229 var n float64
230 for j := 0; j < i%9+1; j++ {
231 n = n*10 + 1
232 }
233 if m["n"] != n || m["s"] != strings.Repeat("x", i) {
234 t.Fatalf("element %d: got %v", i, m)
235 }
236 }
237 }
238 }
239
240 func TestDecodeInterfaceAfterError(t *testing.T) {
241 // An error in the middle of nested arrays leaves nothing on the stack for the next decode.
242 var v any
243 if err := json.Unmarshal([]byte(`[1,[2,[3,x]]]`), &v); err == nil {
244 t.Fatal("expected an error")
245 }
246 if err := json.Unmarshal([]byte(`[[1],[2,3]]`), &v); err != nil {
247 t.Fatal(err)
248 }
249 if !reflect.DeepEqual(v, []any{[]any{1.0}, []any{2.0, 3.0}}) {
250 t.Fatalf("got %#v", v)
251 }
252 if err := json.Unmarshal([]byte(`{null:1}`), &v); err == nil {
253 t.Fatal("expected an error for null as a key")
254 }
255 }
256
257 func TestDecodeInterfaceEmptyArrays(t *testing.T) {
258 // The empty arrays decoded as interface{} are empty, not nil, slices, and an append to one of them
259 // changes none of the others.
260 var v any
261 if err := json.Unmarshal([]byte(`[[],{"a":[]},[]]`), &v); err != nil {
262 t.Fatal(err)
263 }
264 outer := v.([]any)
265 a, b := outer[0].([]any), outer[1].(map[string]any)["a"].([]any)
266 if a == nil || len(a) != 0 || b == nil || len(b) != 0 {
267 t.Fatalf("got %#v", v)
268 }
269 a = append(a, 1.0)
270 outer[0] = a
271 if len(outer[2].([]any)) != 0 || len(outer[1].(map[string]any)["a"].([]any)) != 0 {
272 t.Fatalf("an append changed another empty array: %#v", v)
273 }
274 var w any
275 if err := json.Unmarshal([]byte(`[]`), &w); err != nil {
276 t.Fatal(err)
277 }
278 if s := w.([]any); s == nil || len(s) != 0 {
279 t.Fatalf("got %#v", w)
280 }
281 if !reflect.DeepEqual(v, []any{[]any{1.0}, map[string]any{"a": []any{}}, []any{}}) {
282 t.Fatalf("got %#v", v)
283 }
284 }