From 5916df66b3733aaef213b39c2c67d0990eac487f Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Wed, 24 Oct 2018 21:05:37 +0800 Subject: [PATCH] fix #311 handle nil any --- any.go | 4 ++++ value_tests/invalid_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/any.go b/any.go index daecfed..f6b8aea 100644 --- a/any.go +++ b/any.go @@ -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) } diff --git a/value_tests/invalid_test.go b/value_tests/invalid_test.go index 5b594a9..4e73205 100644 --- a/value_tests/invalid_test.go +++ b/value_tests/invalid_test.go @@ -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)) +}