Skip to content

Commit

Permalink
fix json-iterator#311 handle nil any
Browse files Browse the repository at this point in the history
  • Loading branch information
taowen committed Oct 24, 2018
1 parent 605cf42 commit edece26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions any.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ func (codec *directAnyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {

func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
any := *(*Any)(ptr)
if any == nil {
stream.WriteNil()
return
}
any.WriteTo(stream)
}

Expand Down
10 changes: 10 additions & 0 deletions value_tests/invalid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,13 @@ func Test_EmptyInput(t *testing.T) {
t.Errorf("Expected error")
}
}

type Foo struct {
A jsoniter.Any
}

func Test_nil_any(t *testing.T) {
should := require.New(t)
data, _ := jsoniter.Marshal(&Foo{})
should.Equal(`{"A":null}`, string(data))
}

0 comments on commit edece26

Please sign in to comment.