You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-07-03 23:30:41 +02:00
add tests for decoding nil interfaces
This commit is contained in:
@ -371,6 +371,29 @@ func Test_omitempty_nil_interface(t *testing.T) {
|
|||||||
should.Equal(string(js), str)
|
should.Equal(string(js), str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_omitempty_nil_nonempty_interface(t *testing.T) {
|
||||||
|
type TestData struct {
|
||||||
|
Field MyInterface `json:"field,omitempty"`
|
||||||
|
}
|
||||||
|
should := require.New(t)
|
||||||
|
|
||||||
|
obj := TestData{
|
||||||
|
Field: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
js, err := json.Marshal(obj)
|
||||||
|
should.Equal(nil, err)
|
||||||
|
should.Equal("{}", string(js))
|
||||||
|
|
||||||
|
str, err := MarshalToString(obj)
|
||||||
|
should.Equal(nil, err)
|
||||||
|
should.Equal(string(js), str)
|
||||||
|
|
||||||
|
err = Unmarshal(js, &obj)
|
||||||
|
should.Equal(nil, err)
|
||||||
|
should.Equal(nil, obj.Field)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_marshal_nil_marshaler_interface(t *testing.T) {
|
func Test_marshal_nil_marshaler_interface(t *testing.T) {
|
||||||
type TestData struct {
|
type TestData struct {
|
||||||
Field json.Marshaler `json:"field"`
|
Field json.Marshaler `json:"field"`
|
||||||
@ -407,4 +430,9 @@ func Test_marshal_nil_nonempty_interface(t *testing.T) {
|
|||||||
str, err := MarshalToString(obj)
|
str, err := MarshalToString(obj)
|
||||||
should.Equal(nil, err)
|
should.Equal(nil, err)
|
||||||
should.Equal(string(js), str)
|
should.Equal(string(js), str)
|
||||||
|
|
||||||
|
obj.Field = MyString("hello")
|
||||||
|
err = Unmarshal(js, &obj)
|
||||||
|
should.Equal(nil, err)
|
||||||
|
should.Equal(nil, obj.Field)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user