jsoniter_array_test.go 4.6 KB

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