1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-20 18:48:32 +02:00
json-iterator/jsoniter_adapter_test.go

63 lines
1.7 KiB
Go
Raw Normal View History

2017-06-02 15:38:20 +08:00
package jsoniter
import (
"bytes"
2017-06-06 23:27:00 +08:00
"encoding/json"
"github.com/json-iterator/go/require"
2017-06-02 16:00:12 +08:00
"io/ioutil"
2017-06-06 23:27:00 +08:00
"testing"
2017-06-02 15:38:20 +08:00
)
func Test_new_decoder(t *testing.T) {
should := require.New(t)
2017-06-02 16:00:12 +08:00
decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`))
decoder2 := NewDecoder(bytes.NewBufferString(`[1][2]`))
2017-06-02 15:38:20 +08:00
arr1 := []int{}
should.Nil(decoder1.Decode(&arr1))
should.Equal([]int{1}, arr1)
arr2 := []int{}
2017-06-02 16:00:12 +08:00
should.True(decoder1.More())
buffered, _ := ioutil.ReadAll(decoder1.Buffered())
should.Equal("[2]", string(buffered))
2017-06-02 15:38:20 +08:00
should.Nil(decoder2.Decode(&arr2))
2017-06-02 16:00:12 +08:00
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())
2017-06-02 18:46:44 +08:00
}
func Test_new_encoder(t *testing.T) {
should := require.New(t)
buf1 := &bytes.Buffer{}
encoder1 := json.NewEncoder(buf1)
2017-06-16 00:10:05 +08:00
encoder1.SetEscapeHTML(false)
2017-06-02 18:46:44 +08:00
encoder1.Encode([]int{1})
should.Equal("[1]\n", buf1.String())
buf2 := &bytes.Buffer{}
encoder2 := NewEncoder(buf2)
2017-06-16 00:10:05 +08:00
encoder2.SetEscapeHTML(false)
2017-06-02 18:46:44 +08:00
encoder2.Encode([]int{1})
should.Equal("[1]", buf2.String())
2017-06-06 23:15:15 +08:00
}
func Test_use_number(t *testing.T) {
should := require.New(t)
decoder1 := json.NewDecoder(bytes.NewBufferString(`123`))
decoder1.UseNumber()
decoder2 := NewDecoder(bytes.NewBufferString(`123`))
decoder2.UseNumber()
var obj1 interface{}
should.Nil(decoder1.Decode(&obj1))
should.Equal(json.Number("123"), obj1)
var obj2 interface{}
should.Nil(decoder2.Decode(&obj2))
should.Equal(json.Number("123"), obj2)
2017-06-06 23:27:00 +08:00
}