Debian Codegraph @

canonical/json/encode_test.go

raw
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package json
6
7 import (
8 "bytes"
9 "fmt"
10 "math"
11 "reflect"
12 "testing"
13 "unicode"
14 )
15
16 type Optionals struct {
17 Sr string `json:"sr"`
18 So string `json:"so,omitempty"`
19 Sw string `json:"-"`
20
21 Ir int `json:"omitempty"` // actually named omitempty, not an option
22 Io int `json:"io,omitempty"`
23
24 Slr []string `json:"slr,random"`
25 Slo []string `json:"slo,omitempty"`
26
27 Mr map[string]interface{} `json:"mr"`
28 Mo map[string]interface{} `json:",omitempty"`
29
30 Fr float64 `json:"fr"`
31 Fo float64 `json:"fo,omitempty"`
32
33 Br bool `json:"br"`
34 Bo bool `json:"bo,omitempty"`
35
36 Ur uint `json:"ur"`
37 Uo uint `json:"uo,omitempty"`
38
39 Str struct{} `json:"str"`
40 Sto struct{} `json:"sto,omitempty"`
41 }
42
43 var optionalsExpected = `{
44 "sr": "",
45 "omitempty": 0,
46 "slr": null,
47 "mr": {},
48 "fr": 0,
49 "br": false,
50 "ur": 0,
51 "str": {},
52 "sto": {}
53 }`
54
55 func TestOmitEmpty(t *testing.T) {
56 var o Optionals
57 o.Sw = "something"
58 o.Mr = map[string]interface{}{}
59 o.Mo = map[string]interface{}{}
60
61 got, err := MarshalIndent(&o, "", " ")
62 if err != nil {
63 t.Fatal(err)
64 }
65 if got := string(got); got != optionalsExpected {
66 t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected)
67 }
68 }
69
70 type StringTag struct {
71 BoolStr bool `json:",string"`
72 IntStr int64 `json:",string"`
73 StrStr string `json:",string"`
74 }
75
76 var stringTagExpected = `{
77 "BoolStr": "true",
78 "IntStr": "42",
79 "StrStr": "\"xzbit\""
80 }`
81
82 func TestStringTag(t *testing.T) {
83 var s StringTag
84 s.BoolStr = true
85 s.IntStr = 42
86 s.StrStr = "xzbit"
87 got, err := MarshalIndent(&s, "", " ")
88 if err != nil {
89 t.Fatal(err)
90 }
91 if got := string(got); got != stringTagExpected {
92 t.Fatalf(" got: %s\nwant: %s\n", got, stringTagExpected)
93 }
94
95 // Verify that it round-trips.
96 var s2 StringTag
97 err = NewDecoder(bytes.NewReader(got)).Decode(&s2)
98 if err != nil {
99 t.Fatalf("Decode: %v", err)
100 }
101 if !reflect.DeepEqual(s, s2) {
102 t.Fatalf("decode didn't match.\nsource: %#v\nEncoded as:\n%s\ndecode: %#v", s, string(got), s2)
103 }
104 }
105
106 // byte slices are special even if they're renamed types.
107 type renamedByte byte
108 type renamedByteSlice []byte
109 type renamedRenamedByteSlice []renamedByte
110
111 func TestEncodeRenamedByteSlice(t *testing.T) {
112 s := renamedByteSlice("abc")
113 result, err := Marshal(s)
114 if err != nil {
115 t.Fatal(err)
116 }
117 expect := `"YWJj"`
118 if string(result) != expect {
119 t.Errorf(" got %s want %s", result, expect)
120 }
121 r := renamedRenamedByteSlice("abc")
122 result, err = Marshal(r)
123 if err != nil {
124 t.Fatal(err)
125 }
126 if string(result) != expect {
127 t.Errorf(" got %s want %s", result, expect)
128 }
129 }
130
131 var unsupportedValues = []interface{}{
132 math.NaN(),
133 math.Inf(-1),
134 math.Inf(1),
135 }
136
137 func TestUnsupportedValues(t *testing.T) {
138 for _, v := range unsupportedValues {
139 if _, err := Marshal(v); err != nil {
140 if _, ok := err.(*UnsupportedValueError); !ok {
141 t.Errorf("for %v, got %T want UnsupportedValueError", v, err)
142 }
143 } else {
144 t.Errorf("for %v, expected error", v)
145 }
146 }
147 }
148
149 // Ref has Marshaler and Unmarshaler methods with pointer receiver.
150 type Ref int
151
152 func (*Ref) MarshalJSON() ([]byte, error) {
153 return []byte(`"ref"`), nil
154 }
155
156 func (r *Ref) UnmarshalJSON([]byte) error {
157 *r = 12
158 return nil
159 }
160
161 // Val has Marshaler methods with value receiver.
162 type Val int
163
164 func (Val) MarshalJSON() ([]byte, error) {
165 return []byte(`"val"`), nil
166 }
167
168 // RefText has Marshaler and Unmarshaler methods with pointer receiver.
169 type RefText int
170
171 func (*RefText) MarshalText() ([]byte, error) {
172 return []byte(`"ref"`), nil
173 }
174
175 func (r *RefText) UnmarshalText([]byte) error {
176 *r = 13
177 return nil
178 }
179
180 // ValText has Marshaler methods with value receiver.
181 type ValText int
182
183 func (ValText) MarshalText() ([]byte, error) {
184 return []byte(`"val"`), nil
185 }
186
187 func TestRefValMarshal(t *testing.T) {
188 var s = struct {
189 R0 Ref
190 R1 *Ref
191 R2 RefText
192 R3 *RefText
193 V0 Val
194 V1 *Val
195 V2 ValText
196 V3 *ValText
197 }{
198 R0: 12,
199 R1: new(Ref),
200 R2: 14,
201 R3: new(RefText),
202 V0: 13,
203 V1: new(Val),
204 V2: 15,
205 V3: new(ValText),
206 }
207 const want = `{"R0":"ref","R1":"ref","R2":"\"ref\"","R3":"\"ref\"","V0":"val","V1":"val","V2":"\"val\"","V3":"\"val\""}`
208 b, err := Marshal(&s)
209 if err != nil {
210 t.Fatalf("Marshal: %v", err)
211 }
212 if got := string(b); got != want {
213 t.Errorf("got %q, want %q", got, want)
214 }
215 }
216
217 // C implements Marshaler and returns unescaped JSON.
218 type C int
219
220 func (C) MarshalJSON() ([]byte, error) {
221 return []byte(`"<&>"`), nil
222 }
223
224 // CText implements Marshaler and returns unescaped text.
225 type CText int
226
227 func (CText) MarshalText() ([]byte, error) {
228 return []byte(`"<&>"`), nil
229 }
230
231 func TestMarshalerEscaping(t *testing.T) {
232 var c C
233 want := `"\u003c\u0026\u003e"`
234 b, err := Marshal(c)
235 if err != nil {
236 t.Fatalf("Marshal(c): %v", err)
237 }
238 if got := string(b); got != want {
239 t.Errorf("Marshal(c) = %#q, want %#q", got, want)
240 }
241
242 var ct CText
243 want = `"\"\u003c\u0026\u003e\""`
244 b, err = Marshal(ct)
245 if err != nil {
246 t.Fatalf("Marshal(ct): %v", err)
247 }
248 if got := string(b); got != want {
249 t.Errorf("Marshal(ct) = %#q, want %#q", got, want)
250 }
251 }
252
253 type IntType int
254
255 type MyStruct struct {
256 IntType
257 }
258
259 func TestAnonymousNonstruct(t *testing.T) {
260 var i IntType = 11
261 a := MyStruct{i}
262 const want = `{"IntType":11}`
263
264 b, err := Marshal(a)
265 if err != nil {
266 t.Fatalf("Marshal: %v", err)
267 }
268 if got := string(b); got != want {
269 t.Errorf("got %q, want %q", got, want)
270 }
271 }
272
273 type BugA struct {
274 S string
275 }
276
277 type BugB struct {
278 BugA
279 S string
280 }
281
282 type BugC struct {
283 S string
284 }
285
286 // Legal Go: We never use the repeated embedded field (S).
287 type BugX struct {
288 A int
289 BugA
290 BugB
291 }
292
293 // Issue 5245.
294 func TestEmbeddedBug(t *testing.T) {
295 v := BugB{
296 BugA{"A"},
297 "B",
298 }
299 b, err := Marshal(v)
300 if err != nil {
301 t.Fatal("Marshal:", err)
302 }
303 want := `{"S":"B"}`
304 got := string(b)
305 if got != want {
306 t.Fatalf("Marshal: got %s want %s", got, want)
307 }
308 // Now check that the duplicate field, S, does not appear.
309 x := BugX{
310 A: 23,
311 }
312 b, err = Marshal(x)
313 if err != nil {
314 t.Fatal("Marshal:", err)
315 }
316 want = `{"A":23}`
317 got = string(b)
318 if got != want {
319 t.Fatalf("Marshal: got %s want %s", got, want)
320 }
321 }
322
323 type BugD struct { // Same as BugA after tagging.
324 XXX string `json:"S"`
325 }
326
327 // BugD's tagged S field should dominate BugA's.
328 type BugY struct {
329 BugA
330 BugD
331 }
332
333 // Test that a field with a tag dominates untagged fields.
334 func TestTaggedFieldDominates(t *testing.T) {
335 v := BugY{
336 BugA{"BugA"},
337 BugD{"BugD"},
338 }
339 b, err := Marshal(v)
340 if err != nil {
341 t.Fatal("Marshal:", err)
342 }
343 want := `{"S":"BugD"}`
344 got := string(b)
345 if got != want {
346 t.Fatalf("Marshal: got %s want %s", got, want)
347 }
348 }
349
350 // There are no tags here, so S should not appear.
351 type BugZ struct {
352 BugA
353 BugC
354 BugY // Contains a tagged S field through BugD; should not dominate.
355 }
356
357 func TestDuplicatedFieldDisappears(t *testing.T) {
358 v := BugZ{
359 BugA{"BugA"},
360 BugC{"BugC"},
361 BugY{
362 BugA{"nested BugA"},
363 BugD{"nested BugD"},
364 },
365 }
366 b, err := Marshal(v)
367 if err != nil {
368 t.Fatal("Marshal:", err)
369 }
370 want := `{}`
371 got := string(b)
372 if got != want {
373 t.Fatalf("Marshal: got %s want %s", got, want)
374 }
375 }
376
377 func TestStringBytes(t *testing.T) {
378 // Test that encodeState.stringBytes and encodeState.string use the same encoding.
379 es := &encodeState{}
380 var r []rune
381 for i := '\u0000'; i <= unicode.MaxRune; i++ {
382 r = append(r, i)
383 }
384 s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
385 es.string(s)
386
387 esBytes := &encodeState{}
388 esBytes.stringBytes([]byte(s))
389
390 enc := es.Buffer.String()
391 encBytes := esBytes.Buffer.String()
392 if enc != encBytes {
393 i := 0
394 for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
395 i++
396 }
397 enc = enc[i:]
398 encBytes = encBytes[i:]
399 i = 0
400 for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
401 i++
402 }
403 enc = enc[:len(enc)-i]
404 encBytes = encBytes[:len(encBytes)-i]
405
406 if len(enc) > 20 {
407 enc = enc[:20] + "..."
408 }
409 if len(encBytes) > 20 {
410 encBytes = encBytes[:20] + "..."
411 }
412
413 t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
414 }
415 }
416
417 func TestIssue6458(t *testing.T) {
418 type Foo struct {
419 M RawMessage
420 }
421 x := Foo{RawMessage(`"foo"`)}
422
423 b, err := Marshal(&x)
424 if err != nil {
425 t.Fatal(err)
426 }
427 if want := `{"M":"foo"}`; string(b) != want {
428 t.Errorf("Marshal(&x) = %#q; want %#q", b, want)
429 }
430
431 b, err = Marshal(x)
432 if err != nil {
433 t.Fatal(err)
434 }
435
436 if want := `{"M":"ImZvbyI="}`; string(b) != want {
437 t.Errorf("Marshal(x) = %#q; want %#q", b, want)
438 }
439 }
440
441 func TestIssue10281(t *testing.T) {
442 type Foo struct {
443 N Number
444 }
445 x := Foo{Number(`invalid`)}
446
447 b, err := Marshal(&x)
448 if err == nil {
449 t.Errorf("Marshal(&x) = %#q; want error", b)
450 }
451 }
452
453 func TestHTMLEscape(t *testing.T) {
454 var b, want bytes.Buffer
455 m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}`
456 want.Write([]byte(`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
457 HTMLEscape(&b, []byte(m))
458 if !bytes.Equal(b.Bytes(), want.Bytes()) {
459 t.Errorf("HTMLEscape(&b, []byte(m)) = %s; want %s", b.Bytes(), want.Bytes())
460 }
461 }
462
463 // golang.org/issue/8582
464 func TestEncodePointerString(t *testing.T) {
465 type stringPointer struct {
466 N *int64 `json:"n,string"`
467 }
468 var n int64 = 42
469 b, err := Marshal(stringPointer{N: &n})
470 if err != nil {
471 t.Fatalf("Marshal: %v", err)
472 }
473 if got, want := string(b), `{"n":"42"}`; got != want {
474 t.Errorf("Marshal = %s, want %s", got, want)
475 }
476 var back stringPointer
477 err = Unmarshal(b, &back)
478 if err != nil {
479 t.Fatalf("Unmarshal: %v", err)
480 }
481 if back.N == nil {
482 t.Fatalf("Unmarshalled nil N field")
483 }
484 if *back.N != 42 {
485 t.Fatalf("*N = %d; want 42", *back.N)
486 }
487 }
488
489 var encodeStringTests = []struct {
490 in string
491 out string
492 }{
493 {"\x00", `"\u0000"`},
494 {"\x01", `"\u0001"`},
495 {"\x02", `"\u0002"`},
496 {"\x03", `"\u0003"`},
497 {"\x04", `"\u0004"`},
498 {"\x05", `"\u0005"`},
499 {"\x06", `"\u0006"`},
500 {"\x07", `"\u0007"`},
501 {"\x08", `"\u0008"`},
502 {"\x09", `"\t"`},
503 {"\x0a", `"\n"`},
504 {"\x0b", `"\u000b"`},
505 {"\x0c", `"\u000c"`},
506 {"\x0d", `"\r"`},
507 {"\x0e", `"\u000e"`},
508 {"\x0f", `"\u000f"`},
509 {"\x10", `"\u0010"`},
510 {"\x11", `"\u0011"`},
511 {"\x12", `"\u0012"`},
512 {"\x13", `"\u0013"`},
513 {"\x14", `"\u0014"`},
514 {"\x15", `"\u0015"`},
515 {"\x16", `"\u0016"`},
516 {"\x17", `"\u0017"`},
517 {"\x18", `"\u0018"`},
518 {"\x19", `"\u0019"`},
519 {"\x1a", `"\u001a"`},
520 {"\x1b", `"\u001b"`},
521 {"\x1c", `"\u001c"`},
522 {"\x1d", `"\u001d"`},
523 {"\x1e", `"\u001e"`},
524 {"\x1f", `"\u001f"`},
525 }
526
527 func TestEncodeString(t *testing.T) {
528 for _, tt := range encodeStringTests {
529 b, err := Marshal(tt.in)
530 if err != nil {
531 t.Errorf("Marshal(%q): %v", tt.in, err)
532 continue
533 }
534 out := string(b)
535 if out != tt.out {
536 t.Errorf("Marshal(%q) = %#q, want %#q", tt.in, out, tt.out)
537 }
538 }
539 }
540
541 type CanonicalTestStruct struct {
542 S string
543 F float64
544 I int
545 E *CanonicalTestStruct
546 }
547
548 func (s *CanonicalTestStruct) String() string {
549 var e interface{} = s.E
550 if s.E == nil {
551 e = "nil"
552 }
553 return fmt.Sprintf("{S:%q F:%v I:%v E:%v}", s.S, s.F, s.I, e)
554 }
555
556 var encodeCanonicalTests = []struct {
557 in interface{}
558 out string
559 expectErr bool
560 }{
561 {nil, `null`, false},
562 {&CanonicalTestStruct{}, `{"E":null,"F":0,"I":0,"S":""}`, false},
563 {&CanonicalTestStruct{F: 1.0}, `{"E":null,"F":1,"I":0,"S":""}`, false},
564 // error out on floating numbers
565 {&CanonicalTestStruct{F: 1.2}, ``, true},
566 {&CanonicalTestStruct{S: "foo", E: &CanonicalTestStruct{I: 42}}, `{"E":{"E":null,"F":0,"I":42,"S":""},"F":0,"I":0,"S":"foo"}`, false},
567 // only escape \ and " and keep any other character as-is
568 {"\u0090 \t \\ \n \"", "\"\u0090 \t \\\\ \n \\\"\"", false},
569 }
570
571 func TestEncodeCanonicalStruct(t *testing.T) {
572 for _, tt := range encodeCanonicalTests {
573 b, err := MarshalCanonical(tt.in)
574 if err != nil {
575 if !tt.expectErr {
576 t.Errorf("MarshalCanonical(%#v) = error(%v), want %s", tt.in, err, tt.out)
577 }
578 continue
579 } else if tt.expectErr {
580 t.Errorf("MarshalCanonical(%#v) expects an error", tt.in)
581 continue
582 }
583 out := string(b)
584 if out != tt.out {
585 t.Errorf("MarshalCanonical(%#v) = %q, want %q", tt.in, out, tt.out)
586 }
587 }
588 }
589
590 func TestCanonicalFloatError(t *testing.T) {
591 input := struct{ A float64 }{1.1}
592
593 _, err := MarshalCanonical(input)
594 if err == nil {
595 t.Errorf("want float error, got nil")
596 }
597 }
598
599 func TestCanonicalFloatAsInt(t *testing.T) {
600 in := struct{ A float64 }{1234567}
601
602 b, err := MarshalCanonical(in)
603 if err != nil {
604 t.Fatalf("Marshal(%q): %v", in, err)
605 }
606 out := string(b)
607 expected := `{"A":1234567}`
608 if out != expected {
609 t.Errorf("Marshal(%q) = %#q, want %#q", in, out, expected)
610 }
611 }