Skip to content

Commit 5916df6

Browse files
committed
fix json-iterator#311 handle nil any
1 parent 2433035 commit 5916df6

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

any.go

+4
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ func (codec *directAnyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
312312

313313
func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
314314
any := *(*Any)(ptr)
315+
if any == nil {
316+
stream.WriteNil()
317+
return
318+
}
315319
any.WriteTo(stream)
316320
}
317321

value_tests/invalid_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,13 @@ func Test_EmptyInput(t *testing.T) {
224224
t.Errorf("Expected error")
225225
}
226226
}
227+
228+
type Foo struct {
229+
A jsoniter.Any
230+
}
231+
232+
func Test_nil_any(t *testing.T) {
233+
should := require.New(t)
234+
data, _ := jsoniter.Marshal(&Foo{})
235+
should.Equal(`{"A":null}`, string(data))
236+
}

0 commit comments

Comments
 (0)