mirror of
https://github.com/json-iterator/go.git
synced 2025-03-23 21:09:11 +02:00
#34 support More() and Buffered()
This commit is contained in:
parent
a7f992f0e1
commit
69b742e73a
@ -112,4 +112,13 @@ type AdaptedDecoder struct {
|
||||
func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
|
||||
adapter.iter.ReadVal(obj)
|
||||
return adapter.iter.Error
|
||||
}
|
||||
|
||||
func (adapter *AdaptedDecoder) More() bool {
|
||||
return adapter.iter.head != adapter.iter.tail
|
||||
}
|
||||
|
||||
func (adapter *AdaptedDecoder) Buffered() io.Reader {
|
||||
remaining := adapter.iter.buf[adapter.iter.head:adapter.iter.tail]
|
||||
return bytes.NewReader(remaining)
|
||||
}
|
@ -5,15 +5,30 @@ import (
|
||||
"github.com/json-iterator/go/require"
|
||||
"encoding/json"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func Test_new_decoder(t *testing.T) {
|
||||
should := require.New(t)
|
||||
decoder1 := json.NewDecoder(bytes.NewBufferString(`[1]`))
|
||||
decoder2 := NewDecoder(bytes.NewBufferString(`[1]`))
|
||||
decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||
decoder2 := NewDecoder(bytes.NewBufferString(`[1][2]`))
|
||||
arr1 := []int{}
|
||||
should.Nil(decoder1.Decode(&arr1))
|
||||
should.Equal([]int{1}, arr1)
|
||||
arr2 := []int{}
|
||||
should.True(decoder1.More())
|
||||
buffered, _ := ioutil.ReadAll(decoder1.Buffered())
|
||||
should.Equal("[2]", string(buffered))
|
||||
should.Nil(decoder2.Decode(&arr2))
|
||||
should.Equal([]int{1}, arr2)
|
||||
should.True(decoder2.More())
|
||||
buffered, _ = ioutil.ReadAll(decoder2.Buffered())
|
||||
should.Equal("[2]", string(buffered))
|
||||
|
||||
should.Nil(decoder1.Decode(&arr1))
|
||||
should.Equal([]int{2}, arr1)
|
||||
should.False(decoder1.More())
|
||||
should.Nil(decoder2.Decode(&arr2))
|
||||
should.Equal([]int{2}, arr2)
|
||||
should.False(decoder2.More())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user