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
5package json
6
7import (
8	"bytes"
9	"encoding"
10	"fmt"
11	"log"
12	"math"
13	"reflect"
14	"regexp"
15	"strconv"
16	"testing"
17	"unicode"
18)
19
20type Optionals struct {
21	Sr string `json:"sr"`
22	So string `json:"so,omitempty"`
23	Sw string `json:"-"`
24
25	Ir int `json:"omitempty"` // actually named omitempty, not an option
26	Io int `json:"io,omitempty"`
27
28	Slr []string `json:"slr,random"`
29	Slo []string `json:"slo,omitempty"`
30
31	Mr map[string]interface{} `json:"mr"`
32	Mo map[string]interface{} `json:",omitempty"`
33
34	Fr float64 `json:"fr"`
35	Fo float64 `json:"fo,omitempty"`
36
37	Br bool `json:"br"`
38	Bo bool `json:"bo,omitempty"`
39
40	Ur uint `json:"ur"`
41	Uo uint `json:"uo,omitempty"`
42
43	Str struct{} `json:"str"`
44	Sto struct{} `json:"sto,omitempty"`
45}
46
47var optionalsExpected = `{
48 "sr": "",
49 "omitempty": 0,
50 "slr": null,
51 "mr": {},
52 "fr": 0,
53 "br": false,
54 "ur": 0,
55 "str": {},
56 "sto": {}
57}`
58
59func TestOmitEmpty(t *testing.T) {
60	var o Optionals
61	o.Sw = "something"
62	o.Mr = map[string]interface{}{}
63	o.Mo = map[string]interface{}{}
64
65	got, err := MarshalIndent(&o, "", " ")
66	if err != nil {
67		t.Fatal(err)
68	}
69	if got := string(got); got != optionalsExpected {
70		t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected)
71	}
72}
73
74type StringTag struct {
75	BoolStr    bool    `json:",string"`
76	IntStr     int64   `json:",string"`
77	UintptrStr uintptr `json:",string"`
78	StrStr     string  `json:",string"`
79	NumberStr  Number  `json:",string"`
80}
81
82func TestRoundtripStringTag(t *testing.T) {
83	tests := []struct {
84		name string
85		in   StringTag
86		want string // empty to just test that we roundtrip
87	}{
88		{
89			name: "AllTypes",
90			in: StringTag{
91				BoolStr:    true,
92				IntStr:     42,
93				UintptrStr: 44,
94				StrStr:     "xzbit",
95				NumberStr:  "46",
96			},
97			want: `{
98				"BoolStr": "true",
99				"IntStr": "42",
100				"UintptrStr": "44",
101				"StrStr": "\"xzbit\"",
102				"NumberStr": "46"
103			}`,
104		},
105		{
106			// See golang.org/issues/38173.
107			name: "StringDoubleEscapes",
108			in: StringTag{
109				StrStr:    "\b\f\n\r\t\"\\",
110				NumberStr: "0", // just to satisfy the roundtrip
111			},
112			want: `{
113				"BoolStr": "false",
114				"IntStr": "0",
115				"UintptrStr": "0",
116				"StrStr": "\"\\u0008\\u000c\\n\\r\\t\\\"\\\\\"",
117				"NumberStr": "0"
118			}`,
119		},
120	}
121	for _, test := range tests {
122		t.Run(test.name, func(t *testing.T) {
123			// Indent with a tab prefix to make the multi-line string
124			// literals in the table nicer to read.
125			got, err := MarshalIndent(&test.in, "\t\t\t", "\t")
126			if err != nil {
127				t.Fatal(err)
128			}
129			if got := string(got); got != test.want {
130				t.Fatalf(" got: %s\nwant: %s\n", got, test.want)
131			}
132
133			// Verify that it round-trips.
134			var s2 StringTag
135			if err := Unmarshal(got, &s2); err != nil {
136				t.Fatalf("Decode: %v", err)
137			}
138			if !reflect.DeepEqual(test.in, s2) {
139				t.Fatalf("decode didn't match.\nsource: %#v\nEncoded as:\n%s\ndecode: %#v", test.in, string(got), s2)
140			}
141		})
142	}
143}
144
145// byte slices are special even if they're renamed types.
146type renamedByte byte
147type renamedByteSlice []byte
148type renamedRenamedByteSlice []renamedByte
149
150func TestEncodeRenamedByteSlice(t *testing.T) {
151	s := renamedByteSlice("abc")
152	result, err := Marshal(s)
153	if err != nil {
154		t.Fatal(err)
155	}
156	expect := `"YWJj"`
157	if string(result) != expect {
158		t.Errorf(" got %s want %s", result, expect)
159	}
160	r := renamedRenamedByteSlice("abc")
161	result, err = Marshal(r)
162	if err != nil {
163		t.Fatal(err)
164	}
165	if string(result) != expect {
166		t.Errorf(" got %s want %s", result, expect)
167	}
168}
169
170type SamePointerNoCycle struct {
171	Ptr1, Ptr2 *SamePointerNoCycle
172}
173
174var samePointerNoCycle = &SamePointerNoCycle{}
175
176type PointerCycle struct {
177	Ptr *PointerCycle
178}
179
180var pointerCycle = &PointerCycle{}
181
182type PointerCycleIndirect struct {
183	Ptrs []interface{}
184}
185
186type RecursiveSlice []RecursiveSlice
187
188var (
189	pointerCycleIndirect = &PointerCycleIndirect{}
190	mapCycle             = make(map[string]interface{})
191	sliceCycle           = []interface{}{nil}
192	sliceNoCycle         = []interface{}{nil, nil}
193	recursiveSliceCycle  = []RecursiveSlice{nil}
194)
195
196func init() {
197	ptr := &SamePointerNoCycle{}
198	samePointerNoCycle.Ptr1 = ptr
199	samePointerNoCycle.Ptr2 = ptr
200
201	pointerCycle.Ptr = pointerCycle
202	pointerCycleIndirect.Ptrs = []interface{}{pointerCycleIndirect}
203
204	mapCycle["x"] = mapCycle
205	sliceCycle[0] = sliceCycle
206	sliceNoCycle[1] = sliceNoCycle[:1]
207	for i := startDetectingCyclesAfter; i > 0; i-- {
208		sliceNoCycle = []interface{}{sliceNoCycle}
209	}
210	recursiveSliceCycle[0] = recursiveSliceCycle
211}
212
213func TestSamePointerNoCycle(t *testing.T) {
214	if _, err := Marshal(samePointerNoCycle); err != nil {
215		t.Fatalf("unexpected error: %v", err)
216	}
217}
218
219func TestSliceNoCycle(t *testing.T) {
220	if _, err := Marshal(sliceNoCycle); err != nil {
221		t.Fatalf("unexpected error: %v", err)
222	}
223}
224
225var unsupportedValues = []interface{}{
226	math.NaN(),
227	math.Inf(-1),
228	math.Inf(1),
229	pointerCycle,
230	pointerCycleIndirect,
231	mapCycle,
232	sliceCycle,
233	recursiveSliceCycle,
234}
235
236func TestUnsupportedValues(t *testing.T) {
237	for _, v := range unsupportedValues {
238		if _, err := Marshal(v); err != nil {
239			if _, ok := err.(*UnsupportedValueError); !ok {
240				t.Errorf("for %v, got %T want UnsupportedValueError", v, err)
241			}
242		} else {
243			t.Errorf("for %v, expected error", v)
244		}
245	}
246}
247
248// Ref has Marshaler and Unmarshaler methods with pointer receiver.
249type Ref int
250
251func (*Ref) MarshalJSON() ([]byte, error) {
252	return []byte(`"ref"`), nil
253}
254
255func (r *Ref) UnmarshalJSON([]byte) error {
256	*r = 12
257	return nil
258}
259
260// Val has Marshaler methods with value receiver.
261type Val int
262
263func (Val) MarshalJSON() ([]byte, error) {
264	return []byte(`"val"`), nil
265}
266
267// RefText has Marshaler and Unmarshaler methods with pointer receiver.
268type RefText int
269
270func (*RefText) MarshalText() ([]byte, error) {
271	return []byte(`"ref"`), nil
272}
273
274func (r *RefText) UnmarshalText([]byte) error {
275	*r = 13
276	return nil
277}
278
279// ValText has Marshaler methods with value receiver.
280type ValText int
281
282func (ValText) MarshalText() ([]byte, error) {
283	return []byte(`"val"`), nil
284}
285
286func TestRefValMarshal(t *testing.T) {
287	var s = struct {
288		R0 Ref
289		R1 *Ref
290		R2 RefText
291		R3 *RefText
292		V0 Val
293		V1 *Val
294		V2 ValText
295		V3 *ValText
296	}{
297		R0: 12,
298		R1: new(Ref),
299		R2: 14,
300		R3: new(RefText),
301		V0: 13,
302		V1: new(Val),
303		V2: 15,
304		V3: new(ValText),
305	}
306	const want = `{"R0":"ref","R1":"ref","R2":"\"ref\"","R3":"\"ref\"","V0":"val","V1":"val","V2":"\"val\"","V3":"\"val\""}`
307	b, err := Marshal(&s)
308	if err != nil {
309		t.Fatalf("Marshal: %v", err)
310	}
311	if got := string(b); got != want {
312		t.Errorf("got %q, want %q", got, want)
313	}
314}
315
316// C implements Marshaler and returns unescaped JSON.
317type C int
318
319func (C) MarshalJSON() ([]byte, error) {
320	return []byte(`"<&>"`), nil
321}
322
323// CText implements Marshaler and returns unescaped text.
324type CText int
325
326func (CText) MarshalText() ([]byte, error) {
327	return []byte(`"<&>"`), nil
328}
329
330func TestMarshalerEscaping(t *testing.T) {
331	var c C
332	want := `"\u003c\u0026\u003e"`
333	b, err := Marshal(c)
334	if err != nil {
335		t.Fatalf("Marshal(c): %v", err)
336	}
337	if got := string(b); got != want {
338		t.Errorf("Marshal(c) = %#q, want %#q", got, want)
339	}
340
341	var ct CText
342	want = `"\"\u003c\u0026\u003e\""`
343	b, err = Marshal(ct)
344	if err != nil {
345		t.Fatalf("Marshal(ct): %v", err)
346	}
347	if got := string(b); got != want {
348		t.Errorf("Marshal(ct) = %#q, want %#q", got, want)
349	}
350}
351
352func TestAnonymousFields(t *testing.T) {
353	tests := []struct {
354		label     string             // Test name
355		makeInput func() interface{} // Function to create input value
356		want      string             // Expected JSON output
357	}{{
358		// Both S1 and S2 have a field named X. From the perspective of S,
359		// it is ambiguous which one X refers to.
360		// This should not serialize either field.
361		label: "AmbiguousField",
362		makeInput: func() interface{} {
363			type (
364				S1 struct{ x, X int }
365				S2 struct{ x, X int }
366				S  struct {
367					S1
368					S2
369				}
370			)
371			return S{S1{1, 2}, S2{3, 4}}
372		},
373		want: `{}`,
374	}, {
375		label: "DominantField",
376		// Both S1 and S2 have a field named X, but since S has an X field as
377		// well, it takes precedence over S1.X and S2.X.
378		makeInput: func() interface{} {
379			type (
380				S1 struct{ x, X int }
381				S2 struct{ x, X int }
382				S  struct {
383					S1
384					S2
385					x, X int
386				}
387			)
388			return S{S1{1, 2}, S2{3, 4}, 5, 6}
389		},
390		want: `{"X":6}`,
391	}, {
392		// Unexported embedded field of non-struct type should not be serialized.
393		label: "UnexportedEmbeddedInt",
394		makeInput: func() interface{} {
395			type (
396				myInt int
397				S     struct{ myInt }
398			)
399			return S{5}
400		},
401		want: `{}`,
402	}, {
403		// Exported embedded field of non-struct type should be serialized.
404		label: "ExportedEmbeddedInt",
405		makeInput: func() interface{} {
406			type (
407				MyInt int
408				S     struct{ MyInt }
409			)
410			return S{5}
411		},
412		want: `{"MyInt":5}`,
413	}, {
414		// Unexported embedded field of pointer to non-struct type
415		// should not be serialized.
416		label: "UnexportedEmbeddedIntPointer",
417		makeInput: func() interface{} {
418			type (
419				myInt int
420				S     struct{ *myInt }
421			)
422			s := S{new(myInt)}
423			*s.myInt = 5
424			return s
425		},
426		want: `{}`,
427	}, {
428		// Exported embedded field of pointer to non-struct type
429		// should be serialized.
430		label: "ExportedEmbeddedIntPointer",
431		makeInput: func() interface{} {
432			type (
433				MyInt int
434				S     struct{ *MyInt }
435			)
436			s := S{new(MyInt)}
437			*s.MyInt = 5
438			return s
439		},
440		want: `{"MyInt":5}`,
441	}, {
442		// Exported fields of embedded structs should have their
443		// exported fields be serialized regardless of whether the struct types
444		// themselves are exported.
445		label: "EmbeddedStruct",
446		makeInput: func() interface{} {
447			type (
448				s1 struct{ x, X int }
449				S2 struct{ y, Y int }
450				S  struct {
451					s1
452					S2
453				}
454			)
455			return S{s1{1, 2}, S2{3, 4}}
456		},
457		want: `{"X":2,"Y":4}`,
458	}, {
459		// Exported fields of pointers to embedded structs should have their
460		// exported fields be serialized regardless of whether the struct types
461		// themselves are exported.
462		label: "EmbeddedStructPointer",
463		makeInput: func() interface{} {
464			type (
465				s1 struct{ x, X int }
466				S2 struct{ y, Y int }
467				S  struct {
468					*s1
469					*S2
470				}
471			)
472			return S{&s1{1, 2}, &S2{3, 4}}
473		},
474		want: `{"X":2,"Y":4}`,
475	}, {
476		// Exported fields on embedded unexported structs at multiple levels
477		// of nesting should still be serialized.
478		label: "NestedStructAndInts",
479		makeInput: func() interface{} {
480			type (
481				MyInt1 int
482				MyInt2 int
483				myInt  int
484				s2     struct {
485					MyInt2
486					myInt
487				}
488				s1 struct {
489					MyInt1
490					myInt
491					s2
492				}
493				S struct {
494					s1
495					myInt
496				}
497			)
498			return S{s1{1, 2, s2{3, 4}}, 6}
499		},
500		want: `{"MyInt1":1,"MyInt2":3}`,
501	}, {
502		// If an anonymous struct pointer field is nil, we should ignore
503		// the embedded fields behind it. Not properly doing so may
504		// result in the wrong output or reflect panics.
505		label: "EmbeddedFieldBehindNilPointer",
506		makeInput: func() interface{} {
507			type (
508				S2 struct{ Field string }
509				S  struct{ *S2 }
510			)
511			return S{}
512		},
513		want: `{}`,
514	}}
515
516	for _, tt := range tests {
517		t.Run(tt.label, func(t *testing.T) {
518			b, err := Marshal(tt.makeInput())
519			if err != nil {
520				t.Fatalf("Marshal() = %v, want nil error", err)
521			}
522			if string(b) != tt.want {
523				t.Fatalf("Marshal() = %q, want %q", b, tt.want)
524			}
525		})
526	}
527}
528
529type BugA struct {
530	S string
531}
532
533type BugB struct {
534	BugA
535	S string
536}
537
538type BugC struct {
539	S string
540}
541
542// Legal Go: We never use the repeated embedded field (S).
543type BugX struct {
544	A int
545	BugA
546	BugB
547}
548
549// golang.org/issue/16042.
550// Even if a nil interface value is passed in, as long as
551// it implements Marshaler, it should be marshaled.
552type nilJSONMarshaler string
553
554func (nm *nilJSONMarshaler) MarshalJSON() ([]byte, error) {
555	if nm == nil {
556		return Marshal("0zenil0")
557	}
558	return Marshal("zenil:" + string(*nm))
559}
560
561// golang.org/issue/34235.
562// Even if a nil interface value is passed in, as long as
563// it implements encoding.TextMarshaler, it should be marshaled.
564type nilTextMarshaler string
565
566func (nm *nilTextMarshaler) MarshalText() ([]byte, error) {
567	if nm == nil {
568		return []byte("0zenil0"), nil
569	}
570	return []byte("zenil:" + string(*nm)), nil
571}
572
573// See golang.org/issue/16042 and golang.org/issue/34235.
574func TestNilMarshal(t *testing.T) {
575	testCases := []struct {
576		v    interface{}
577		want string
578	}{
579		{v: nil, want: `null`},
580		{v: new(float64), want: `0`},
581		{v: []interface{}(nil), want: `null`},
582		{v: []string(nil), want: `null`},
583		{v: map[string]string(nil), want: `null`},
584		{v: []byte(nil), want: `null`},
585		{v: struct{ M string }{"gopher"}, want: `{"M":"gopher"}`},
586		{v: struct{ M Marshaler }{}, want: `{"M":null}`},
587		{v: struct{ M Marshaler }{(*nilJSONMarshaler)(nil)}, want: `{"M":"0zenil0"}`},
588		{v: struct{ M interface{} }{(*nilJSONMarshaler)(nil)}, want: `{"M":null}`},
589		{v: struct{ M encoding.TextMarshaler }{}, want: `{"M":null}`},
590		{v: struct{ M encoding.TextMarshaler }{(*nilTextMarshaler)(nil)}, want: `{"M":"0zenil0"}`},
591		{v: struct{ M interface{} }{(*nilTextMarshaler)(nil)}, want: `{"M":null}`},
592	}
593
594	for _, tt := range testCases {
595		out, err := Marshal(tt.v)
596		if err != nil || string(out) != tt.want {
597			t.Errorf("Marshal(%#v) = %#q, %#v, want %#q, nil", tt.v, out, err, tt.want)
598			continue
599		}
600	}
601}
602
603// Issue 5245.
604func TestEmbeddedBug(t *testing.T) {
605	v := BugB{
606		BugA{"A"},
607		"B",
608	}
609	b, err := Marshal(v)
610	if err != nil {
611		t.Fatal("Marshal:", err)
612	}
613	want := `{"S":"B"}`
614	got := string(b)
615	if got != want {
616		t.Fatalf("Marshal: got %s want %s", got, want)
617	}
618	// Now check that the duplicate field, S, does not appear.
619	x := BugX{
620		A: 23,
621	}
622	b, err = Marshal(x)
623	if err != nil {
624		t.Fatal("Marshal:", err)
625	}
626	want = `{"A":23}`
627	got = string(b)
628	if got != want {
629		t.Fatalf("Marshal: got %s want %s", got, want)
630	}
631}
632
633type BugD struct { // Same as BugA after tagging.
634	XXX string `json:"S"`
635}
636
637// BugD's tagged S field should dominate BugA's.
638type BugY struct {
639	BugA
640	BugD
641}
642
643// Test that a field with a tag dominates untagged fields.
644func TestTaggedFieldDominates(t *testing.T) {
645	v := BugY{
646		BugA{"BugA"},
647		BugD{"BugD"},
648	}
649	b, err := Marshal(v)
650	if err != nil {
651		t.Fatal("Marshal:", err)
652	}
653	want := `{"S":"BugD"}`
654	got := string(b)
655	if got != want {
656		t.Fatalf("Marshal: got %s want %s", got, want)
657	}
658}
659
660// There are no tags here, so S should not appear.
661type BugZ struct {
662	BugA
663	BugC
664	BugY // Contains a tagged S field through BugD; should not dominate.
665}
666
667func TestDuplicatedFieldDisappears(t *testing.T) {
668	v := BugZ{
669		BugA{"BugA"},
670		BugC{"BugC"},
671		BugY{
672			BugA{"nested BugA"},
673			BugD{"nested BugD"},
674		},
675	}
676	b, err := Marshal(v)
677	if err != nil {
678		t.Fatal("Marshal:", err)
679	}
680	want := `{}`
681	got := string(b)
682	if got != want {
683		t.Fatalf("Marshal: got %s want %s", got, want)
684	}
685}
686
687func TestStringBytes(t *testing.T) {
688	t.Parallel()
689	// Test that encodeState.stringBytes and encodeState.string use the same encoding.
690	var r []rune
691	for i := '\u0000'; i <= unicode.MaxRune; i++ {
692		if testing.Short() && i > 1000 {
693			i = unicode.MaxRune
694		}
695		r = append(r, i)
696	}
697	s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
698
699	for _, escapeHTML := range []bool{true, false} {
700		es := &encodeState{}
701		es.string(s, escapeHTML)
702
703		esBytes := &encodeState{}
704		esBytes.stringBytes([]byte(s), escapeHTML)
705
706		enc := es.Buffer.String()
707		encBytes := esBytes.Buffer.String()
708		if enc != encBytes {
709			i := 0
710			for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
711				i++
712			}
713			enc = enc[i:]
714			encBytes = encBytes[i:]
715			i = 0
716			for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
717				i++
718			}
719			enc = enc[:len(enc)-i]
720			encBytes = encBytes[:len(encBytes)-i]
721
722			if len(enc) > 20 {
723				enc = enc[:20] + "..."
724			}
725			if len(encBytes) > 20 {
726				encBytes = encBytes[:20] + "..."
727			}
728
729			t.Errorf("with escapeHTML=%t, encodings differ at %#q vs %#q",
730				escapeHTML, enc, encBytes)
731		}
732	}
733}
734
735func TestIssue10281(t *testing.T) {
736	type Foo struct {
737		N Number
738	}
739	x := Foo{Number(`invalid`)}
740
741	b, err := Marshal(&x)
742	if err == nil {
743		t.Errorf("Marshal(&x) = %#q; want error", b)
744	}
745}
746
747func TestHTMLEscape(t *testing.T) {
748	var b, want bytes.Buffer
749	m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}`
750	want.Write([]byte(`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
751	HTMLEscape(&b, []byte(m))
752	if !bytes.Equal(b.Bytes(), want.Bytes()) {
753		t.Errorf("HTMLEscape(&b, []byte(m)) = %s; want %s", b.Bytes(), want.Bytes())
754	}
755}
756
757// golang.org/issue/8582
758func TestEncodePointerString(t *testing.T) {
759	type stringPointer struct {
760		N *int64 `json:"n,string"`
761	}
762	var n int64 = 42
763	b, err := Marshal(stringPointer{N: &n})
764	if err != nil {
765		t.Fatalf("Marshal: %v", err)
766	}
767	if got, want := string(b), `{"n":"42"}`; got != want {
768		t.Errorf("Marshal = %s, want %s", got, want)
769	}
770	var back stringPointer
771	err = Unmarshal(b, &back)
772	if err != nil {
773		t.Fatalf("Unmarshal: %v", err)
774	}
775	if back.N == nil {
776		t.Fatalf("Unmarshaled nil N field")
777	}
778	if *back.N != 42 {
779		t.Fatalf("*N = %d; want 42", *back.N)
780	}
781}
782
783var encodeStringTests = []struct {
784	in  string
785	out string
786}{
787	{"\x00", `"\u0000"`},
788	{"\x01", `"\u0001"`},
789	{"\x02", `"\u0002"`},
790	{"\x03", `"\u0003"`},
791	{"\x04", `"\u0004"`},
792	{"\x05", `"\u0005"`},
793	{"\x06", `"\u0006"`},
794	{"\x07", `"\u0007"`},
795	{"\x08", `"\u0008"`},
796	{"\x09", `"\t"`},
797	{"\x0a", `"\n"`},
798	{"\x0b", `"\u000b"`},
799	{"\x0c", `"\u000c"`},
800	{"\x0d", `"\r"`},
801	{"\x0e", `"\u000e"`},
802	{"\x0f", `"\u000f"`},
803	{"\x10", `"\u0010"`},
804	{"\x11", `"\u0011"`},
805	{"\x12", `"\u0012"`},
806	{"\x13", `"\u0013"`},
807	{"\x14", `"\u0014"`},
808	{"\x15", `"\u0015"`},
809	{"\x16", `"\u0016"`},
810	{"\x17", `"\u0017"`},
811	{"\x18", `"\u0018"`},
812	{"\x19", `"\u0019"`},
813	{"\x1a", `"\u001a"`},
814	{"\x1b", `"\u001b"`},
815	{"\x1c", `"\u001c"`},
816	{"\x1d", `"\u001d"`},
817	{"\x1e", `"\u001e"`},
818	{"\x1f", `"\u001f"`},
819}
820
821func TestEncodeString(t *testing.T) {
822	for _, tt := range encodeStringTests {
823		b, err := Marshal(tt.in)
824		if err != nil {
825			t.Errorf("Marshal(%q): %v", tt.in, err)
826			continue
827		}
828		out := string(b)
829		if out != tt.out {
830			t.Errorf("Marshal(%q) = %#q, want %#q", tt.in, out, tt.out)
831		}
832	}
833}
834
835type jsonbyte byte
836
837func (b jsonbyte) MarshalJSON() ([]byte, error) { return tenc(`{"JB":%d}`, b) }
838
839type textbyte byte
840
841func (b textbyte) MarshalText() ([]byte, error) { return tenc(`TB:%d`, b) }
842
843type jsonint int
844
845func (i jsonint) MarshalJSON() ([]byte, error) { return tenc(`{"JI":%d}`, i) }
846
847type textint int
848
849func (i textint) MarshalText() ([]byte, error) { return tenc(`TI:%d`, i) }
850
851func tenc(format string, a ...interface{}) ([]byte, error) {
852	var buf bytes.Buffer
853	fmt.Fprintf(&buf, format, a...)
854	return buf.Bytes(), nil
855}
856
857// Issue 13783
858func TestEncodeBytekind(t *testing.T) {
859	testdata := []struct {
860		data interface{}
861		want string
862	}{
863		{byte(7), "7"},
864		{jsonbyte(7), `{"JB":7}`},
865		{textbyte(4), `"TB:4"`},
866		{jsonint(5), `{"JI":5}`},
867		{textint(1), `"TI:1"`},
868		{[]byte{0, 1}, `"AAE="`},
869		{[]jsonbyte{0, 1}, `[{"JB":0},{"JB":1}]`},
870		{[][]jsonbyte{{0, 1}, {3}}, `[[{"JB":0},{"JB":1}],[{"JB":3}]]`},
871		{[]textbyte{2, 3}, `["TB:2","TB:3"]`},
872		{[]jsonint{5, 4}, `[{"JI":5},{"JI":4}]`},
873		{[]textint{9, 3}, `["TI:9","TI:3"]`},
874		{[]int{9, 3}, `[9,3]`},
875	}
876	for _, d := range testdata {
877		js, err := Marshal(d.data)
878		if err != nil {
879			t.Error(err)
880			continue
881		}
882		got, want := string(js), d.want
883		if got != want {
884			t.Errorf("got %s, want %s", got, want)
885		}
886	}
887}
888
889func TestTextMarshalerMapKeysAreSorted(t *testing.T) {
890	b, err := Marshal(map[unmarshalerText]int{
891		{"x", "y"}: 1,
892		{"y", "x"}: 2,
893		{"a", "z"}: 3,
894		{"z", "a"}: 4,
895	})
896	if err != nil {
897		t.Fatalf("Failed to Marshal text.Marshaler: %v", err)
898	}
899	const want = `{"a:z":3,"x:y":1,"y:x":2,"z:a":4}`
900	if string(b) != want {
901		t.Errorf("Marshal map with text.Marshaler keys: got %#q, want %#q", b, want)
902	}
903}
904
905// https://golang.org/issue/33675
906func TestNilMarshalerTextMapKey(t *testing.T) {
907	b, err := Marshal(map[*unmarshalerText]int{
908		(*unmarshalerText)(nil): 1,
909		{"A", "B"}:              2,
910	})
911	if err != nil {
912		t.Fatalf("Failed to Marshal *text.Marshaler: %v", err)
913	}
914	const want = `{"":1,"A:B":2}`
915	if string(b) != want {
916		t.Errorf("Marshal map with *text.Marshaler keys: got %#q, want %#q", b, want)
917	}
918}
919
920var re = regexp.MustCompile
921
922// syntactic checks on form of marshaled floating point numbers.
923var badFloatREs = []*regexp.Regexp{
924	re(`p`),                     // no binary exponential notation
925	re(`^\+`),                   // no leading + sign
926	re(`^-?0[^.]`),              // no unnecessary leading zeros
927	re(`^-?\.`),                 // leading zero required before decimal point
928	re(`\.(e|$)`),               // no trailing decimal
929	re(`\.[0-9]+0(e|$)`),        // no trailing zero in fraction
930	re(`^-?(0|[0-9]{2,})\..*e`), // exponential notation must have normalized mantissa
931	re(`e[0-9]`),                // positive exponent must be signed
932	re(`e[+-]0`),                // exponent must not have leading zeros
933	re(`e-[1-6]$`),              // not tiny enough for exponential notation
934	re(`e+(.|1.|20)$`),          // not big enough for exponential notation
935	re(`^-?0\.0000000`),         // too tiny, should use exponential notation
936	re(`^-?[0-9]{22}`),          // too big, should use exponential notation
937	re(`[1-9][0-9]{16}[1-9]`),   // too many significant digits in integer
938	re(`[1-9][0-9.]{17}[1-9]`),  // too many significant digits in decimal
939	// below here for float32 only
940	re(`[1-9][0-9]{8}[1-9]`),  // too many significant digits in integer
941	re(`[1-9][0-9.]{9}[1-9]`), // too many significant digits in decimal
942}
943
944func TestMarshalFloat(t *testing.T) {
945	t.Parallel()
946	nfail := 0
947	test := func(f float64, bits int) {
948		vf := interface{}(f)
949		if bits == 32 {
950			f = float64(float32(f)) // round
951			vf = float32(f)
952		}
953		bout, err := Marshal(vf)
954		if err != nil {
955			t.Errorf("Marshal(%T(%g)): %v", vf, vf, err)
956			nfail++
957			return
958		}
959		out := string(bout)
960
961		// result must convert back to the same float
962		g, err := strconv.ParseFloat(out, bits)
963		if err != nil {
964			t.Errorf("Marshal(%T(%g)) = %q, cannot parse back: %v", vf, vf, out, err)
965			nfail++
966			return
967		}
968		if f != g || fmt.Sprint(f) != fmt.Sprint(g) { // fmt.Sprint handles ±0
969			t.Errorf("Marshal(%T(%g)) = %q (is %g, not %g)", vf, vf, out, float32(g), vf)
970			nfail++
971			return
972		}
973
974		bad := badFloatREs
975		if bits == 64 {
976			bad = bad[:len(bad)-2]
977		}
978		for _, re := range bad {
979			if re.MatchString(out) {
980				t.Errorf("Marshal(%T(%g)) = %q, must not match /%s/", vf, vf, out, re)
981				nfail++
982				return
983			}
984		}
985	}
986
987	var (
988		bigger  = math.Inf(+1)
989		smaller = math.Inf(-1)
990	)
991
992	var digits = "1.2345678901234567890123"
993	for i := len(digits); i >= 2; i-- {
994		if testing.Short() && i < len(digits)-4 {
995			break
996		}
997		for exp := -30; exp <= 30; exp++ {
998			for _, sign := range "+-" {
999				for bits := 32; bits <= 64; bits += 32 {
1000					s := fmt.Sprintf("%c%se%d", sign, digits[:i], exp)
1001					f, err := strconv.ParseFloat(s, bits)
1002					if err != nil {
1003						log.Fatal(err)
1004					}
1005					next := math.Nextafter
1006					if bits == 32 {
1007						next = func(g, h float64) float64 {
1008							return float64(math.Nextafter32(float32(g), float32(h)))
1009						}
1010					}
1011					test(f, bits)
1012					test(next(f, bigger), bits)
1013					test(next(f, smaller), bits)
1014					if nfail > 50 {
1015						t.Fatalf("stopping test early")
1016					}
1017				}
1018			}
1019		}
1020	}
1021	test(0, 64)
1022	test(math.Copysign(0, -1), 64)
1023	test(0, 32)
1024	test(math.Copysign(0, -1), 32)
1025}
1026
1027func TestMarshalRawMessageValue(t *testing.T) {
1028	type (
1029		T1 struct {
1030			M RawMessage `json:",omitempty"`
1031		}
1032		T2 struct {
1033			M *RawMessage `json:",omitempty"`
1034		}
1035	)
1036
1037	var (
1038		rawNil   = RawMessage(nil)
1039		rawEmpty = RawMessage([]byte{})
1040		rawText  = RawMessage([]byte(`"foo"`))
1041	)
1042
1043	tests := []struct {
1044		in   interface{}
1045		want string
1046		ok   bool
1047	}{
1048		// Test with nil RawMessage.
1049		{rawNil, "null", true},
1050		{&rawNil, "null", true},
1051		{[]interface{}{rawNil}, "[null]", true},
1052		{&[]interface{}{rawNil}, "[null]", true},
1053		{[]interface{}{&rawNil}, "[null]", true},
1054		{&[]interface{}{&rawNil}, "[null]", true},
1055		{struct{ M RawMessage }{rawNil}, `{"M":null}`, true},
1056		{&struct{ M RawMessage }{rawNil}, `{"M":null}`, true},
1057		{struct{ M *RawMessage }{&rawNil}, `{"M":null}`, true},
1058		{&struct{ M *RawMessage }{&rawNil}, `{"M":null}`, true},
1059		{map[string]interface{}{"M": rawNil}, `{"M":null}`, true},
1060		{&map[string]interface{}{"M": rawNil}, `{"M":null}`, true},
1061		{map[string]interface{}{"M": &rawNil}, `{"M":null}`, true},
1062		{&map[string]interface{}{"M": &rawNil}, `{"M":null}`, true},
1063		{T1{rawNil}, "{}", true},
1064		{T2{&rawNil}, `{"M":null}`, true},
1065		{&T1{rawNil}, "{}", true},
1066		{&T2{&rawNil}, `{"M":null}`, true},
1067
1068		// Test with empty, but non-nil, RawMessage.
1069		{rawEmpty, "", false},
1070		{&rawEmpty, "", false},
1071		{[]interface{}{rawEmpty}, "", false},
1072		{&[]interface{}{rawEmpty}, "", false},
1073		{[]interface{}{&rawEmpty}, "", false},
1074		{&[]interface{}{&rawEmpty}, "", false},
1075		{struct{ X RawMessage }{rawEmpty}, "", false},
1076		{&struct{ X RawMessage }{rawEmpty}, "", false},
1077		{struct{ X *RawMessage }{&rawEmpty}, "", false},
1078		{&struct{ X *RawMessage }{&rawEmpty}, "", false},
1079		{map[string]interface{}{"nil": rawEmpty}, "", false},
1080		{&map[string]interface{}{"nil": rawEmpty}, "", false},
1081		{map[string]interface{}{"nil": &rawEmpty}, "", false},
1082		{&map[string]interface{}{"nil": &rawEmpty}, "", false},
1083		{T1{rawEmpty}, "{}", true},
1084		{T2{&rawEmpty}, "", false},
1085		{&T1{rawEmpty}, "{}", true},
1086		{&T2{&rawEmpty}, "", false},
1087
1088		// Test with RawMessage with some text.
1089		//
1090		// The tests below marked with Issue6458 used to generate "ImZvbyI=" instead "foo".
1091		// This behavior was intentionally changed in Go 1.8.
1092		// See https://golang.org/issues/14493#issuecomment-255857318
1093		{rawText, `"foo"`, true}, // Issue6458
1094		{&rawText, `"foo"`, true},
1095		{[]interface{}{rawText}, `["foo"]`, true},  // Issue6458
1096		{&[]interface{}{rawText}, `["foo"]`, true}, // Issue6458
1097		{[]interface{}{&rawText}, `["foo"]`, true},
1098		{&[]interface{}{&rawText}, `["foo"]`, true},
1099		{struct{ M RawMessage }{rawText}, `{"M":"foo"}`, true}, // Issue6458
1100		{&struct{ M RawMessage }{rawText}, `{"M":"foo"}`, true},
1101		{struct{ M *RawMessage }{&rawText}, `{"M":"foo"}`, true},
1102		{&struct{ M *RawMessage }{&rawText}, `{"M":"foo"}`, true},
1103		{map[string]interface{}{"M": rawText}, `{"M":"foo"}`, true},  // Issue6458
1104		{&map[string]interface{}{"M": rawText}, `{"M":"foo"}`, true}, // Issue6458
1105		{map[string]interface{}{"M": &rawText}, `{"M":"foo"}`, true},
1106		{&map[string]interface{}{"M": &rawText}, `{"M":"foo"}`, true},
1107		{T1{rawText}, `{"M":"foo"}`, true}, // Issue6458
1108		{T2{&rawText}, `{"M":"foo"}`, true},
1109		{&T1{rawText}, `{"M":"foo"}`, true},
1110		{&T2{&rawText}, `{"M":"foo"}`, true},
1111	}
1112
1113	for i, tt := range tests {
1114		b, err := Marshal(tt.in)
1115		if ok := (err == nil); ok != tt.ok {
1116			if err != nil {
1117				t.Errorf("test %d, unexpected failure: %v", i, err)
1118			} else {
1119				t.Errorf("test %d, unexpected success", i)
1120			}
1121		}
1122		if got := string(b); got != tt.want {
1123			t.Errorf("test %d, Marshal(%#v) = %q, want %q", i, tt.in, got, tt.want)
1124		}
1125	}
1126}
1127
1128type marshalPanic struct{}
1129
1130func (marshalPanic) MarshalJSON() ([]byte, error) { panic(0xdead) }
1131
1132func TestMarshalPanic(t *testing.T) {
1133	defer func() {
1134		if got := recover(); !reflect.DeepEqual(got, 0xdead) {
1135			t.Errorf("panic() = (%T)(%v), want 0xdead", got, got)
1136		}
1137	}()
1138	Marshal(&marshalPanic{})
1139	t.Error("Marshal should have panicked")
1140}
1141
1142func TestMarshalUncommonFieldNames(t *testing.T) {
1143	v := struct {
1144		A0, À, Aβ int
1145	}{}
1146	b, err := Marshal(v)
1147	if err != nil {
1148		t.Fatal("Marshal:", err)
1149	}
1150	want := `{"A0":0,"À":0,"Aβ":0}`
1151	got := string(b)
1152	if got != want {
1153		t.Fatalf("Marshal: got %s want %s", got, want)
1154	}
1155}
1156
1157func TestMarshalerError(t *testing.T) {
1158	s := "test variable"
1159	st := reflect.TypeOf(s)
1160	errText := "json: test error"
1161
1162	tests := []struct {
1163		err  *MarshalerError
1164		want string
1165	}{
1166		{
1167			&MarshalerError{st, fmt.Errorf(errText), ""},
1168			"json: error calling MarshalJSON for type " + st.String() + ": " + errText,
1169		},
1170		{
1171			&MarshalerError{st, fmt.Errorf(errText), "TestMarshalerError"},
1172			"json: error calling TestMarshalerError for type " + st.String() + ": " + errText,
1173		},
1174	}
1175
1176	for i, tt := range tests {
1177		got := tt.err.Error()
1178		if got != tt.want {
1179			t.Errorf("MarshalerError test %d, got: %s, want: %s", i, got, tt.want)
1180		}
1181	}
1182}
1183