jsoniter_array_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/json-iterator/go/require"
  6. "io"
  7. "testing"
  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_wrapper_any_get_all(t *testing.T) {
  106. should := require.New(t)
  107. any := wrapArray([][]int{
  108. {1, 2},
  109. {3, 4},
  110. {5, 6},
  111. })
  112. should.Equal("[1,3,5]", any.Get('*', 0).ToString())
  113. }
  114. func Test_array_lazy_any_get_invalid(t *testing.T) {
  115. should := require.New(t)
  116. any, err := UnmarshalAnyFromString("[]")
  117. should.Nil(err)
  118. should.Equal(Invalid, any.Get(1, 1).ValueType())
  119. should.NotNil(any.Get(1, 1).LastError())
  120. should.Equal(Invalid, any.Get("1").ValueType())
  121. should.NotNil(any.Get("1").LastError())
  122. }
  123. func Test_array_lazy_any_set(t *testing.T) {
  124. should := require.New(t)
  125. any, err := UnmarshalAnyFromString("[1,[2,3],4]")
  126. should.Nil(err)
  127. any.GetArray()[0] = WrapInt64(2)
  128. str, err := MarshalToString(any)
  129. should.Nil(err)
  130. should.Equal("[2,[2,3],4]", str)
  131. }
  132. func Test_invalid_array(t *testing.T) {
  133. _, err := UnmarshalAnyFromString("[")
  134. if err == nil || err == io.EOF {
  135. t.FailNow()
  136. }
  137. }
  138. func Test_whitespace_in_head(t *testing.T) {
  139. iter := ParseString(` [1]`)
  140. cont := iter.ReadArray()
  141. if cont != true {
  142. t.FailNow()
  143. }
  144. if iter.ReadUint64() != 1 {
  145. t.FailNow()
  146. }
  147. }
  148. func Test_whitespace_after_array_start(t *testing.T) {
  149. iter := ParseString(`[ 1]`)
  150. cont := iter.ReadArray()
  151. if cont != true {
  152. t.FailNow()
  153. }
  154. if iter.ReadUint64() != 1 {
  155. t.FailNow()
  156. }
  157. }
  158. func Test_whitespace_before_array_end(t *testing.T) {
  159. iter := ParseString(`[1 ]`)
  160. cont := iter.ReadArray()
  161. if cont != true {
  162. t.FailNow()
  163. }
  164. if iter.ReadUint64() != 1 {
  165. t.FailNow()
  166. }
  167. cont = iter.ReadArray()
  168. if cont != false {
  169. t.FailNow()
  170. }
  171. }
  172. func Test_whitespace_before_comma(t *testing.T) {
  173. iter := ParseString(`[1 ,2]`)
  174. cont := iter.ReadArray()
  175. if cont != true {
  176. t.FailNow()
  177. }
  178. if iter.ReadUint64() != 1 {
  179. t.FailNow()
  180. }
  181. cont = iter.ReadArray()
  182. if cont != true {
  183. t.FailNow()
  184. }
  185. if iter.ReadUint64() != 2 {
  186. t.FailNow()
  187. }
  188. cont = iter.ReadArray()
  189. if cont != false {
  190. t.FailNow()
  191. }
  192. }
  193. func Test_write_array(t *testing.T) {
  194. should := require.New(t)
  195. buf := &bytes.Buffer{}
  196. stream := NewStream(buf, 4096)
  197. stream.IndentionStep = 2
  198. stream.WriteArrayStart()
  199. stream.WriteInt(1)
  200. stream.WriteMore()
  201. stream.WriteInt(2)
  202. stream.WriteArrayEnd()
  203. stream.Flush()
  204. should.Nil(stream.Error)
  205. should.Equal("[\n 1,\n 2\n]", buf.String())
  206. }
  207. func Test_write_val_array(t *testing.T) {
  208. should := require.New(t)
  209. val := []int{1, 2, 3}
  210. str, err := MarshalToString(val)
  211. should.Nil(err)
  212. should.Equal("[1,2,3]", str)
  213. }
  214. func Test_write_val_empty_array(t *testing.T) {
  215. should := require.New(t)
  216. val := []int{}
  217. str, err := MarshalToString(val)
  218. should.Nil(err)
  219. should.Equal("[]", str)
  220. }
  221. func Test_write_array_of_interface_in_struct(t *testing.T) {
  222. should := require.New(t)
  223. type TestObject struct {
  224. Field []interface{}
  225. Field2 string
  226. }
  227. val := TestObject{[]interface{}{1, 2}, ""}
  228. str, err := MarshalToString(val)
  229. should.Nil(err)
  230. should.Contains(str, `"Field":[1,2]`)
  231. should.Contains(str, `"Field2":""`)
  232. }
  233. func Test_json_RawMessage(t *testing.T) {
  234. should := require.New(t)
  235. var data json.RawMessage
  236. should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
  237. should.Equal(`[1,2,3]`, string(data))
  238. str, err := MarshalToString(data)
  239. should.Nil(err)
  240. should.Equal(`[1,2,3]`, str)
  241. }
  242. func Test_encode_byte_array(t *testing.T) {
  243. should := require.New(t)
  244. bytes, err := json.Marshal([]byte{1, 2, 3})
  245. should.Nil(err)
  246. should.Equal(`"AQID"`, string(bytes))
  247. bytes, err = Marshal([]byte{1, 2, 3})
  248. should.Nil(err)
  249. should.Equal(`"AQID"`, string(bytes))
  250. }
  251. func Test_decode_byte_array(t *testing.T) {
  252. should := require.New(t)
  253. data := []byte{}
  254. err := json.Unmarshal([]byte(`"AQID"`), &data)
  255. should.Nil(err)
  256. should.Equal([]byte{1, 2, 3}, data)
  257. err = Unmarshal([]byte(`"AQID"`), &data)
  258. should.Nil(err)
  259. should.Equal([]byte{1, 2, 3}, data)
  260. }
  261. func Benchmark_jsoniter_array(b *testing.B) {
  262. b.ReportAllocs()
  263. input := []byte(`[1,2,3,4,5,6,7,8,9]`)
  264. iter := ParseBytes(input)
  265. b.ResetTimer()
  266. for n := 0; n < b.N; n++ {
  267. iter.ResetBytes(input)
  268. for iter.ReadArray() {
  269. iter.ReadUint64()
  270. }
  271. }
  272. }
  273. func Benchmark_json_array(b *testing.B) {
  274. for n := 0; n < b.N; n++ {
  275. result := []interface{}{}
  276. json.Unmarshal([]byte(`[1,2,3]`), &result)
  277. }
  278. }