jsoniter_array_test.go 4.3 KB

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