jsoniter_skip_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func Test_skip_number(t *testing.T) {
  7. iter := ParseString(`[-0.12, "b"]`)
  8. iter.ReadArray()
  9. iter.Skip()
  10. iter.ReadArray()
  11. if iter.ReadString() != "b" {
  12. t.FailNow()
  13. }
  14. }
  15. func Test_skip_null(t *testing.T) {
  16. iter := ParseString(`[null , "b"]`)
  17. iter.ReadArray()
  18. iter.Skip()
  19. iter.ReadArray()
  20. if iter.ReadString() != "b" {
  21. t.FailNow()
  22. }
  23. }
  24. func Test_skip_true(t *testing.T) {
  25. iter := ParseString(`[true , "b"]`)
  26. iter.ReadArray()
  27. iter.Skip()
  28. iter.ReadArray()
  29. if iter.ReadString() != "b" {
  30. t.FailNow()
  31. }
  32. }
  33. func Test_skip_false(t *testing.T) {
  34. iter := ParseString(`[false , "b"]`)
  35. iter.ReadArray()
  36. iter.Skip()
  37. iter.ReadArray()
  38. if iter.ReadString() != "b" {
  39. t.FailNow()
  40. }
  41. }
  42. func Test_skip_array(t *testing.T) {
  43. iter := ParseString(`[[1, [2, [3], 4]], "b"]`)
  44. iter.ReadArray()
  45. iter.Skip()
  46. iter.ReadArray()
  47. if iter.ReadString() != "b" {
  48. t.FailNow()
  49. }
  50. }
  51. func Test_skip_empty_array(t *testing.T) {
  52. iter := ParseString(`[ [ ], "b"]`)
  53. iter.ReadArray()
  54. iter.Skip()
  55. iter.ReadArray()
  56. if iter.ReadString() != "b" {
  57. t.FailNow()
  58. }
  59. }
  60. func Test_skip_nested(t *testing.T) {
  61. iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
  62. iter.ReadArray()
  63. iter.Skip()
  64. iter.ReadArray()
  65. if iter.ReadString() != "b" {
  66. t.FailNow()
  67. }
  68. }
  69. type TestResp struct {
  70. Code uint64
  71. }
  72. func Benchmark_jsoniter_skip(b *testing.B) {
  73. input := []byte(`
  74. {
  75. "_shards":{
  76. "total" : 5,
  77. "successful" : 5,
  78. "failed" : 0
  79. },
  80. "hits":{
  81. "total" : 1,
  82. "hits" : [
  83. {
  84. "_index" : "twitter",
  85. "_type" : "tweet",
  86. "_id" : "1",
  87. "_source" : {
  88. "user" : "kimchy",
  89. "postDate" : "2009-11-15T14:12:12",
  90. "message" : "trying out Elasticsearch"
  91. }
  92. }
  93. ]
  94. },
  95. "code": 200
  96. }`)
  97. for n := 0; n < b.N; n++ {
  98. result := TestResp{}
  99. iter := ParseBytes(input)
  100. for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  101. switch field {
  102. case "code":
  103. result.Code = iter.ReadUint64()
  104. default:
  105. iter.Skip()
  106. }
  107. }
  108. }
  109. }
  110. func Benchmark_json_skip(b *testing.B) {
  111. input := []byte(`
  112. {
  113. "_shards":{
  114. "total" : 5,
  115. "successful" : 5,
  116. "failed" : 0
  117. },
  118. "hits":{
  119. "total" : 1,
  120. "hits" : [
  121. {
  122. "_index" : "twitter",
  123. "_type" : "tweet",
  124. "_id" : "1",
  125. "_source" : {
  126. "user" : "kimchy",
  127. "postDate" : "2009-11-15T14:12:12",
  128. "message" : "trying out Elasticsearch"
  129. }
  130. }
  131. ]
  132. },
  133. "code": 200
  134. }`)
  135. for n := 0; n < b.N; n++ {
  136. result := TestResp{}
  137. json.Unmarshal(input, &result)
  138. }
  139. }