...

Source file src/github.com/json-iterator/go/skip_tests/jsoniter_skip_test.go

Documentation: github.com/json-iterator/go/skip_tests

     1  package skip_tests
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/json-iterator/go"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func Test_skip_number_in_array(t *testing.T) {
    13  	should := require.New(t)
    14  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[-0.12, "stream"]`)
    15  	iter.ReadArray()
    16  	iter.Skip()
    17  	iter.ReadArray()
    18  	should.Nil(iter.Error)
    19  	should.Equal("stream", iter.ReadString())
    20  }
    21  
    22  func Test_skip_string_in_array(t *testing.T) {
    23  	should := require.New(t)
    24  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `["hello", "stream"]`)
    25  	iter.ReadArray()
    26  	iter.Skip()
    27  	iter.ReadArray()
    28  	should.Nil(iter.Error)
    29  	should.Equal("stream", iter.ReadString())
    30  }
    31  
    32  func Test_skip_null(t *testing.T) {
    33  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null , "stream"]`)
    34  	iter.ReadArray()
    35  	iter.Skip()
    36  	iter.ReadArray()
    37  	if iter.ReadString() != "stream" {
    38  		t.FailNow()
    39  	}
    40  }
    41  
    42  func Test_skip_true(t *testing.T) {
    43  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[true , "stream"]`)
    44  	iter.ReadArray()
    45  	iter.Skip()
    46  	iter.ReadArray()
    47  	if iter.ReadString() != "stream" {
    48  		t.FailNow()
    49  	}
    50  }
    51  
    52  func Test_skip_false(t *testing.T) {
    53  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[false , "stream"]`)
    54  	iter.ReadArray()
    55  	iter.Skip()
    56  	iter.ReadArray()
    57  	if iter.ReadString() != "stream" {
    58  		t.FailNow()
    59  	}
    60  }
    61  
    62  func Test_skip_array(t *testing.T) {
    63  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[[1, [2, [3], 4]], "stream"]`)
    64  	iter.ReadArray()
    65  	iter.Skip()
    66  	iter.ReadArray()
    67  	if iter.ReadString() != "stream" {
    68  		t.FailNow()
    69  	}
    70  }
    71  
    72  func Test_skip_empty_array(t *testing.T) {
    73  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[ [ ], "stream"]`)
    74  	iter.ReadArray()
    75  	iter.Skip()
    76  	iter.ReadArray()
    77  	if iter.ReadString() != "stream" {
    78  		t.FailNow()
    79  	}
    80  }
    81  
    82  func Test_skip_nested(t *testing.T) {
    83  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`)
    84  	iter.ReadArray()
    85  	iter.Skip()
    86  	iter.ReadArray()
    87  	if iter.ReadString() != "stream" {
    88  		t.FailNow()
    89  	}
    90  }
    91  
    92  func Test_skip_and_return_bytes(t *testing.T) {
    93  	should := require.New(t)
    94  	iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`)
    95  	iter.ReadArray()
    96  	skipped := iter.SkipAndReturnBytes()
    97  	should.Equal(`{"a" : [{"stream": "c"}], "d": 102 }`, string(skipped))
    98  }
    99  
   100  func Test_skip_and_return_bytes_with_reader(t *testing.T) {
   101  	should := require.New(t)
   102  	iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(`[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`), 4)
   103  	iter.ReadArray()
   104  	skipped := iter.SkipAndReturnBytes()
   105  	should.Equal(`{"a" : [{"stream": "c"}], "d": 102 }`, string(skipped))
   106  }
   107  
   108  func Test_append_skip_and_return_bytes_with_reader(t *testing.T) {
   109  	should := require.New(t)
   110  	iter := jsoniter.Parse(jsoniter.ConfigDefault, bytes.NewBufferString(`[ {"a" : [{"stream": "c"}], "d": 102 }, "stream"]`), 4)
   111  	iter.ReadArray()
   112  	buf := make([]byte, 0, 1024)
   113  	buf = iter.SkipAndAppendBytes(buf)
   114  	should.Equal(`{"a" : [{"stream": "c"}], "d": 102 }`, string(buf))
   115  }
   116  
   117  func Test_skip_empty(t *testing.T) {
   118  	should := require.New(t)
   119  	should.NotNil(jsoniter.Get([]byte("")).LastError())
   120  }
   121  
   122  type TestResp struct {
   123  	Code uint64
   124  }
   125  
   126  func Benchmark_jsoniter_skip(b *testing.B) {
   127  	input := []byte(`
   128  {
   129      "_shards":{
   130          "total" : 5,
   131          "successful" : 5,
   132          "failed" : 0
   133      },
   134      "hits":{
   135          "total" : 1,
   136          "hits" : [
   137              {
   138                  "_index" : "twitter",
   139                  "_type" : "tweet",
   140                  "_id" : "1",
   141                  "_source" : {
   142                      "user" : "kimchy",
   143                      "postDate" : "2009-11-15T14:12:12",
   144                      "message" : "trying out Elasticsearch"
   145                  }
   146              }
   147          ]
   148      },
   149      "code": 200
   150  }`)
   151  	for n := 0; n < b.N; n++ {
   152  		result := TestResp{}
   153  		iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, input)
   154  		for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
   155  			switch field {
   156  			case "code":
   157  				result.Code = iter.ReadUint64()
   158  			default:
   159  				iter.Skip()
   160  			}
   161  		}
   162  	}
   163  }
   164  
   165  func Benchmark_json_skip(b *testing.B) {
   166  	input := []byte(`
   167  {
   168      "_shards":{
   169          "total" : 5,
   170          "successful" : 5,
   171          "failed" : 0
   172      },
   173      "hits":{
   174          "total" : 1,
   175          "hits" : [
   176              {
   177                  "_index" : "twitter",
   178                  "_type" : "tweet",
   179                  "_id" : "1",
   180                  "_source" : {
   181                      "user" : "kimchy",
   182                      "postDate" : "2009-11-15T14:12:12",
   183                      "message" : "trying out Elasticsearch"
   184                  }
   185              }
   186          ]
   187      },
   188      "code": 200
   189  }`)
   190  	for n := 0; n < b.N; n++ {
   191  		result := TestResp{}
   192  		json.Unmarshal(input, &result)
   193  	}
   194  }
   195  

View as plain text