1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00

fix #311 handle nil any

This commit is contained in:
Tao Wen 2018-10-24 21:05:37 +08:00
parent 2433035e51
commit 5916df66b3
2 changed files with 14 additions and 0 deletions

4
any.go
View File

@ -312,6 +312,10 @@ func (codec *directAnyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) { func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
any := *(*Any)(ptr) any := *(*Any)(ptr)
if any == nil {
stream.WriteNil()
return
}
any.WriteTo(stream) any.WriteTo(stream)
} }

View File

@ -224,3 +224,13 @@ func Test_EmptyInput(t *testing.T) {
t.Errorf("Expected error") 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))
}