jsoniter_array_test.go 4.6 KB

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