1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/jsoniter_skip_test.go
2016-12-02 00:13:13 +08:00

138 lines
2.6 KiB
Go

package jsoniter
import (
"testing"
"encoding/json"
)
func Test_skip_string(t *testing.T) {
iter := ParseString(`["a", "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
if iter.ReadString() != "b" {
t.FailNow()
}
}
func Test_skip_string_with_escape(t *testing.T) {
iter := ParseString(`["a\"", "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
if iter.ReadString() != "b" {
t.FailNow()
}
}
func Test_skip_number(t *testing.T) {
iter := ParseString(`[-0.12, "b"]`)
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()
}
}
func Test_skip_object(t *testing.T) {
iter := ParseString(`[ {"a" : {"b": "c"}, "d": 102 }, "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
if iter.ReadString() != "b" {
t.FailNow()
}
}
func Test_skip_nested(t *testing.T) {
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
iter.ReadArray()
iter.Skip()
iter.ReadArray()
if iter.ReadString() != "b" {
t.FailNow()
}
}
type TestResp struct {
Code uint64
}
func Benchmark_jsoniter_skip(b *testing.B) {
for n := 0; n < b.N; n++ {
result := TestResp{}
iter := ParseString(`
{
"_shards":{
"total" : 5,
"successful" : 5,
"failed" : 0
},
"code": 200,
"hits":{
"total" : 1,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_source" : {
"user" : "kimchy",
"postDate" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
]
}
}`)
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) {
for n := 0; n < b.N; n++ {
result := TestResp{}
json.Unmarshal([]byte(`
{
"_shards":{
"total" : 5,
"successful" : 5,
"failed" : 0
},
"code": 200,
"hits":{
"total" : 1,
"hits" : [
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_source" : {
"user" : "kimchy",
"postDate" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
]
}
}`), &result)
}
}