1 package test
2
3 import (
4 "bytes"
5 "encoding/json"
6 "time"
7 )
8
9 func init() {
10 var pString = func(val string) *string {
11 return &val
12 }
13 epoch := time.Unix(0, 0)
14 unmarshalCases = append(unmarshalCases, unmarshalCase{
15 ptr: (*struct {
16 Field interface{}
17 })(nil),
18 input: `{"Field": "hello"}`,
19 }, unmarshalCase{
20 ptr: (*struct {
21 Field interface{}
22 })(nil),
23 input: `{"Field": "hello"} `,
24 }, unmarshalCase{
25 ptr: (*struct {
26 Field int `json:"field"`
27 })(nil),
28 input: `{"field": null}`,
29 }, unmarshalCase{
30 ptr: (*struct {
31 Field int `json:"field,string"`
32 })(nil),
33 input: `{"field": null}`,
34 }, unmarshalCase{
35 ptr: (*struct {
36 ID int `json:"id"`
37 Payload map[string]interface{} `json:"payload"`
38 buf *bytes.Buffer
39 })(nil),
40 input: ` {"id":1, "payload":{"account":"123","password":"456"}}`,
41 }, unmarshalCase{
42 ptr: (*struct {
43 Field1 string
44 })(nil),
45 input: `{"Field\"1":"hello"}`,
46 }, unmarshalCase{
47 ptr: (*struct {
48 Field1 string
49 })(nil),
50 input: `{"\u0046ield1":"hello"}`,
51 }, unmarshalCase{
52 ptr: (*struct {
53 Field1 *string
54 Field2 *string
55 })(nil),
56 input: `{"field1": null, "field2": "world"}`,
57 }, unmarshalCase{
58 ptr: (*struct {
59 Field1 string
60 Field2 json.RawMessage
61 })(nil),
62 input: `{"field1": "hello", "field2":[1,2,3]}`,
63 }, unmarshalCase{
64 ptr: (*struct {
65 a int
66 b <-chan int
67 C int
68 d *time.Timer
69 })(nil),
70 input: `{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`,
71 }, unmarshalCase{
72 ptr: (*struct {
73 A string
74 B string
75 C string
76 D string
77 E string
78 F string
79 G string
80 H string
81 I string
82 J string
83 K string
84 })(nil),
85 input: `{"a":"1","b":"2","c":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`,
86 }, unmarshalCase{
87 ptr: (*struct {
88 T float64 `json:"T"`
89 })(nil),
90 input: `{"t":10.0}`,
91 }, unmarshalCase{
92 ptr: (*struct {
93 T float64 `json:"T"`
94 })(nil),
95 input: `{"T":10.0}`,
96 }, unmarshalCase{
97 ptr: (*struct {
98 T float64 `json:"t"`
99 })(nil),
100 input: `{"T":10.0}`,
101 }, unmarshalCase{
102 ptr: (*struct {
103 KeyString string `json:"key_string"`
104 Type string `json:"type"`
105 Asks [][2]float64 `json:"asks"`
106 })(nil),
107 input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
108 })
109 marshalCases = append(marshalCases,
110 struct {
111 Field map[string]interface{}
112 }{
113 map[string]interface{}{"hello": "world"},
114 },
115 struct {
116 Field map[string]interface{}
117 Field2 string
118 }{
119 map[string]interface{}{"hello": "world"}, "",
120 },
121 struct {
122 Field interface{}
123 }{
124 1024,
125 },
126 struct {
127 Field MyInterface
128 }{
129 MyString("hello"),
130 },
131 struct {
132 F *float64
133 }{},
134 struct {
135 *time.Time
136 }{&epoch},
137 struct {
138 *StructVarious
139 }{&StructVarious{}},
140 struct {
141 *StructVarious
142 Field int
143 }{nil, 10},
144 struct {
145 Field1 int
146 Field2 [1]*float64
147 }{},
148 struct {
149 Field interface{} `json:"field,omitempty"`
150 }{},
151 struct {
152 Field MyInterface `json:"field,omitempty"`
153 }{},
154 struct {
155 Field MyInterface `json:"field,omitempty"`
156 }{MyString("hello")},
157 struct {
158 Field json.Marshaler `json:"field"`
159 }{},
160 struct {
161 Field MyInterface `json:"field"`
162 }{},
163 struct {
164 Field MyInterface `json:"field"`
165 }{MyString("hello")},
166 struct {
167 Field1 string `json:"field-1,omitempty"`
168 Field2 func() `json:"-"`
169 }{},
170 structRecursive{},
171 struct {
172 *CacheItem
173
174
175 OmitMaxAge omit `json:"cacheAge,omitempty"`
176
177
178 MaxAge int `json:"max_age"`
179 }{
180 CacheItem: &CacheItem{
181 Key: "value",
182 MaxAge: 100,
183 },
184 MaxAge: 20,
185 },
186 structOrder{},
187 struct {
188 Field1 *string
189 Field2 *string
190 }{Field2: pString("world")},
191 struct {
192 a int
193 b <-chan int
194 C int
195 d *time.Timer
196 }{
197 a: 42,
198 b: make(<-chan int, 10),
199 C: 21,
200 d: time.NewTimer(10 * time.Second),
201 },
202 struct {
203 _UnderscoreField string
204 }{
205 "should not marshal",
206 },
207 )
208 }
209
210 type StructVarious struct {
211 Field0 string
212 Field1 []string
213 Field2 map[string]interface{}
214 }
215
216 type structRecursive struct {
217 Field1 string
218 Me *structRecursive
219 }
220
221 type omit *struct{}
222 type CacheItem struct {
223 Key string `json:"key"`
224 MaxAge int `json:"cacheAge"`
225 }
226
227 type orderA struct {
228 Field2 string
229 }
230
231 type orderC struct {
232 Field5 string
233 }
234
235 type orderB struct {
236 Field4 string
237 orderC
238 Field6 string
239 }
240
241 type structOrder struct {
242 Field1 string
243 orderA
244 Field3 string
245 orderB
246 Field7 string
247 }
248
View as plain text