diff --git a/extra/fuzzy_decoder.go b/extra/fuzzy_decoder.go index 4091f4b..08e23ff 100644 --- a/extra/fuzzy_decoder.go +++ b/extra/fuzzy_decoder.go @@ -183,6 +183,9 @@ func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Ite *((*string)(ptr)) = string(number) case jsoniter.StringValue: *((*string)(ptr)) = iter.ReadString() + case jsoniter.NilValue: + iter.Skip() + *((*string)(ptr)) = "" default: iter.ReportError("fuzzyStringDecoder", "not number or string") } @@ -208,6 +211,9 @@ func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It } else { str = "0" } + case jsoniter.NilValue: + iter.Skip() + str = "0" default: iter.ReportError("fuzzyIntegerDecoder", "not number or string") } @@ -244,6 +250,9 @@ func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It } else { *((*float32)(ptr)) = 0 } + case jsoniter.NilValue: + iter.Skip() + *((*float32)(ptr)) = 0 default: iter.ReportError("fuzzyFloat32Decoder", "not number or string") } @@ -273,7 +282,10 @@ func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It } else { *((*float64)(ptr)) = 0 } + case jsoniter.NilValue: + iter.Skip() + *((*float64)(ptr)) = 0 default: - iter.ReportError("fuzzyFloat32Decoder", "not number or string") + iter.ReportError("fuzzyFloat64Decoder", "not number or string") } } diff --git a/extra/fuzzy_decoder_test.go b/extra/fuzzy_decoder_test.go index b6a1559..3490e3a 100644 --- a/extra/fuzzy_decoder_test.go +++ b/extra/fuzzy_decoder_test.go @@ -357,3 +357,35 @@ func Test_bad_case(t *testing.T) { should := require.New(t) should.Nil(err) } + +func Test_null_to_string(t *testing.T) { + should := require.New(t) + body := []byte(`null`) + var message string + err := jsoniter.Unmarshal(body, &message) + should.NoError(err) +} + +func Test_null_to_int(t *testing.T) { + should := require.New(t) + body := []byte(`null`) + var message int + err := jsoniter.Unmarshal(body, &message) + should.NoError(err) +} + +func Test_null_to_float32(t *testing.T) { + should := require.New(t) + body := []byte(`null`) + var message float32 + err := jsoniter.Unmarshal(body, &message) + should.NoError(err) +} + +func Test_null_to_float64(t *testing.T) { + should := require.New(t) + body := []byte(`null`) + var message float64 + err := jsoniter.Unmarshal(body, &message) + should.NoError(err) +}