jsoniter_array_test.go 4.1 KB

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