Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion feature_reflect_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions jsoniter_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}