diff --git a/feature_reflect_native.go b/feature_reflect_native.go index 19b5fa4..0e25bc9 100644 --- a/feature_reflect_native.go +++ b/feature_reflect_native.go @@ -441,9 +441,13 @@ type jsonNumberCodec struct { } func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) { - if iter.WhatIsNext() == StringValue { + switch iter.WhatIsNext() { + case StringValue: *((*json.Number)(ptr)) = json.Number(iter.ReadString()) - } else { + case NilValue: + iter.skipFourBytes('n', 'u', 'l', 'l') + *((*json.Number)(ptr)) = "" + default: *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString())) } } @@ -464,9 +468,13 @@ type jsoniterNumberCodec struct { } func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) { - if iter.WhatIsNext() == StringValue { + switch iter.WhatIsNext() { + case StringValue: *((*Number)(ptr)) = Number(iter.ReadString()) - } else { + case NilValue: + iter.skipFourBytes('n', 'u', 'l', 'l') + *((*Number)(ptr)) = "" + default: *((*Number)(ptr)) = Number([]byte(iter.readNumberAsString())) } } diff --git a/jsoniter_int_test.go b/jsoniter_int_test.go index 7d7e0ad..e0c8029 100644 --- a/jsoniter_int_test.go +++ b/jsoniter_int_test.go @@ -518,6 +518,18 @@ func Test_non_numeric_as_number(t *testing.T) { should.Equal("500", string(v2)) } +func Test_null_as_number(t *testing.T) { + should := require.New(t) + var v1 json.Number + err := json.Unmarshal([]byte(`null`), &v1) + should.Nil(err) + should.Equal("", string(v1)) + var v2 Number + err = Unmarshal([]byte(`null`), &v2) + should.Nil(err) + should.Equal("", string(v2)) +} + func Benchmark_jsoniter_encode_int(b *testing.B) { stream := NewStream(ConfigDefault, ioutil.Discard, 64) for n := 0; n < b.N; n++ {