1
0
mirror of https://github.com/json-iterator/go.git synced 2024-12-03 08:45:22 +02:00
json-iterator/jsoniter_array_test.go

203 lines
4.5 KiB
Go
Raw Normal View History

2016-12-01 07:53:40 +02:00
package jsoniter
import (
2017-06-06 17:27:00 +02:00
"bytes"
2016-12-01 07:53:40 +02:00
"encoding/json"
2017-01-07 16:16:20 +02:00
"github.com/json-iterator/go/require"
2017-06-06 17:27:00 +02:00
"testing"
2016-12-01 07:53:40 +02:00
)
func Test_empty_array(t *testing.T) {
2017-01-20 07:54:51 +02:00
should := require.New(t)
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `[]`)
2016-12-01 07:53:40 +02:00
cont := iter.ReadArray()
2017-01-20 07:54:51 +02:00
should.False(cont)
2017-06-17 04:21:37 +02:00
iter = ParseString(ConfigDefault, `[]`)
2017-01-20 07:54:51 +02:00
iter.ReadArrayCB(func(iter *Iterator) bool {
should.FailNow("should not call")
return true
})
2016-12-01 07:53:40 +02:00
}
func Test_one_element(t *testing.T) {
2017-01-20 07:54:51 +02:00
should := require.New(t)
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `[1]`)
2017-01-20 07:54:51 +02:00
should.True(iter.ReadArray())
should.Equal(1, iter.ReadInt())
should.False(iter.ReadArray())
2017-06-17 04:21:37 +02:00
iter = ParseString(ConfigDefault, `[1]`)
2017-01-20 07:54:51 +02:00
iter.ReadArrayCB(func(iter *Iterator) bool {
should.Equal(1, iter.ReadInt())
return true
})
2016-12-01 07:53:40 +02:00
}
func Test_two_elements(t *testing.T) {
2017-01-21 10:09:38 +02:00
should := require.New(t)
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `[1,2]`)
2017-01-21 10:09:38 +02:00
should.True(iter.ReadArray())
should.Equal(int64(1), iter.ReadInt64())
should.True(iter.ReadArray())
should.Equal(int64(2), iter.ReadInt64())
should.False(iter.ReadArray())
2017-06-17 04:21:37 +02:00
iter = ParseString(ConfigDefault, `[1,2]`)
2017-01-21 10:09:38 +02:00
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
2016-12-01 07:53:40 +02:00
}
func Test_whitespace_in_head(t *testing.T) {
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, ` [1]`)
2016-12-01 07:53:40 +02:00
cont := iter.ReadArray()
if cont != true {
t.FailNow()
}
if iter.ReadUint64() != 1 {
t.FailNow()
}
}
func Test_whitespace_after_array_start(t *testing.T) {
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `[ 1]`)
2016-12-01 07:53:40 +02:00
cont := iter.ReadArray()
if cont != true {
t.FailNow()
}
if iter.ReadUint64() != 1 {
t.FailNow()
}
}
func Test_whitespace_before_array_end(t *testing.T) {
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `[1 ]`)
2016-12-01 07:53:40 +02:00
cont := iter.ReadArray()
if cont != true {
t.FailNow()
}
if iter.ReadUint64() != 1 {
t.FailNow()
}
cont = iter.ReadArray()
if cont != false {
t.FailNow()
}
}
func Test_whitespace_before_comma(t *testing.T) {
2017-06-17 04:21:37 +02:00
iter := ParseString(ConfigDefault, `[1 ,2]`)
2016-12-01 07:53:40 +02:00
cont := iter.ReadArray()
if cont != true {
t.FailNow()
}
if iter.ReadUint64() != 1 {
t.FailNow()
}
cont = iter.ReadArray()
if cont != true {
t.FailNow()
}
if iter.ReadUint64() != 2 {
t.FailNow()
}
cont = iter.ReadArray()
if cont != false {
t.FailNow()
}
}
2017-01-07 16:16:20 +02:00
func Test_write_array(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
2017-01-07 16:16:20 +02:00
stream.WriteArrayStart()
stream.WriteInt(1)
stream.WriteMore()
stream.WriteInt(2)
stream.WriteArrayEnd()
stream.Flush()
should.Nil(stream.Error)
should.Equal("[\n 1,\n 2\n]", buf.String())
}
2017-01-09 13:48:57 +02:00
func Test_write_val_array(t *testing.T) {
should := require.New(t)
2017-01-20 07:54:51 +02:00
val := []int{1, 2, 3}
str, err := MarshalToString(&val)
2017-01-09 13:48:57 +02:00
should.Nil(err)
should.Equal("[1,2,3]", str)
}
func Test_write_val_empty_array(t *testing.T) {
should := require.New(t)
val := []int{}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal("[]", str)
}
2017-02-03 12:44:54 +02:00
func Test_write_array_of_interface_in_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
2017-06-06 17:27:00 +02:00
Field []interface{}
2017-02-03 12:44:54 +02:00
Field2 string
}
val := TestObject{[]interface{}{1, 2}, ""}
str, err := MarshalToString(val)
should.Nil(err)
2017-06-02 11:34:40 +02:00
should.Contains(str, `"Field":[1,2]`)
should.Contains(str, `"Field2":""`)
2017-02-03 12:44:54 +02:00
}
2017-06-06 03:44:56 +02:00
func Test_encode_byte_array(t *testing.T) {
should := require.New(t)
2017-06-06 17:27:00 +02:00
bytes, err := json.Marshal([]byte{1, 2, 3})
2017-06-06 03:44:56 +02:00
should.Nil(err)
should.Equal(`"AQID"`, string(bytes))
2017-06-06 17:27:00 +02:00
bytes, err = Marshal([]byte{1, 2, 3})
2017-06-06 03:44:56 +02:00
should.Nil(err)
should.Equal(`"AQID"`, string(bytes))
}
func Test_decode_byte_array(t *testing.T) {
should := require.New(t)
data := []byte{}
err := json.Unmarshal([]byte(`"AQID"`), &data)
should.Nil(err)
2017-06-06 17:27:00 +02:00
should.Equal([]byte{1, 2, 3}, data)
2017-06-06 03:44:56 +02:00
err = Unmarshal([]byte(`"AQID"`), &data)
should.Nil(err)
2017-06-06 17:27:00 +02:00
should.Equal([]byte{1, 2, 3}, data)
2017-06-06 03:44:56 +02:00
}
2017-06-17 15:32:48 +02:00
func Test_decode_slice(t *testing.T) {
should := require.New(t)
slice := make([]string, 0, 5)
UnmarshalFromString(`["hello", "world"]`, &slice)
should.Equal([]string{"hello", "world"}, slice)
}
func Test_decode_large_slice(t *testing.T) {
should := require.New(t)
slice := make([]int, 0, 1)
UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
}
2016-12-01 07:53:40 +02:00
func Benchmark_jsoniter_array(b *testing.B) {
2016-12-06 07:48:03 +02:00
b.ReportAllocs()
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
2017-06-17 04:21:37 +02:00
iter := ParseBytes(ConfigDefault, input)
2016-12-06 07:48:03 +02:00
b.ResetTimer()
2016-12-01 07:53:40 +02:00
for n := 0; n < b.N; n++ {
2016-12-10 08:34:36 +02:00
iter.ResetBytes(input)
2016-12-01 07:53:40 +02:00
for iter.ReadArray() {
iter.ReadUint64()
}
}
}
func Benchmark_json_array(b *testing.B) {
for n := 0; n < b.N; n++ {
result := []interface{}{}
json.Unmarshal([]byte(`[1,2,3]`), &result)
}
}