From e658f6597a29bce83f1d974a3800581d4ab49378 Mon Sep 17 00:00:00 2001 From: Jason Toffaletti Date: Thu, 14 Sep 2017 20:44:42 -0700 Subject: [PATCH 1/2] add failing test for handling of nil interface with omitempty --- jsoniter_interface_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/jsoniter_interface_test.go b/jsoniter_interface_test.go index 0e7ef882..1e1fb833 100644 --- a/jsoniter_interface_test.go +++ b/jsoniter_interface_test.go @@ -351,3 +351,22 @@ func Test_nil_out_null_interface(t *testing.T) { should.Equal(nil, err) should.Equal(nil, obj2.Field) } + +func Test_omitempty_nil_interface(t *testing.T) { + type TestData struct { + Field interface{} `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) +} From 005d86dc447869f61ddc693cda73815ed6d57398 Mon Sep 17 00:00:00 2001 From: Jason Toffaletti Date: Thu, 14 Sep 2017 21:32:42 -0700 Subject: [PATCH 2/2] fix handling of nil empty interface --- feature_reflect_native.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/feature_reflect_native.go b/feature_reflect_native.go index 9bd290b8..78912c05 100644 --- a/feature_reflect_native.go +++ b/feature_reflect_native.go @@ -373,7 +373,8 @@ func (codec *emptyInterfaceCodec) EncodeInterface(val interface{}, stream *Strea } func (codec *emptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool { - return ptr == nil + emptyInterface := (*emptyInterface)(ptr) + return emptyInterface.typ == nil } type nonEmptyInterfaceCodec struct {