mirror of
https://github.com/json-iterator/go.git
synced 2025-03-23 21:09:11 +02:00
#34 add decoder adapter
This commit is contained in:
parent
5310d4aa9a
commit
4cc44e7380
@ -99,3 +99,17 @@ func MarshalToString(v interface{}) (string, error) {
|
||||
}
|
||||
return string(buf), nil
|
||||
}
|
||||
|
||||
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
||||
iter := Parse(reader, 512)
|
||||
return &AdaptedDecoder{iter}
|
||||
}
|
||||
|
||||
type AdaptedDecoder struct {
|
||||
iter *Iterator
|
||||
}
|
||||
|
||||
func (adapter *AdaptedDecoder) Decode(obj interface{}) error {
|
||||
adapter.iter.ReadVal(obj)
|
||||
return adapter.iter.Error
|
||||
}
|
19
jsoniter_adapter_test.go
Normal file
19
jsoniter_adapter_test.go
Normal file
@ -0,0 +1,19 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/json-iterator/go/require"
|
||||
"encoding/json"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func Test_new_decoder(t *testing.T) {
|
||||
should := require.New(t)
|
||||
decoder1 := json.NewDecoder(bytes.NewBufferString(`[1]`))
|
||||
decoder2 := NewDecoder(bytes.NewBufferString(`[1]`))
|
||||
arr1 := []int{}
|
||||
should.Nil(decoder1.Decode(&arr1))
|
||||
should.Equal([]int{1}, arr1)
|
||||
arr2 := []int{}
|
||||
should.Nil(decoder2.Decode(&arr2))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user