jsoniter_array_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/json-iterator/go/require"
  6. "testing"
  7. )
  8. func Test_empty_array(t *testing.T) {
  9. should := require.New(t)
  10. iter := ParseString(ConfigDefault, `[]`)
  11. cont := iter.ReadArray()
  12. should.False(cont)
  13. iter = ParseString(ConfigDefault, `[]`)
  14. iter.ReadArrayCB(func(iter *Iterator) bool {
  15. should.FailNow("should not call")
  16. return true
  17. })
  18. }
  19. func Test_one_element(t *testing.T) {
  20. should := require.New(t)
  21. iter := ParseString(ConfigDefault, `[1]`)
  22. should.True(iter.ReadArray())
  23. should.Equal(1, iter.ReadInt())
  24. should.False(iter.ReadArray())
  25. iter = ParseString(ConfigDefault, `[1]`)
  26. iter.ReadArrayCB(func(iter *Iterator) bool {
  27. should.Equal(1, iter.ReadInt())
  28. return true
  29. })
  30. }
  31. func Test_two_elements(t *testing.T) {
  32. should := require.New(t)
  33. iter := ParseString(ConfigDefault, `[1,2]`)
  34. should.True(iter.ReadArray())
  35. should.Equal(int64(1), iter.ReadInt64())
  36. should.True(iter.ReadArray())
  37. should.Equal(int64(2), iter.ReadInt64())
  38. should.False(iter.ReadArray())
  39. iter = ParseString(ConfigDefault, `[1,2]`)
  40. should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
  41. }
  42. func Test_whitespace_in_head(t *testing.T) {
  43. iter := ParseString(ConfigDefault, ` [1]`)
  44. cont := iter.ReadArray()
  45. if cont != true {
  46. t.FailNow()
  47. }
  48. if iter.ReadUint64() != 1 {
  49. t.FailNow()
  50. }
  51. }
  52. func Test_whitespace_after_array_start(t *testing.T) {
  53. iter := ParseString(ConfigDefault, `[ 1]`)
  54. cont := iter.ReadArray()
  55. if cont != true {
  56. t.FailNow()
  57. }
  58. if iter.ReadUint64() != 1 {
  59. t.FailNow()
  60. }
  61. }
  62. func Test_whitespace_before_array_end(t *testing.T) {
  63. iter := ParseString(ConfigDefault, `[1 ]`)
  64. cont := iter.ReadArray()
  65. if cont != true {
  66. t.FailNow()
  67. }
  68. if iter.ReadUint64() != 1 {
  69. t.FailNow()
  70. }
  71. cont = iter.ReadArray()
  72. if cont != false {
  73. t.FailNow()
  74. }
  75. }
  76. func Test_whitespace_before_comma(t *testing.T) {
  77. iter := ParseString(ConfigDefault, `[1 ,2]`)
  78. cont := iter.ReadArray()
  79. if cont != true {
  80. t.FailNow()
  81. }
  82. if iter.ReadUint64() != 1 {
  83. t.FailNow()
  84. }
  85. cont = iter.ReadArray()
  86. if cont != true {
  87. t.FailNow()
  88. }
  89. if iter.ReadUint64() != 2 {
  90. t.FailNow()
  91. }
  92. cont = iter.ReadArray()
  93. if cont != false {
  94. t.FailNow()
  95. }
  96. }
  97. func Test_write_array(t *testing.T) {
  98. should := require.New(t)
  99. buf := &bytes.Buffer{}
  100. stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
  101. stream.WriteArrayStart()
  102. stream.WriteInt(1)
  103. stream.WriteMore()
  104. stream.WriteInt(2)
  105. stream.WriteArrayEnd()
  106. stream.Flush()
  107. should.Nil(stream.Error)
  108. should.Equal("[\n 1,\n 2\n]", buf.String())
  109. }
  110. func Test_write_val_array(t *testing.T) {
  111. should := require.New(t)
  112. val := []int{1, 2, 3}
  113. str, err := MarshalToString(&val)
  114. should.Nil(err)
  115. should.Equal("[1,2,3]", str)
  116. }
  117. func Test_write_val_empty_array(t *testing.T) {
  118. should := require.New(t)
  119. val := []int{}
  120. str, err := MarshalToString(val)
  121. should.Nil(err)
  122. should.Equal("[]", str)
  123. }
  124. func Test_write_array_of_interface_in_struct(t *testing.T) {
  125. should := require.New(t)
  126. type TestObject struct {
  127. Field []interface{}
  128. Field2 string
  129. }
  130. val := TestObject{[]interface{}{1, 2}, ""}
  131. str, err := MarshalToString(val)
  132. should.Nil(err)
  133. should.Contains(str, `"Field":[1,2]`)
  134. should.Contains(str, `"Field2":""`)
  135. }
  136. func Test_json_RawMessage(t *testing.T) {
  137. should := require.New(t)
  138. var data json.RawMessage
  139. should.Nil(Unmarshal([]byte(`[1,2,3]`), &data))
  140. should.Equal(`[1,2,3]`, string(data))
  141. str, err := MarshalToString(data)
  142. should.Nil(err)
  143. should.Equal(`[1,2,3]`, str)
  144. }
  145. func Test_encode_byte_array(t *testing.T) {
  146. should := require.New(t)
  147. bytes, err := json.Marshal([]byte{1, 2, 3})
  148. should.Nil(err)
  149. should.Equal(`"AQID"`, string(bytes))
  150. bytes, err = Marshal([]byte{1, 2, 3})
  151. should.Nil(err)
  152. should.Equal(`"AQID"`, string(bytes))
  153. }
  154. func Test_decode_byte_array(t *testing.T) {
  155. should := require.New(t)
  156. data := []byte{}
  157. err := json.Unmarshal([]byte(`"AQID"`), &data)
  158. should.Nil(err)
  159. should.Equal([]byte{1, 2, 3}, data)
  160. err = Unmarshal([]byte(`"AQID"`), &data)
  161. should.Nil(err)
  162. should.Equal([]byte{1, 2, 3}, data)
  163. }
  164. func Test_decode_slice(t *testing.T) {
  165. should := require.New(t)
  166. slice := make([]string, 0, 5)
  167. UnmarshalFromString(`["hello", "world"]`, &slice)
  168. should.Equal([]string{"hello", "world"}, slice)
  169. }
  170. func Test_decode_large_slice(t *testing.T) {
  171. should := require.New(t)
  172. slice := make([]int, 0, 1)
  173. UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
  174. should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
  175. }
  176. func Benchmark_jsoniter_array(b *testing.B) {
  177. b.ReportAllocs()
  178. input := []byte(`[1,2,3,4,5,6,7,8,9]`)
  179. iter := ParseBytes(ConfigDefault, input)
  180. b.ResetTimer()
  181. for n := 0; n < b.N; n++ {
  182. iter.ResetBytes(input)
  183. for iter.ReadArray() {
  184. iter.ReadUint64()
  185. }
  186. }
  187. }
  188. func Benchmark_json_array(b *testing.B) {
  189. for n := 0; n < b.N; n++ {
  190. result := []interface{}{}
  191. json.Unmarshal([]byte(`[1,2,3]`), &result)
  192. }
  193. }