1
2
3
18
19 package encoder
20
21 import (
22 `encoding/json`
23 `testing`
24
25 `github.com/stretchr/testify/require`
26 )
27
28 func TestOptionSliceOrMapNoNull(t *testing.T) {
29 obj := sample{}
30 out, err := Encode(obj, NoNullSliceOrMap)
31 if err != nil {
32 t.Fatal(err)
33 }
34 require.Equal(t, `{"M":{},"S":[],"A":[],"MP":null,"SP":null,"AP":null}`, string(out))
35
36 obj2 := sample{}
37 out, err = Encode(obj2, 0)
38 if err != nil {
39 t.Fatal(err)
40 }
41 require.Equal(t, `{"M":null,"S":null,"A":[],"MP":null,"SP":null,"AP":null}`, string(out))
42 }
43
44 func TestEncoder_Marshaler(t *testing.T) {
45 v := MarshalerStruct{V: MarshalerImpl{X: 12345}}
46 ret, err := Encode(&v, 0)
47 require.NoError(t, err)
48 require.Equal(t, `{"V":12345 }`, string(ret))
49 ret, err = Encode(v, 0)
50 require.NoError(t, err)
51 require.Equal(t, `{"V":{"X":12345}}`, string(ret))
52
53 ret2, err2 := Encode(&v, 0)
54 require.NoError(t, err2)
55 require.Equal(t, `{"V":12345 }`, string(ret2))
56 ret3, err3 := Encode(v, CompactMarshaler)
57 require.NoError(t, err3)
58 require.Equal(t, `{"V":{"X":12345}}`, string(ret3))
59 }
60
61 func TestMarshalerError(t *testing.T) {
62 v := MarshalerErrorStruct{}
63 ret, err := Encode(&v, 0)
64 require.EqualError(t, err, `invalid Marshaler output json syntax at 5: "[\"\"] {"`)
65 require.Equal(t, []byte(nil), ret)
66 }
67
68
69 func TestEncoder_RawMessage(t *testing.T) {
70 rms := RawMessageStruct{
71 X: json.RawMessage("123456 "),
72 }
73 ret, err := Encode(&rms, 0)
74 require.NoError(t, err)
75 require.Equal(t, `{"X":123456 }`, string(ret))
76
77 ret, err = Encode(&rms, CompactMarshaler)
78 require.NoError(t, err)
79 require.Equal(t, `{"X":123456}`, string(ret))
80 }
81
82
83 func TestEncoder_TextMarshaler(t *testing.T) {
84 v := TextMarshalerStruct{V: TextMarshalerImpl{X: (`{"a"}`)}}
85 ret, err := Encode(&v, 0)
86 require.NoError(t, err)
87 require.Equal(t, `{"V":"{\"a\"}"}`, string(ret))
88 ret, err = Encode(v, 0)
89 require.NoError(t, err)
90 require.Equal(t, `{"V":{"X":"{\"a\"}"}}`, string(ret))
91
92 ret2, err2 := Encode(&v, NoQuoteTextMarshaler)
93 require.NoError(t, err2)
94 require.Equal(t, `{"V":{"a"}}`, string(ret2))
95 ret3, err3 := Encode(v, NoQuoteTextMarshaler)
96 require.NoError(t, err3)
97 require.Equal(t, `{"V":{"X":"{\"a\"}"}}`, string(ret3))
98 }
99
100 func TestEncoder_Marshal_EscapeHTML(t *testing.T) {
101 v := map[string]TextMarshalerImpl{"&&":{"<>"}}
102 ret, err := Encode(v, EscapeHTML)
103 require.NoError(t, err)
104 require.Equal(t, `{"\u0026\u0026":{"X":"\u003c\u003e"}}`, string(ret))
105 ret, err = Encode(v, 0)
106 require.NoError(t, err)
107 require.Equal(t, `{"&&":{"X":"<>"}}`, string(ret))
108
109
110
111 m := map[string]string{"test": "“123”"}
112 ret, err = Encode(m, EscapeHTML)
113 require.Equal(t, string(ret), `{"test":"“123”"}`)
114 require.NoError(t, err)
115
116 m = map[string]string{"K": "\u2028\u2028\xe2"}
117 ret, err = Encode(m, EscapeHTML)
118 require.Equal(t, string(ret), "{\"K\":\"\\u2028\\u2028\xe2\"}")
119 require.NoError(t, err)
120 }
121
View as plain text