1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-29 21:20:52 +02:00

fix #214 report EOF like stdlib

This commit is contained in:
Tao Wen 2017-12-21 22:18:28 +08:00
parent a9b9c73b4d
commit 60a9df5ebc
3 changed files with 15 additions and 0 deletions

View File

@ -71,6 +71,9 @@ type Decoder struct {
// Decode decode JSON into interface{}
func (adapter *Decoder) Decode(obj interface{}) error {
if adapter.iter.Error == io.EOF {
return io.EOF
}
adapter.iter.ReadVal(obj)
err := adapter.iter.Error
if err == io.EOF {

View File

@ -337,6 +337,11 @@ func (cfg *frozenConfig) NewEncoder(writer io.Writer) *Encoder {
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *Decoder {
iter := Parse(cfg, reader, 512)
if reader != nil {
if !iter.loadMore() {
iter.Error = io.EOF
}
}
return &Decoder{iter}
}

View File

@ -3,6 +3,7 @@ package jsoniter
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"testing"
@ -184,3 +185,9 @@ func Test_func_pointer_type(t *testing.T) {
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)
}