Tao Wen пре 7 година
родитељ
комит
5916df66b3
2 измењених фајлова са 14 додато и 0 уклоњено
  1. 4 0
      any.go
  2. 10 0
      value_tests/invalid_test.go

+ 4 - 0
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)
 }
 

+ 10 - 0
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))
+}