mirror of
https://github.com/json-iterator/go.git
synced 2025-04-01 21:24:21 +02:00
fix #214 report EOF like stdlib
This commit is contained in:
parent
a9b9c73b4d
commit
60a9df5ebc
@ -71,6 +71,9 @@ type Decoder struct {
|
|||||||
|
|
||||||
// Decode decode JSON into interface{}
|
// Decode decode JSON into interface{}
|
||||||
func (adapter *Decoder) Decode(obj interface{}) error {
|
func (adapter *Decoder) Decode(obj interface{}) error {
|
||||||
|
if adapter.iter.Error == io.EOF {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
adapter.iter.ReadVal(obj)
|
adapter.iter.ReadVal(obj)
|
||||||
err := adapter.iter.Error
|
err := adapter.iter.Error
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
@ -337,6 +337,11 @@ func (cfg *frozenConfig) NewEncoder(writer io.Writer) *Encoder {
|
|||||||
|
|
||||||
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
|
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
|
||||||
iter := Parse(cfg, reader, 512)
|
iter := Parse(cfg, reader, 512)
|
||||||
|
if reader != nil {
|
||||||
|
if !iter.loadMore() {
|
||||||
|
iter.Error = io.EOF
|
||||||
|
}
|
||||||
|
}
|
||||||
return &Decoder{iter}
|
return &Decoder{iter}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package jsoniter
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
@ -184,3 +185,9 @@ func Test_func_pointer_type(t *testing.T) {
|
|||||||
should.NotNil(Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
|
should.NotNil(Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEOF(t *testing.T) {
|
||||||
|
var s string
|
||||||
|
err := ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
|
||||||
|
assert.Equal(t, io.EOF, err)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user