From e658f6597a29bce83f1d974a3800581d4ab49378 Mon Sep 17 00:00:00 2001 From: Jason Toffaletti Date: Thu, 14 Sep 2017 20:44:42 -0700 Subject: [PATCH] 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 0e7ef88..1e1fb83 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) +}