jsoniter_array_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_array_lazy_any_get(t *testing.T) {
  77. should := require.New(t)
  78. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  79. should.Nil(err)
  80. should.Equal(3, any.Get(1,1).ToInt())
  81. should.Equal("[1,[2,3],4]", any.ToString())
  82. }
  83. func Test_array_lazy_any_get_all(t *testing.T) {
  84. should := require.New(t)
  85. any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
  86. should.Nil(err)
  87. should.Equal("[1,2,3]", any.Get('*',0).ToString())
  88. }
  89. func Test_array_lazy_any_get_invalid(t *testing.T) {
  90. should := require.New(t)
  91. any, err := UnmarshalAnyFromString("[]")
  92. should.Nil(err)
  93. should.Equal(Invalid, any.Get(1,1).ValueType())
  94. should.NotNil(any.Get(1,1).LastError())
  95. should.Equal(Invalid, any.Get("1").ValueType())
  96. should.NotNil(any.Get("1").LastError())
  97. }
  98. func Test_array_lazy_any_set(t *testing.T) {
  99. should := require.New(t)
  100. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  101. should.Nil(err)
  102. any.GetArray()[0] = WrapInt64(2)
  103. str, err := MarshalToString(any)
  104. should.Nil(err)
  105. should.Equal("[2,[2,3],4]", str)
  106. }
  107. func Test_invalid_array(t *testing.T) {
  108. _, err := UnmarshalAnyFromString("[")
  109. if err == nil || err == io.EOF {
  110. t.FailNow()
  111. }
  112. }
  113. func Test_whitespace_in_head(t *testing.T) {
  114. iter := ParseString(` [1]`)
  115. cont := iter.ReadArray()
  116. if cont != true {
  117. t.FailNow()
  118. }
  119. if iter.ReadUint64() != 1 {
  120. t.FailNow()
  121. }
  122. }
  123. func Test_whitespace_after_array_start(t *testing.T) {
  124. iter := ParseString(`[ 1]`)
  125. cont := iter.ReadArray()
  126. if cont != true {
  127. t.FailNow()
  128. }
  129. if iter.ReadUint64() != 1 {
  130. t.FailNow()
  131. }
  132. }
  133. func Test_whitespace_before_array_end(t *testing.T) {
  134. iter := ParseString(`[1 ]`)
  135. cont := iter.ReadArray()
  136. if cont != true {
  137. t.FailNow()
  138. }
  139. if iter.ReadUint64() != 1 {
  140. t.FailNow()
  141. }
  142. cont = iter.ReadArray()
  143. if cont != false {
  144. t.FailNow()
  145. }
  146. }
  147. func Test_whitespace_before_comma(t *testing.T) {
  148. iter := ParseString(`[1 ,2]`)
  149. cont := iter.ReadArray()
  150. if cont != true {
  151. t.FailNow()
  152. }
  153. if iter.ReadUint64() != 1 {
  154. t.FailNow()
  155. }
  156. cont = iter.ReadArray()
  157. if cont != true {
  158. t.FailNow()
  159. }
  160. if iter.ReadUint64() != 2 {
  161. t.FailNow()
  162. }
  163. cont = iter.ReadArray()
  164. if cont != false {
  165. t.FailNow()
  166. }
  167. }
  168. func Test_write_array(t *testing.T) {
  169. should := require.New(t)
  170. buf := &bytes.Buffer{}
  171. stream := NewStream(buf, 4096)
  172. stream.IndentionStep = 2
  173. stream.WriteArrayStart()
  174. stream.WriteInt(1)
  175. stream.WriteMore()
  176. stream.WriteInt(2)
  177. stream.WriteArrayEnd()
  178. stream.Flush()
  179. should.Nil(stream.Error)
  180. should.Equal("[\n 1,\n 2\n]", buf.String())
  181. }
  182. func Test_write_val_array(t *testing.T) {
  183. should := require.New(t)
  184. val := []int{1, 2, 3}
  185. str, err := MarshalToString(val)
  186. should.Nil(err)
  187. should.Equal("[1,2,3]", str)
  188. }
  189. func Test_write_val_empty_array(t *testing.T) {
  190. should := require.New(t)
  191. val := []int{}
  192. str, err := MarshalToString(val)
  193. should.Nil(err)
  194. should.Equal("[]", str)
  195. }
  196. func Benchmark_jsoniter_array(b *testing.B) {
  197. b.ReportAllocs()
  198. input := []byte(`[1,2,3,4,5,6,7,8,9]`)
  199. iter := ParseBytes(input)
  200. b.ResetTimer()
  201. for n := 0; n < b.N; n++ {
  202. iter.ResetBytes(input)
  203. for iter.ReadArray() {
  204. iter.ReadUint64()
  205. }
  206. }
  207. }
  208. func Benchmark_json_array(b *testing.B) {
  209. for n := 0; n < b.N; n++ {
  210. result := []interface{}{}
  211. json.Unmarshal([]byte(`[1,2,3]`), &result)
  212. }
  213. }