Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add ,string option tests
  • Loading branch information
ngicks committed Feb 23, 2023
commit b661cd90e6393709d87f73e13d631129cfd8fdf9
22 changes: 22 additions & 0 deletions type_tests/struct_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ func init() {
(*struct {
Field bool `json:",omitempty,string"`
})(nil),
(*struct {
Str *string `json:",string"`
F32 *float32 `json:",string"`
F64 *float64 `json:",string"`
Int *int `json:",string"`
Uint *uint `json:",string"`
I16 *int16 `json:",string"`
I32 *int32 `json:",string"`
I64 *int64 `json:",string"`
U8 *uint8 `json:",string"`
U16 *uint16 `json:",string"`
U32 *uint32 `json:",string"`
U64 *uint64 `json:",string"`
Uptr *uintptr `json:",string"`
Bool *bool `json:",string"`
})(nil),
(*struct {
Struct struct{ Foo string } `json:",string"`
Arr [2]int `json:",string"`
Slice []string `json:",string"`
Map map[int]int `json:",string"`
})(nil),
(*struct {
Field bool `json:"中文"`
})(nil),
Expand Down
40 changes: 40 additions & 0 deletions value_tests/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ func init() {
Asks [][2]float64 `json:"asks"`
})(nil),
input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
}, unmarshalCase{
ptr: (*quote)(nil),
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}`,
}, unmarshalCase{
ptr: (*quote)(nil),
input: `{"Str":"\"foo\""}`,
}, unmarshalCase{
ptr: (*struct {
AnyStr interface{} `json:",string"`
AnyInt interface{} `json:",string"`
})(nil),
input: `{"AnyStr":"foo","AnyInt":123}`,
})
marshalCases = append(marshalCases,
struct {
Expand Down Expand Up @@ -204,6 +216,14 @@ func init() {
}{
"should not marshal",
},
quote{},
struct {
AnyStr interface{} `json:",string"`
AnyInt interface{} `json:",string"`
}{
AnyStr: "foo",
AnyInt: 123,
},
)
}

Expand Down Expand Up @@ -245,3 +265,23 @@ type structOrder struct {
orderB
Field7 string
}

type quote struct {
// The ,string option applies only to fields of string, floating point, integer,
// or boolean types as per https://pkg.go.dev/encoding/[email protected].
// It is poorly or not totally documented that json.Marshal does not quote null.
Str *string `json:",string"`
F32 *float32 `json:",string"`
F64 *float64 `json:",string"`
Int *int `json:",string"`
Uint *uint `json:",string"`
I16 *int16 `json:",string"`
I32 *int32 `json:",string"`
I64 *int64 `json:",string"`
U8 *uint8 `json:",string"`
U16 *uint16 `json:",string"`
U32 *uint32 `json:",string"`
U64 *uint64 `json:",string"`
Uptr *uintptr `json:",string"`
Bool *bool `json:",string"`
}