From 832437440218a83b4798e5c893d2151865390b8a Mon Sep 17 00:00:00 2001 From: Jason Toffaletti Date: Sat, 16 Sep 2017 16:24:27 -0700 Subject: [PATCH] add tests for decoding nil interfaces --- jsoniter_interface_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/jsoniter_interface_test.go b/jsoniter_interface_test.go index c489332..62aec84 100644 --- a/jsoniter_interface_test.go +++ b/jsoniter_interface_test.go @@ -371,6 +371,29 @@ func Test_omitempty_nil_interface(t *testing.T) { 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) { type TestData struct { Field json.Marshaler `json:"field"` @@ -407,4 +430,9 @@ func Test_marshal_nil_nonempty_interface(t *testing.T) { str, err := MarshalToString(obj) should.Equal(nil, err) should.Equal(string(js), str) + + obj.Field = MyString("hello") + err = Unmarshal(js, &obj) + should.Equal(nil, err) + should.Equal(nil, obj.Field) }