jsoniter_array_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package misc_tests
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/json-iterator/go"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func Test_empty_array(t *testing.T) {
  10. should := require.New(t)
  11. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[]`)
  12. cont := iter.ReadArray()
  13. should.False(cont)
  14. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `[]`)
  15. iter.ReadArrayCB(func(iter *jsoniter.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 := jsoniter.ParseString(jsoniter.ConfigDefault, `[1]`)
  23. should.True(iter.ReadArray())
  24. should.Equal(1, iter.ReadInt())
  25. should.False(iter.ReadArray())
  26. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `[1]`)
  27. iter.ReadArrayCB(func(iter *jsoniter.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 := jsoniter.ParseString(jsoniter.ConfigDefault, `[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 = jsoniter.ParseString(jsoniter.ConfigDefault, `[1,2]`)
  41. should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
  42. }
  43. func Test_whitespace_in_head(t *testing.T) {
  44. iter := jsoniter.ParseString(jsoniter.ConfigDefault, ` [1]`)
  45. cont := iter.ReadArray()
  46. if cont != true {
  47. t.FailNow()
  48. }
  49. if iter.ReadUint64() != 1 {
  50. t.FailNow()
  51. }
  52. }
  53. func Test_whitespace_after_array_start(t *testing.T) {
  54. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[ 1]`)
  55. cont := iter.ReadArray()
  56. if cont != true {
  57. t.FailNow()
  58. }
  59. if iter.ReadUint64() != 1 {
  60. t.FailNow()
  61. }
  62. }
  63. func Test_whitespace_before_array_end(t *testing.T) {
  64. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[1 ]`)
  65. cont := iter.ReadArray()
  66. if cont != true {
  67. t.FailNow()
  68. }
  69. if iter.ReadUint64() != 1 {
  70. t.FailNow()
  71. }
  72. cont = iter.ReadArray()
  73. if cont != false {
  74. t.FailNow()
  75. }
  76. }
  77. func Test_whitespace_before_comma(t *testing.T) {
  78. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[1 ,2]`)
  79. cont := iter.ReadArray()
  80. if cont != true {
  81. t.FailNow()
  82. }
  83. if iter.ReadUint64() != 1 {
  84. t.FailNow()
  85. }
  86. cont = iter.ReadArray()
  87. if cont != true {
  88. t.FailNow()
  89. }
  90. if iter.ReadUint64() != 2 {
  91. t.FailNow()
  92. }
  93. cont = iter.ReadArray()
  94. if cont != false {
  95. t.FailNow()
  96. }
  97. }
  98. func Test_write_array(t *testing.T) {
  99. should := require.New(t)
  100. buf := &bytes.Buffer{}
  101. stream := jsoniter.NewStream(jsoniter.Config{IndentionStep: 2}.Froze(), buf, 4096)
  102. stream.WriteArrayStart()
  103. stream.WriteInt(1)
  104. stream.WriteMore()
  105. stream.WriteInt(2)
  106. stream.WriteArrayEnd()
  107. stream.Flush()
  108. should.Nil(stream.Error)
  109. should.Equal("[\n 1,\n 2\n]", buf.String())
  110. }
  111. func Test_write_val_array(t *testing.T) {
  112. should := require.New(t)
  113. val := []int{1, 2, 3}
  114. str, err := jsoniter.MarshalToString(&val)
  115. should.Nil(err)
  116. should.Equal("[1,2,3]", str)
  117. }
  118. func Test_write_val_empty_array(t *testing.T) {
  119. should := require.New(t)
  120. val := []int{}
  121. str, err := jsoniter.MarshalToString(val)
  122. should.Nil(err)
  123. should.Equal("[]", str)
  124. }
  125. func Test_write_array_of_interface_in_struct(t *testing.T) {
  126. should := require.New(t)
  127. type TestObject struct {
  128. Field []interface{}
  129. Field2 string
  130. }
  131. val := TestObject{[]interface{}{1, 2}, ""}
  132. str, err := jsoniter.MarshalToString(val)
  133. should.Nil(err)
  134. should.Contains(str, `"Field":[1,2]`)
  135. should.Contains(str, `"Field2":""`)
  136. }
  137. func Test_encode_byte_array(t *testing.T) {
  138. should := require.New(t)
  139. bytes, err := json.Marshal([]byte{1, 2, 3})
  140. should.Nil(err)
  141. should.Equal(`"AQID"`, string(bytes))
  142. bytes, err = jsoniter.Marshal([]byte{1, 2, 3})
  143. should.Nil(err)
  144. should.Equal(`"AQID"`, string(bytes))
  145. }
  146. func Test_encode_empty_byte_array(t *testing.T) {
  147. should := require.New(t)
  148. bytes, err := json.Marshal([]byte{})
  149. should.Nil(err)
  150. should.Equal(`""`, string(bytes))
  151. bytes, err = jsoniter.Marshal([]byte{})
  152. should.Nil(err)
  153. should.Equal(`""`, string(bytes))
  154. }
  155. func Test_encode_nil_byte_array(t *testing.T) {
  156. should := require.New(t)
  157. var nilSlice []byte
  158. bytes, err := json.Marshal(nilSlice)
  159. should.Nil(err)
  160. should.Equal(`null`, string(bytes))
  161. bytes, err = jsoniter.Marshal(nilSlice)
  162. should.Nil(err)
  163. should.Equal(`null`, string(bytes))
  164. }
  165. func Test_decode_byte_array_from_base64(t *testing.T) {
  166. should := require.New(t)
  167. data := []byte{}
  168. err := json.Unmarshal([]byte(`"AQID"`), &data)
  169. should.Nil(err)
  170. should.Equal([]byte{1, 2, 3}, data)
  171. err = jsoniter.Unmarshal([]byte(`"AQID"`), &data)
  172. should.Nil(err)
  173. should.Equal([]byte{1, 2, 3}, data)
  174. }
  175. func Test_decode_byte_array_from_base64_with_newlines(t *testing.T) {
  176. should := require.New(t)
  177. data := []byte{}
  178. err := json.Unmarshal([]byte(`"A\rQ\nID"`), &data)
  179. should.Nil(err)
  180. should.Equal([]byte{1, 2, 3}, data)
  181. err = jsoniter.Unmarshal([]byte(`"A\rQ\nID"`), &data)
  182. should.Nil(err)
  183. should.Equal([]byte{1, 2, 3}, data)
  184. }
  185. func Test_decode_byte_array_from_array(t *testing.T) {
  186. should := require.New(t)
  187. data := []byte{}
  188. err := json.Unmarshal([]byte(`[1,2,3]`), &data)
  189. should.Nil(err)
  190. should.Equal([]byte{1, 2, 3}, data)
  191. err = jsoniter.Unmarshal([]byte(`[1,2,3]`), &data)
  192. should.Nil(err)
  193. should.Equal([]byte{1, 2, 3}, data)
  194. }
  195. func Test_decode_slice(t *testing.T) {
  196. should := require.New(t)
  197. slice := make([]string, 0, 5)
  198. jsoniter.UnmarshalFromString(`["hello", "world"]`, &slice)
  199. should.Equal([]string{"hello", "world"}, slice)
  200. }
  201. func Test_decode_large_slice(t *testing.T) {
  202. should := require.New(t)
  203. slice := make([]int, 0, 1)
  204. jsoniter.UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice)
  205. should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice)
  206. }
  207. func Benchmark_jsoniter_array(b *testing.B) {
  208. b.ReportAllocs()
  209. input := []byte(`[1,2,3,4,5,6,7,8,9]`)
  210. iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, input)
  211. b.ResetTimer()
  212. for n := 0; n < b.N; n++ {
  213. iter.ResetBytes(input)
  214. for iter.ReadArray() {
  215. iter.ReadUint64()
  216. }
  217. }
  218. }
  219. func Benchmark_json_array(b *testing.B) {
  220. for n := 0; n < b.N; n++ {
  221. result := []interface{}{}
  222. json.Unmarshal([]byte(`[1,2,3]`), &result)
  223. }
  224. }