1
0
mirror of https://github.com/json-iterator/go.git synced 2025-05-25 22:09:04 +02:00

fix #217 when input is null, non-decodable type should not be considered as error, to be compatible with stdlib

This commit is contained in:
Tao Wen 2018-01-04 16:19:26 +08:00
parent 96fcb84835
commit 11c1cce0d8
2 changed files with 11 additions and 0 deletions

View File

@ -382,6 +382,10 @@ type nonEmptyInterfaceCodec struct {
}
func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.WhatIsNext() == NilValue {
iter.skipFourBytes('n', 'u', 'l', 'l')
return
}
nonEmptyInterface := (*nonEmptyInterface)(ptr)
if nonEmptyInterface.itab == nil {
iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")

View File

@ -191,3 +191,10 @@ func TestEOF(t *testing.T) {
err := ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
assert.Equal(t, io.EOF, err)
}
func TestDecodeErrorType(t *testing.T) {
should := require.New(t)
var err error
should.Nil(Unmarshal([]byte("null"), &err))
should.NotNil(Unmarshal([]byte("123"), &err))
}