Skip to content

Commit b661cd9

Browse files
committed
add ,string option tests
1 parent 71ac162 commit b661cd9

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

type_tests/struct_tags_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ func init() {
146146
(*struct {
147147
Field bool `json:",omitempty,string"`
148148
})(nil),
149+
(*struct {
150+
Str *string `json:",string"`
151+
F32 *float32 `json:",string"`
152+
F64 *float64 `json:",string"`
153+
Int *int `json:",string"`
154+
Uint *uint `json:",string"`
155+
I16 *int16 `json:",string"`
156+
I32 *int32 `json:",string"`
157+
I64 *int64 `json:",string"`
158+
U8 *uint8 `json:",string"`
159+
U16 *uint16 `json:",string"`
160+
U32 *uint32 `json:",string"`
161+
U64 *uint64 `json:",string"`
162+
Uptr *uintptr `json:",string"`
163+
Bool *bool `json:",string"`
164+
})(nil),
165+
(*struct {
166+
Struct struct{ Foo string } `json:",string"`
167+
Arr [2]int `json:",string"`
168+
Slice []string `json:",string"`
169+
Map map[int]int `json:",string"`
170+
})(nil),
149171
(*struct {
150172
Field bool `json:"中文"`
151173
})(nil),

value_tests/struct_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ func init() {
105105
Asks [][2]float64 `json:"asks"`
106106
})(nil),
107107
input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
108+
}, unmarshalCase{
109+
ptr: (*quote)(nil),
110+
input: `{"Str":null,"F32":null,"F64":null,"Int":null,"Uint":null,"I16":null,"I32":null,"I64":null,"U8":null,"U16":null,"U32":null,"U64":null,"Uptr":null,"Bool":null}`,
111+
}, unmarshalCase{
112+
ptr: (*quote)(nil),
113+
input: `{"Str":"\"foo\""}`,
114+
}, unmarshalCase{
115+
ptr: (*struct {
116+
AnyStr interface{} `json:",string"`
117+
AnyInt interface{} `json:",string"`
118+
})(nil),
119+
input: `{"AnyStr":"foo","AnyInt":123}`,
108120
})
109121
marshalCases = append(marshalCases,
110122
struct {
@@ -204,6 +216,14 @@ func init() {
204216
}{
205217
"should not marshal",
206218
},
219+
quote{},
220+
struct {
221+
AnyStr interface{} `json:",string"`
222+
AnyInt interface{} `json:",string"`
223+
}{
224+
AnyStr: "foo",
225+
AnyInt: 123,
226+
},
207227
)
208228
}
209229

@@ -245,3 +265,23 @@ type structOrder struct {
245265
orderB
246266
Field7 string
247267
}
268+
269+
type quote struct {
270+
// The ,string option applies only to fields of string, floating point, integer,
271+
// or boolean types as per https://pkg.go.dev/encoding/[email protected].
272+
// It is poorly or not totally documented that json.Marshal does not quote null.
273+
Str *string `json:",string"`
274+
F32 *float32 `json:",string"`
275+
F64 *float64 `json:",string"`
276+
Int *int `json:",string"`
277+
Uint *uint `json:",string"`
278+
I16 *int16 `json:",string"`
279+
I32 *int32 `json:",string"`
280+
I64 *int64 `json:",string"`
281+
U8 *uint8 `json:",string"`
282+
U16 *uint16 `json:",string"`
283+
U32 *uint32 `json:",string"`
284+
U64 *uint64 `json:",string"`
285+
Uptr *uintptr `json:",string"`
286+
Bool *bool `json:",string"`
287+
}

0 commit comments

Comments
 (0)