Skip to content

Commit 11c1cce

Browse files
committed
fix #217 when input is null, non-decodable type should not be considered as error, to be compatible with stdlib
1 parent 96fcb84 commit 11c1cce

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

feature_reflect_native.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ type nonEmptyInterfaceCodec struct {
382382
}
383383

384384
func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
385+
if iter.WhatIsNext() == NilValue {
386+
iter.skipFourBytes('n', 'u', 'l', 'l')
387+
return
388+
}
385389
nonEmptyInterface := (*nonEmptyInterface)(ptr)
386390
if nonEmptyInterface.itab == nil {
387391
iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")

jsoniter_invalid_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,10 @@ func TestEOF(t *testing.T) {
191191
err := ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
192192
assert.Equal(t, io.EOF, err)
193193
}
194+
195+
func TestDecodeErrorType(t *testing.T) {
196+
should := require.New(t)
197+
var err error
198+
should.Nil(Unmarshal([]byte("null"), &err))
199+
should.NotNil(Unmarshal([]byte("123"), &err))
200+
}

0 commit comments

Comments
 (0)