2016-12-01 18:13:13 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-01-05 15:23:08 +02:00
|
|
|
"testing"
|
2016-12-01 18:13:13 +02:00
|
|
|
)
|
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
func Test_skip_number(t *testing.T) {
|
|
|
|
iter := ParseString(`[-0.12, "b"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
func Test_skip_null(t *testing.T) {
|
|
|
|
iter := ParseString(`[null , "b"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
func Test_skip_true(t *testing.T) {
|
|
|
|
iter := ParseString(`[true , "b"]`)
|
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_skip_false(t *testing.T) {
|
|
|
|
iter := ParseString(`[false , "b"]`)
|
2016-12-01 18:13:13 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_skip_array(t *testing.T) {
|
|
|
|
iter := ParseString(`[[1, [2, [3], 4]], "b"]`)
|
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 05:22:20 +02:00
|
|
|
func Test_skip_empty_array(t *testing.T) {
|
|
|
|
iter := ParseString(`[ [ ], "b"]`)
|
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:13:13 +02:00
|
|
|
func Test_skip_nested(t *testing.T) {
|
2016-12-06 15:28:39 +02:00
|
|
|
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "b" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
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{}
|
|
|
|
iter := ParseBytes(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
|
|
|
}
|