jsoniter_array_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/json-iterator/go/require"
  6. "bytes"
  7. )
  8. func Test_empty_array(t *testing.T) {
  9. should := require.New(t)
  10. iter := ParseString(`[]`)
  11. cont := iter.ReadArray()
  12. should.False(cont)
  13. iter = ParseString(`[]`)
  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(`[1]`)
  22. should.True(iter.ReadArray())
  23. should.Equal(1, iter.ReadInt())
  24. should.False(iter.ReadArray())
  25. iter = ParseString(`[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(`[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(`[1,2]`)
  40. should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
  41. }
  42. func Test_invalid_array(t *testing.T) {
  43. iter := ParseString(`[`)
  44. iter.ReadArray()
  45. if iter.Error == nil {
  46. t.FailNow()
  47. }
  48. }
  49. func Test_whitespace_in_head(t *testing.T) {
  50. iter := ParseString(` [1]`)
  51. cont := iter.ReadArray()
  52. if cont != true {
  53. t.FailNow()
  54. }
  55. if iter.ReadUint64() != 1 {
  56. t.FailNow()
  57. }
  58. }
  59. func Test_whitespace_after_array_start(t *testing.T) {
  60. iter := ParseString(`[ 1]`)
  61. cont := iter.ReadArray()
  62. if cont != true {
  63. t.FailNow()
  64. }
  65. if iter.ReadUint64() != 1 {
  66. t.FailNow()
  67. }
  68. }
  69. func Test_whitespace_before_array_end(t *testing.T) {
  70. iter := ParseString(`[1 ]`)
  71. cont := iter.ReadArray()
  72. if cont != true {
  73. t.FailNow()
  74. }
  75. if iter.ReadUint64() != 1 {
  76. t.FailNow()
  77. }
  78. cont = iter.ReadArray()
  79. if cont != false {
  80. t.FailNow()
  81. }
  82. }
  83. func Test_whitespace_before_comma(t *testing.T) {
  84. iter := ParseString(`[1 ,2]`)
  85. cont := iter.ReadArray()
  86. if cont != true {
  87. t.FailNow()
  88. }
  89. if iter.ReadUint64() != 1 {
  90. t.FailNow()
  91. }
  92. cont = iter.ReadArray()
  93. if cont != true {
  94. t.FailNow()
  95. }
  96. if iter.ReadUint64() != 2 {
  97. t.FailNow()
  98. }
  99. cont = iter.ReadArray()
  100. if cont != false {
  101. t.FailNow()
  102. }
  103. }
  104. func Test_write_array(t *testing.T) {
  105. should := require.New(t)
  106. buf := &bytes.Buffer{}
  107. stream := NewStream(buf, 4096)
  108. stream.IndentionStep = 2
  109. stream.WriteArrayStart()
  110. stream.WriteInt(1)
  111. stream.WriteMore()
  112. stream.WriteInt(2)
  113. stream.WriteArrayEnd()
  114. stream.Flush()
  115. should.Nil(stream.Error)
  116. should.Equal("[\n 1,\n 2\n]", buf.String())
  117. }
  118. func Test_write_val_array(t *testing.T) {
  119. should := require.New(t)
  120. val := []int{1, 2, 3}
  121. str, err := MarshalToString(val)
  122. should.Nil(err)
  123. should.Equal("[1,2,3]", str)
  124. }
  125. func Test_write_val_empty_array(t *testing.T) {
  126. should := require.New(t)
  127. val := []int{}
  128. str, err := MarshalToString(val)
  129. should.Nil(err)
  130. should.Equal("[]", str)
  131. }
  132. func Benchmark_jsoniter_array(b *testing.B) {
  133. b.ReportAllocs()
  134. input := []byte(`[1,2,3,4,5,6,7,8,9]`)
  135. iter := ParseBytes(input)
  136. b.ResetTimer()
  137. for n := 0; n < b.N; n++ {
  138. iter.ResetBytes(input)
  139. for iter.ReadArray() {
  140. iter.ReadUint64()
  141. }
  142. }
  143. }
  144. func Benchmark_json_array(b *testing.B) {
  145. for n := 0; n < b.N; n++ {
  146. result := []interface{}{}
  147. json.Unmarshal([]byte(`[1,2,3]`), &result)
  148. }
  149. }