From edece2695dad840386cefbc713352758b8c99ce0 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 daecfed6..f6b8aeab 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 5b594a95..4e732055 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)) +}