Skip to content

More nil interface fixes #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 16, 2017
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
17 changes: 13 additions & 4 deletions feature_reflect_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,20 @@ func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator)
e.typ = nonEmptyInterface.itab.typ
e.word = nonEmptyInterface.word
iter.ReadVal(&i)
if e.word == nil {
nonEmptyInterface.itab = nil
}
nonEmptyInterface.word = e.word
}

func (codec *nonEmptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
nonEmptyInterface := (*nonEmptyInterface)(ptr)
var i interface{}
e := (*emptyInterface)(unsafe.Pointer(&i))
e.typ = nonEmptyInterface.itab.typ
e.word = nonEmptyInterface.word
if nonEmptyInterface.itab != nil {
e := (*emptyInterface)(unsafe.Pointer(&i))
e.typ = nonEmptyInterface.itab.typ
e.word = nonEmptyInterface.word
}
stream.WriteVal(i)
}

Expand Down Expand Up @@ -660,7 +665,11 @@ func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
templateInterface := encoder.templateInterface
templateInterface.word = ptr
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
marshaler := (*realInterface).(json.Marshaler)
marshaler, ok := (*realInterface).(json.Marshaler)
if !ok {
stream.WriteVal(nil)
return
}

bytes, err := marshaler.MarshalJSON()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions jsoniter_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ func Test_bool_can_be_null(t *testing.T) {
obj := TestData{}
data1 := []byte(`{"field": true}`)
err := Unmarshal(data1, &obj)
should.Equal(nil, err)
should.NoError(err)
should.Equal(true, obj.Field)

data2 := []byte(`{"field": null}`)
err = Unmarshal(data2, &obj)
should.Equal(nil, err)
should.NoError(err)
// Same behavior as stdlib, not touching the existing value.
should.Equal(true, obj.Field)

// Checking stdlib behavior as well
obj2 := TestData{}
err = json.Unmarshal(data1, &obj2)
should.Equal(nil, err)
should.NoError(err)
should.Equal(true, obj2.Field)

err = json.Unmarshal(data2, &obj2)
should.Equal(nil, err)
should.NoError(err)
should.Equal(true, obj2.Field)
}
4 changes: 2 additions & 2 deletions jsoniter_enum_marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func Test_custom_marshaler_on_enum(t *testing.T) {
w := Wrapper{Payload: MyEnumB}

jb, err := Marshal(w)
should.Equal(nil, err)
should.NoError(err)
should.Equal(`{"Payload":"foo-1"}`, string(jb))

var w2 Wrapper2
err = Unmarshal(jb, &w2)
should.Equal(nil, err)
should.NoError(err)
should.Equal(MyEnumB, w2.Payload)
}
79 changes: 73 additions & 6 deletions jsoniter_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,13 @@ func Test_nil_out_null_interface(t *testing.T) {
data1 := []byte(`{"field": true}`)

err := Unmarshal(data1, &obj)
should.Equal(nil, err)
should.NoError(err)
should.Equal(true, *(obj.Field.(*bool)))

data2 := []byte(`{"field": null}`)

err = Unmarshal(data2, &obj)
should.Equal(nil, err)
should.NoError(err)
should.Equal(nil, obj.Field)

// Checking stdlib behavior matches.
Expand All @@ -344,11 +344,11 @@ func Test_nil_out_null_interface(t *testing.T) {
}

err = json.Unmarshal(data1, &obj2)
should.Equal(nil, err)
should.NoError(err)
should.Equal(true, *(obj2.Field.(*bool)))

err = json.Unmarshal(data2, &obj2)
should.Equal(nil, err)
should.NoError(err)
should.Equal(nil, obj2.Field)
}

Expand All @@ -363,10 +363,77 @@ func Test_omitempty_nil_interface(t *testing.T) {
}

js, err := json.Marshal(obj)
should.Equal(nil, err)
should.NoError(err)
should.Equal("{}", string(js))

str, err := MarshalToString(obj)
should.Equal(nil, err)
should.NoError(err)
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.NoError(err)
should.Equal("{}", string(js))

str, err := MarshalToString(obj)
should.NoError(err)
should.Equal(string(js), str)

obj.Field = MyString("hello")
err = UnmarshalFromString(`{"field":null}`, &obj)
should.NoError(err)
should.Equal(nil, obj.Field)
}

func Test_marshal_nil_marshaler_interface(t *testing.T) {
type TestData struct {
Field json.Marshaler `json:"field"`
}
should := require.New(t)

obj := TestData{
Field: nil,
}

js, err := json.Marshal(obj)
should.NoError(err)
should.Equal(`{"field":null}`, string(js))

str, err := MarshalToString(obj)
should.NoError(err)
should.Equal(string(js), str)
}

func Test_marshal_nil_nonempty_interface(t *testing.T) {
type TestData struct {
Field MyInterface `json:"field"`
}
should := require.New(t)

obj := TestData{
Field: nil,
}

js, err := json.Marshal(obj)
should.NoError(err)
should.Equal(`{"field":null}`, string(js))

str, err := MarshalToString(obj)
should.NoError(err)
should.Equal(string(js), str)

obj.Field = MyString("hello")
err = Unmarshal(js, &obj)
should.NoError(err)
should.Equal(nil, obj.Field)
}