blob: 83c01d170c0b3405ae52b233f345f4144ec54b72 [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2010 The Go Authors. All rights reserved.
Russ Cox0e8384a2010-04-27 10:46:37 -07002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package json
6
7import (
8 "bytes"
Peter Waldschmidt0cf48b42015-04-18 03:23:32 -04009 "io"
Brad Fitzpatrick91e99c12013-01-28 16:31:46 -080010 "io/ioutil"
Peter Waldschmidt7e70c242015-07-27 21:33:53 -040011 "log"
Russ Coxccf2b882012-09-18 14:22:55 -040012 "net"
Peter Waldschmidt7e70c242015-07-27 21:33:53 -040013 "net/http"
14 "net/http/httptest"
Russ Cox0e8384a2010-04-27 10:46:37 -070015 "reflect"
Brad Fitzpatrick91e99c12013-01-28 16:31:46 -080016 "strings"
Russ Cox0e8384a2010-04-27 10:46:37 -070017 "testing"
18)
19
20// Test values for the stream test.
21// One of each JSON kind.
22var streamTest = []interface{}{
Russ Coxf2b5a072011-01-19 23:09:00 -050023 0.1,
Russ Cox0e8384a2010-04-27 10:46:37 -070024 "hello",
25 nil,
26 true,
27 false,
28 []interface{}{"a", "b", "c"},
29 map[string]interface{}{"K": "Kelvin", "ß": "long s"},
Russ Coxf2b5a072011-01-19 23:09:00 -050030 3.14, // another value to make sure something can follow map
Russ Cox0e8384a2010-04-27 10:46:37 -070031}
32
33var streamEncoded = `0.1
34"hello"
35null
36true
37false
38["a","b","c"]
39{"ß":"long s","K":"Kelvin"}
403.14
41`
42
43func TestEncoder(t *testing.T) {
44 for i := 0; i <= len(streamTest); i++ {
45 var buf bytes.Buffer
46 enc := NewEncoder(&buf)
Russ Cox34b17d42016-05-23 12:28:56 -040047 // Check that enc.SetIndent("", "") turns off indentation.
48 enc.SetIndent(">", ".")
49 enc.SetIndent("", "")
Russ Cox0e8384a2010-04-27 10:46:37 -070050 for j, v := range streamTest[0:i] {
51 if err := enc.Encode(v); err != nil {
52 t.Fatalf("encode #%d: %v", j, err)
53 }
54 }
55 if have, want := buf.String(), nlines(streamEncoded, i); have != want {
56 t.Errorf("encoding %d items: mismatch", i)
57 diff(t, []byte(have), []byte(want))
58 break
59 }
60 }
61}
62
Caleb Spare098b6262016-03-23 23:14:35 -070063var streamEncodedIndent = `0.1
64"hello"
65null
66true
67false
68[
69>."a",
70>."b",
71>."c"
72>]
73{
74>."ß": "long s",
75>."K": "Kelvin"
76>}
773.14
78`
79
80func TestEncoderIndent(t *testing.T) {
81 var buf bytes.Buffer
82 enc := NewEncoder(&buf)
Russ Cox34b17d42016-05-23 12:28:56 -040083 enc.SetIndent(">", ".")
Caleb Spare098b6262016-03-23 23:14:35 -070084 for _, v := range streamTest {
85 enc.Encode(v)
86 }
87 if have, want := buf.String(), streamEncodedIndent; have != want {
88 t.Error("indented encoding mismatch")
89 diff(t, []byte(have), []byte(want))
90 }
91}
92
Russ Cox4aea7a12016-05-23 11:41:00 -040093func TestEncoderSetEscapeHTML(t *testing.T) {
Caleb Spareab52ad892016-04-09 21:18:22 -070094 var c C
95 var ct CText
96 for _, tt := range []struct {
97 name string
98 v interface{}
99 wantEscape string
100 want string
101 }{
102 {"c", c, `"\u003c\u0026\u003e"`, `"<&>"`},
103 {"ct", ct, `"\"\u003c\u0026\u003e\""`, `"\"<&>\""`},
104 {`"<&>"`, "<&>", `"\u003c\u0026\u003e"`, `"<&>"`},
105 } {
106 var buf bytes.Buffer
107 enc := NewEncoder(&buf)
108 if err := enc.Encode(tt.v); err != nil {
109 t.Fatalf("Encode(%s): %s", tt.name, err)
110 }
111 if got := strings.TrimSpace(buf.String()); got != tt.wantEscape {
112 t.Errorf("Encode(%s) = %#q, want %#q", tt.name, got, tt.wantEscape)
113 }
114 buf.Reset()
Russ Cox4aea7a12016-05-23 11:41:00 -0400115 enc.SetEscapeHTML(false)
Caleb Spareab52ad892016-04-09 21:18:22 -0700116 if err := enc.Encode(tt.v); err != nil {
Russ Cox4aea7a12016-05-23 11:41:00 -0400117 t.Fatalf("SetEscapeHTML(false) Encode(%s): %s", tt.name, err)
Caleb Spareab52ad892016-04-09 21:18:22 -0700118 }
119 if got := strings.TrimSpace(buf.String()); got != tt.want {
Russ Cox4aea7a12016-05-23 11:41:00 -0400120 t.Errorf("SetEscapeHTML(false) Encode(%s) = %#q, want %#q",
Caleb Spareab52ad892016-04-09 21:18:22 -0700121 tt.name, got, tt.want)
122 }
123 }
124}
125
Russ Cox0e8384a2010-04-27 10:46:37 -0700126func TestDecoder(t *testing.T) {
127 for i := 0; i <= len(streamTest); i++ {
128 // Use stream without newlines as input,
129 // just to stress the decoder even more.
130 // Our test input does not include back-to-back numbers.
131 // Otherwise stripping the newlines would
132 // merge two adjacent JSON values.
133 var buf bytes.Buffer
134 for _, c := range nlines(streamEncoded, i) {
135 if c != '\n' {
136 buf.WriteRune(c)
137 }
138 }
139 out := make([]interface{}, i)
140 dec := NewDecoder(&buf)
141 for j := range out {
142 if err := dec.Decode(&out[j]); err != nil {
143 t.Fatalf("decode #%d/%d: %v", j, i, err)
144 }
145 }
146 if !reflect.DeepEqual(out, streamTest[0:i]) {
Rob Pike1ce62452010-12-07 16:42:54 -0500147 t.Errorf("decoding %d items: mismatch", i)
Russ Cox0e8384a2010-04-27 10:46:37 -0700148 for j := range out {
149 if !reflect.DeepEqual(out[j], streamTest[j]) {
Rob Pike1ce62452010-12-07 16:42:54 -0500150 t.Errorf("#%d: have %v want %v", j, out[j], streamTest[j])
Russ Cox0e8384a2010-04-27 10:46:37 -0700151 }
152 }
153 break
154 }
155 }
156}
157
Brad Fitzpatrick91e99c12013-01-28 16:31:46 -0800158func TestDecoderBuffered(t *testing.T) {
159 r := strings.NewReader(`{"Name": "Gopher"} extra `)
160 var m struct {
161 Name string
162 }
163 d := NewDecoder(r)
164 err := d.Decode(&m)
165 if err != nil {
166 t.Fatal(err)
167 }
168 if m.Name != "Gopher" {
169 t.Errorf("Name = %q; want Gopher", m.Name)
170 }
171 rest, err := ioutil.ReadAll(d.Buffered())
172 if err != nil {
173 t.Fatal(err)
174 }
175 if g, w := string(rest), " extra "; g != w {
176 t.Errorf("Remaining = %q; want %q", g, w)
177 }
178}
179
Russ Cox0e8384a2010-04-27 10:46:37 -0700180func nlines(s string, n int) string {
181 if n <= 0 {
182 return ""
183 }
184 for i, c := range s {
185 if c == '\n' {
186 if n--; n == 0 {
187 return s[0 : i+1]
188 }
189 }
190 }
191 return s
192}
193
194func TestRawMessage(t *testing.T) {
195 // TODO(rsc): Should not need the * in *RawMessage
196 var data struct {
197 X float64
198 Id *RawMessage
199 Y float32
200 }
201 const raw = `["\u0056",null]`
202 const msg = `{"X":0.1,"Id":["\u0056",null],"Y":0.2}`
203 err := Unmarshal([]byte(msg), &data)
204 if err != nil {
205 t.Fatalf("Unmarshal: %v", err)
206 }
Russ Cox6aaef042010-06-08 17:51:57 -0700207 if string([]byte(*data.Id)) != raw {
Russ Cox0e8384a2010-04-27 10:46:37 -0700208 t.Fatalf("Raw mismatch: have %#q want %#q", []byte(*data.Id), raw)
209 }
210 b, err := Marshal(&data)
211 if err != nil {
212 t.Fatalf("Marshal: %v", err)
213 }
214 if string(b) != msg {
215 t.Fatalf("Marshal: have %#q want %#q", b, msg)
216 }
217}
Russ Coxfc5889d2011-09-19 11:50:41 -0400218
219func TestNullRawMessage(t *testing.T) {
220 // TODO(rsc): Should not need the * in *RawMessage
221 var data struct {
222 X float64
223 Id *RawMessage
224 Y float32
225 }
226 data.Id = new(RawMessage)
227 const msg = `{"X":0.1,"Id":null,"Y":0.2}`
228 err := Unmarshal([]byte(msg), &data)
229 if err != nil {
230 t.Fatalf("Unmarshal: %v", err)
231 }
232 if data.Id != nil {
233 t.Fatalf("Raw mismatch: have non-nil, want nil")
234 }
235 b, err := Marshal(&data)
236 if err != nil {
237 t.Fatalf("Marshal: %v", err)
238 }
239 if string(b) != msg {
240 t.Fatalf("Marshal: have %#q want %#q", b, msg)
241 }
242}
Russ Coxccf2b882012-09-18 14:22:55 -0400243
244var blockingTests = []string{
245 `{"x": 1}`,
246 `[1, 2, 3]`,
247}
248
249func TestBlocking(t *testing.T) {
250 for _, enc := range blockingTests {
251 r, w := net.Pipe()
252 go w.Write([]byte(enc))
253 var val interface{}
254
255 // If Decode reads beyond what w.Write writes above,
256 // it will block, and the test will deadlock.
257 if err := NewDecoder(r).Decode(&val); err != nil {
258 t.Errorf("decoding %s: %v", enc, err)
259 }
260 r.Close()
261 w.Close()
262 }
263}
Brad Fitzpatrickf1583bb2013-05-14 15:50:46 -0700264
265func BenchmarkEncoderEncode(b *testing.B) {
266 b.ReportAllocs()
267 type T struct {
268 X, Y string
269 }
270 v := &T{"foo", "bar"}
Bryan C. Millsc5b6c2a2017-02-10 15:48:30 -0500271 b.RunParallel(func(pb *testing.PB) {
272 for pb.Next() {
273 if err := NewEncoder(ioutil.Discard).Encode(v); err != nil {
274 b.Fatal(err)
275 }
Brad Fitzpatrickf1583bb2013-05-14 15:50:46 -0700276 }
Bryan C. Millsc5b6c2a2017-02-10 15:48:30 -0500277 })
Brad Fitzpatrickf1583bb2013-05-14 15:50:46 -0700278}
Peter Waldschmidt0cf48b42015-04-18 03:23:32 -0400279
280type tokenStreamCase struct {
281 json string
282 expTokens []interface{}
283}
284
285type decodeThis struct {
286 v interface{}
287}
288
289var tokenStreamCases []tokenStreamCase = []tokenStreamCase{
290 // streaming token cases
291 {json: `10`, expTokens: []interface{}{float64(10)}},
292 {json: ` [10] `, expTokens: []interface{}{
293 Delim('['), float64(10), Delim(']')}},
294 {json: ` [false,10,"b"] `, expTokens: []interface{}{
295 Delim('['), false, float64(10), "b", Delim(']')}},
296 {json: `{ "a": 1 }`, expTokens: []interface{}{
297 Delim('{'), "a", float64(1), Delim('}')}},
298 {json: `{"a": 1, "b":"3"}`, expTokens: []interface{}{
299 Delim('{'), "a", float64(1), "b", "3", Delim('}')}},
300 {json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{
301 Delim('['),
302 Delim('{'), "a", float64(1), Delim('}'),
303 Delim('{'), "a", float64(2), Delim('}'),
304 Delim(']')}},
305 {json: `{"obj": {"a": 1}}`, expTokens: []interface{}{
306 Delim('{'), "obj", Delim('{'), "a", float64(1), Delim('}'),
307 Delim('}')}},
308 {json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{
309 Delim('{'), "obj", Delim('['),
310 Delim('{'), "a", float64(1), Delim('}'),
311 Delim(']'), Delim('}')}},
312
313 // streaming tokens with intermittent Decode()
314 {json: `{ "a": 1 }`, expTokens: []interface{}{
315 Delim('{'), "a",
316 decodeThis{float64(1)},
317 Delim('}')}},
318 {json: ` [ { "a" : 1 } ] `, expTokens: []interface{}{
319 Delim('['),
320 decodeThis{map[string]interface{}{"a": float64(1)}},
321 Delim(']')}},
322 {json: ` [{"a": 1},{"a": 2}] `, expTokens: []interface{}{
323 Delim('['),
324 decodeThis{map[string]interface{}{"a": float64(1)}},
325 decodeThis{map[string]interface{}{"a": float64(2)}},
326 Delim(']')}},
327 {json: `{ "obj" : [ { "a" : 1 } ] }`, expTokens: []interface{}{
328 Delim('{'), "obj", Delim('['),
329 decodeThis{map[string]interface{}{"a": float64(1)}},
330 Delim(']'), Delim('}')}},
331
332 {json: `{"obj": {"a": 1}}`, expTokens: []interface{}{
333 Delim('{'), "obj",
334 decodeThis{map[string]interface{}{"a": float64(1)}},
335 Delim('}')}},
336 {json: `{"obj": [{"a": 1}]}`, expTokens: []interface{}{
337 Delim('{'), "obj",
338 decodeThis{[]interface{}{
339 map[string]interface{}{"a": float64(1)},
340 }},
341 Delim('}')}},
342 {json: ` [{"a": 1} {"a": 2}] `, expTokens: []interface{}{
343 Delim('['),
344 decodeThis{map[string]interface{}{"a": float64(1)}},
Michael Fraenkelf1ce59d2017-10-28 20:50:57 -0400345 decodeThis{&SyntaxError{"expected comma after array element", 11}},
Peter Waldschmidt0cf48b42015-04-18 03:23:32 -0400346 }},
Michael Fraenkelf1ce59d2017-10-28 20:50:57 -0400347 {json: `{ "` + strings.Repeat("a", 513) + `" 1 }`, expTokens: []interface{}{
348 Delim('{'), strings.Repeat("a", 513),
349 decodeThis{&SyntaxError{"expected colon after object key", 518}},
350 }},
351 {json: `{ "\a" }`, expTokens: []interface{}{
352 Delim('{'),
353 &SyntaxError{"invalid character 'a' in string escape code", 3},
354 }},
355 {json: ` \a`, expTokens: []interface{}{
356 &SyntaxError{"invalid character '\\\\' looking for beginning of value", 1},
Peter Waldschmidt0cf48b42015-04-18 03:23:32 -0400357 }},
358}
359
360func TestDecodeInStream(t *testing.T) {
361
362 for ci, tcase := range tokenStreamCases {
363
364 dec := NewDecoder(strings.NewReader(tcase.json))
365 for i, etk := range tcase.expTokens {
366
367 var tk interface{}
368 var err error
369
370 if dt, ok := etk.(decodeThis); ok {
371 etk = dt.v
372 err = dec.Decode(&tk)
373 } else {
374 tk, err = dec.Token()
375 }
376 if experr, ok := etk.(error); ok {
Michael Fraenkelf1ce59d2017-10-28 20:50:57 -0400377 if err == nil || !reflect.DeepEqual(err, experr) {
378 t.Errorf("case %v: Expected error %#v in %q, but was %#v", ci, experr, tcase.json, err)
Peter Waldschmidt0cf48b42015-04-18 03:23:32 -0400379 }
380 break
381 } else if err == io.EOF {
382 t.Errorf("case %v: Unexpected EOF in %q", ci, tcase.json)
383 break
384 } else if err != nil {
Michael Fraenkelf1ce59d2017-10-28 20:50:57 -0400385 t.Errorf("case %v: Unexpected error '%#v' in %q", ci, err, tcase.json)
Peter Waldschmidt0cf48b42015-04-18 03:23:32 -0400386 break
387 }
388 if !reflect.DeepEqual(tk, etk) {
389 t.Errorf(`case %v: %q @ %v expected %T(%v) was %T(%v)`, ci, tcase.json, i, etk, etk, tk, tk)
390 break
391 }
392 }
393 }
394
395}
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400396
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200397// Test from golang.org/issue/11893
398func TestHTTPDecoding(t *testing.T) {
399 const raw = `{ "foo": "bar" }`
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400400
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200401 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400402 w.Write([]byte(raw))
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200403 }))
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400404 defer ts.Close()
405 res, err := http.Get(ts.URL)
406 if err != nil {
407 log.Fatalf("GET failed: %v", err)
408 }
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200409 defer res.Body.Close()
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400410
411 foo := struct {
412 Foo string
413 }{}
414
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200415 d := NewDecoder(res.Body)
416 err = d.Decode(&foo)
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400417 if err != nil {
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200418 t.Fatalf("Decode: %v", err)
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400419 }
420 if foo.Foo != "bar" {
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200421 t.Errorf("decoded %q; want \"bar\"", foo.Foo)
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400422 }
423
424 // make sure we get the EOF the second time
425 err = d.Decode(&foo)
426 if err != io.EOF {
Brad Fitzpatrickd0729a62015-07-28 07:53:37 +0200427 t.Errorf("err = %v; want io.EOF", err)
Peter Waldschmidt7e70c242015-07-27 21:33:53 -0400428 }
429}