2016-12-01 18:13:13 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-06-19 17:43:53 +02:00
|
|
|
"bytes"
|
2016-12-01 18:13:13 +02:00
|
|
|
"encoding/json"
|
2017-06-19 17:43:53 +02:00
|
|
|
"testing"
|
2017-07-06 09:24:06 +02:00
|
|
|
|
2017-07-07 03:13:25 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-01 18:13:13 +02:00
|
|
|
)
|
|
|
|
|
2017-07-18 03:01:43 +02:00
|
|
|
func Test_skip_number_in_array(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[-0.12, "stream"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-18 03:01:43 +02:00
|
|
|
should.Nil(iter.Error)
|
|
|
|
should.Equal("stream", iter.ReadString())
|
|
|
|
}
|
2017-07-06 09:24:06 +02:00
|
|
|
|
2017-07-18 03:01:43 +02:00
|
|
|
func Test_skip_string_in_array(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
iter := ParseString(ConfigDefault, `["hello", "stream"]`)
|
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
should.Nil(iter.Error)
|
|
|
|
should.Equal("stream", iter.ReadString())
|
2016-12-01 18:13:13 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
func Test_skip_null(t *testing.T) {
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[null , "stream"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-09 08:48:34 +02:00
|
|
|
if iter.ReadString() != "stream" {
|
2016-12-01 18:13:13 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
func Test_skip_true(t *testing.T) {
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[true , "stream"]`)
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-09 08:48:34 +02:00
|
|
|
if iter.ReadString() != "stream" {
|
2016-12-06 17:31:11 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_skip_false(t *testing.T) {
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[false , "stream"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-09 08:48:34 +02:00
|
|
|
if iter.ReadString() != "stream" {
|
2016-12-01 18:13:13 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_skip_array(t *testing.T) {
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[[1, [2, [3], 4]], "stream"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-09 08:48:34 +02:00
|
|
|
if iter.ReadString() != "stream" {
|
2016-12-01 18:13:13 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 05:22:20 +02:00
|
|
|
func Test_skip_empty_array(t *testing.T) {
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[ [ ], "stream"]`)
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-09 08:48:34 +02:00
|
|
|
if iter.ReadString() != "stream" {
|
2016-12-02 05:22:20 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:13:13 +02:00
|
|
|
func Test_skip_nested(t *testing.T) {
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`)
|
2016-12-06 15:28:39 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
2017-07-09 08:48:34 +02:00
|
|
|
if iter.ReadString() != "stream" {
|
2016-12-06 15:28:39 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2016-12-01 18:13:13 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 10:28:43 +02:00
|
|
|
func Test_skip_and_return_bytes(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := ParseString(ConfigDefault, `[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`)
|
2017-06-18 10:28:43 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
skipped := iter.SkipAndReturnBytes()
|
2017-07-09 08:48:34 +02:00
|
|
|
should.Equal(`{"a" : [{"stream": "c"}], "d": 102 }`, string(skipped))
|
2017-06-18 10:28:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_skip_and_return_bytes_with_reader(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-07-09 08:48:34 +02:00
|
|
|
iter := Parse(ConfigDefault, bytes.NewBufferString(`[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`), 4)
|
2017-06-18 10:28:43 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
skipped := iter.SkipAndReturnBytes()
|
2017-07-09 08:48:34 +02:00
|
|
|
should.Equal(`{"a" : [{"stream": "c"}], "d": 102 }`, string(skipped))
|
2017-06-18 10:28:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 16:47:02 +02:00
|
|
|
func Test_skip_empty(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
should.NotNil(Get([]byte("")).LastError())
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:13:13 +02:00
|
|
|
type TestResp struct {
|
|
|
|
Code uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_jsoniter_skip(b *testing.B) {
|
2016-12-02 05:22:20 +02:00
|
|
|
input := []byte(`
|
2016-12-01 18:13:13 +02:00
|
|
|
{
|
|
|
|
"_shards":{
|
|
|
|
"total" : 5,
|
|
|
|
"successful" : 5,
|
|
|
|
"failed" : 0
|
|
|
|
},
|
|
|
|
"hits":{
|
|
|
|
"total" : 1,
|
|
|
|
"hits" : [
|
|
|
|
{
|
|
|
|
"_index" : "twitter",
|
|
|
|
"_type" : "tweet",
|
|
|
|
"_id" : "1",
|
|
|
|
"_source" : {
|
|
|
|
"user" : "kimchy",
|
|
|
|
"postDate" : "2009-11-15T14:12:12",
|
|
|
|
"message" : "trying out Elasticsearch"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-12-02 05:22:20 +02:00
|
|
|
},
|
|
|
|
"code": 200
|
2016-12-01 18:13:13 +02:00
|
|
|
}`)
|
2016-12-02 05:22:20 +02:00
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
result := TestResp{}
|
2017-06-17 04:21:37 +02:00
|
|
|
iter := ParseBytes(ConfigDefault, input)
|
2016-12-01 18:13:13 +02:00
|
|
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
|
|
switch field {
|
|
|
|
case "code":
|
|
|
|
result.Code = iter.ReadUint64()
|
|
|
|
default:
|
|
|
|
iter.Skip()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_json_skip(b *testing.B) {
|
2016-12-02 05:22:20 +02:00
|
|
|
input := []byte(`
|
2016-12-01 18:13:13 +02:00
|
|
|
{
|
|
|
|
"_shards":{
|
|
|
|
"total" : 5,
|
|
|
|
"successful" : 5,
|
|
|
|
"failed" : 0
|
|
|
|
},
|
|
|
|
"hits":{
|
|
|
|
"total" : 1,
|
|
|
|
"hits" : [
|
|
|
|
{
|
|
|
|
"_index" : "twitter",
|
|
|
|
"_type" : "tweet",
|
|
|
|
"_id" : "1",
|
|
|
|
"_source" : {
|
|
|
|
"user" : "kimchy",
|
|
|
|
"postDate" : "2009-11-15T14:12:12",
|
|
|
|
"message" : "trying out Elasticsearch"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-12-02 05:22:20 +02:00
|
|
|
},
|
|
|
|
"code": 200
|
|
|
|
}`)
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
result := TestResp{}
|
|
|
|
json.Unmarshal(input, &result)
|
2016-12-01 18:13:13 +02:00
|
|
|
}
|
2017-01-05 15:23:08 +02:00
|
|
|
}
|