jsoniter_array_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/json-iterator/go/require"
  6. "bytes"
  7. "io"
  8. )
  9. func Test_empty_array(t *testing.T) {
  10. should := require.New(t)
  11. iter := ParseString(`[]`)
  12. cont := iter.ReadArray()
  13. should.False(cont)
  14. iter = ParseString(`[]`)
  15. iter.ReadArrayCB(func(iter *Iterator) bool {
  16. should.FailNow("should not call")
  17. return true
  18. })
  19. }
  20. func Test_one_element(t *testing.T) {
  21. should := require.New(t)
  22. iter := ParseString(`[1]`)
  23. should.True(iter.ReadArray())
  24. should.Equal(1, iter.ReadInt())
  25. should.False(iter.ReadArray())
  26. iter = ParseString(`[1]`)
  27. iter.ReadArrayCB(func(iter *Iterator) bool {
  28. should.Equal(1, iter.ReadInt())
  29. return true
  30. })
  31. }
  32. func Test_two_elements(t *testing.T) {
  33. should := require.New(t)
  34. iter := ParseString(`[1,2]`)
  35. should.True(iter.ReadArray())
  36. should.Equal(int64(1), iter.ReadInt64())
  37. should.True(iter.ReadArray())
  38. should.Equal(int64(2), iter.ReadInt64())
  39. should.False(iter.ReadArray())
  40. iter = ParseString(`[1,2]`)
  41. should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
  42. }
  43. func Test_read_empty_array_as_any(t *testing.T) {
  44. should := require.New(t)
  45. any, err := UnmarshalAnyFromString("[]")
  46. should.Nil(err)
  47. should.Equal(0, any.Size())
  48. }
  49. func Test_read_one_element_array_as_any(t *testing.T) {
  50. should := require.New(t)
  51. any, err := UnmarshalAnyFromString("[1]")
  52. should.Nil(err)
  53. should.Equal(1, any.Size())
  54. }
  55. func Test_read_two_element_array_as_any(t *testing.T) {
  56. should := require.New(t)
  57. any, err := UnmarshalAnyFromString("[1,2]")
  58. should.Nil(err)
  59. should.Equal(1, any.Get(0).ToInt())
  60. should.Equal(2, any.Size())
  61. should.True(any.ToBool())
  62. should.Equal(1, any.ToInt())
  63. }
  64. func Test_read_array_with_any_iterator(t *testing.T) {
  65. should := require.New(t)
  66. any, err := UnmarshalAnyFromString("[1,2]")
  67. should.Nil(err)
  68. var element Any
  69. var elements []int
  70. for next, hasNext := any.IterateArray(); hasNext; {
  71. element, hasNext = next()
  72. elements = append(elements, element.ToInt())
  73. }
  74. should.Equal([]int{1, 2}, elements)
  75. }
  76. func Test_wrap_array(t *testing.T) {
  77. should := require.New(t)
  78. any := Wrap([]int{1,2,3})
  79. should.Equal("[1,2,3]", any.ToString())
  80. var element Any
  81. var elements []int
  82. for next, hasNext := any.IterateArray(); hasNext; {
  83. element, hasNext = next()
  84. elements = append(elements, element.ToInt())
  85. }
  86. should.Equal([]int{1, 2, 3}, elements)
  87. any = Wrap([]int{1,2,3})
  88. should.Equal(3, any.Size())
  89. any = Wrap([]int{1,2,3})
  90. should.Equal(2, any.Get(1).ToInt())
  91. }
  92. func Test_array_lazy_any_get(t *testing.T) {
  93. should := require.New(t)
  94. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  95. should.Nil(err)
  96. should.Equal(3, any.Get(1,1).ToInt())
  97. should.Equal("[1,[2,3],4]", any.ToString())
  98. }
  99. func Test_array_lazy_any_get_all(t *testing.T) {
  100. should := require.New(t)
  101. any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
  102. should.Nil(err)
  103. should.Equal("[1,2,3]", any.Get('*',0).ToString())
  104. }
  105. func Test_array_lazy_any_get_invalid(t *testing.T) {
  106. should := require.New(t)
  107. any, err := UnmarshalAnyFromString("[]")
  108. should.Nil(err)
  109. should.Equal(Invalid, any.Get(1,1).ValueType())
  110. should.NotNil(any.Get(1,1).LastError())
  111. should.Equal(Invalid, any.Get("1").ValueType())
  112. should.NotNil(any.Get("1").LastError())
  113. }
  114. func Test_array_lazy_any_set(t *testing.T) {
  115. should := require.New(t)
  116. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  117. should.Nil(err)
  118. any.GetArray()[0] = WrapInt64(2)
  119. str, err := MarshalToString(any)
  120. should.Nil(err)
  121. should.Equal("[2,[2,3],4]", str)
  122. }
  123. func Test_invalid_array(t *testing.T) {
  124. _, err := UnmarshalAnyFromString("[")
  125. if err == nil || err == io.EOF {
  126. t.FailNow()
  127. }
  128. }
  129. func Test_whitespace_in_head(t *testing.T) {
  130. iter := ParseString(` [1]`)
  131. cont := iter.ReadArray()
  132. if cont != true {
  133. t.FailNow()
  134. }
  135. if iter.ReadUint64() != 1 {
  136. t.FailNow()
  137. }
  138. }
  139. func Test_whitespace_after_array_start(t *testing.T) {
  140. iter := ParseString(`[ 1]`)
  141. cont := iter.ReadArray()
  142. if cont != true {
  143. t.FailNow()
  144. }
  145. if iter.ReadUint64() != 1 {
  146. t.FailNow()
  147. }
  148. }
  149. func Test_whitespace_before_array_end(t *testing.T) {
  150. iter := ParseString(`[1 ]`)
  151. cont := iter.ReadArray()
  152. if cont != true {
  153. t.FailNow()
  154. }
  155. if iter.ReadUint64() != 1 {
  156. t.FailNow()
  157. }
  158. cont = iter.ReadArray()
  159. if cont != false {
  160. t.FailNow()
  161. }
  162. }
  163. func Test_whitespace_before_comma(t *testing.T) {
  164. iter := ParseString(`[1 ,2]`)
  165. cont := iter.ReadArray()
  166. if cont != true {
  167. t.FailNow()
  168. }
  169. if iter.ReadUint64() != 1 {
  170. t.FailNow()
  171. }
  172. cont = iter.ReadArray()
  173. if cont != true {
  174. t.FailNow()
  175. }
  176. if iter.ReadUint64() != 2 {
  177. t.FailNow()
  178. }
  179. cont = iter.ReadArray()
  180. if cont != false {
  181. t.FailNow()
  182. }
  183. }
  184. func Test_write_array(t *testing.T) {
  185. should := require.New(t)
  186. buf := &bytes.Buffer{}
  187. stream := NewStream(buf, 4096)
  188. stream.IndentionStep = 2
  189. stream.WriteArrayStart()
  190. stream.WriteInt(1)
  191. stream.WriteMore()
  192. stream.WriteInt(2)
  193. stream.WriteArrayEnd()
  194. stream.Flush()
  195. should.Nil(stream.Error)
  196. should.Equal("[\n 1,\n 2\n]", buf.String())
  197. }
  198. func Test_write_val_array(t *testing.T) {
  199. should := require.New(t)
  200. val := []int{1, 2, 3}
  201. str, err := MarshalToString(val)
  202. should.Nil(err)
  203. should.Equal("[1,2,3]", str)
  204. }
  205. func Test_write_val_empty_array(t *testing.T) {
  206. should := require.New(t)
  207. val := []int{}
  208. str, err := MarshalToString(val)
  209. should.Nil(err)
  210. should.Equal("[]", str)
  211. }
  212. func Benchmark_jsoniter_array(b *testing.B) {
  213. b.ReportAllocs()
  214. input := []byte(`[1,2,3,4,5,6,7,8,9]`)
  215. iter := ParseBytes(input)
  216. b.ResetTimer()
  217. for n := 0; n < b.N; n++ {
  218. iter.ResetBytes(input)
  219. for iter.ReadArray() {
  220. iter.ReadUint64()
  221. }
  222. }
  223. }
  224. func Benchmark_json_array(b *testing.B) {
  225. for n := 0; n < b.N; n++ {
  226. result := []interface{}{}
  227. json.Unmarshal([]byte(`[1,2,3]`), &result)
  228. }
  229. }