jsoniter_array_test.go 3.8 KB

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