jsoniter_string_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/json-iterator/go/require"
  7. "testing"
  8. "unicode/utf8"
  9. )
  10. func Test_read_normal_string(t *testing.T) {
  11. cases := map[string]string{
  12. `"0123456789012345678901234567890123456789"`: `0123456789012345678901234567890123456789`,
  13. `""`: ``,
  14. `"hello"`: `hello`,
  15. }
  16. for input, output := range cases {
  17. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  18. should := require.New(t)
  19. iter := ParseString(ConfigOfDefault, input)
  20. should.Equal(output, iter.ReadString())
  21. })
  22. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  23. should := require.New(t)
  24. iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
  25. should.Equal(output, iter.ReadString())
  26. })
  27. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  28. should := require.New(t)
  29. iter := ParseString(ConfigOfDefault, input)
  30. should.Equal(output, string(iter.ReadStringAsSlice()))
  31. })
  32. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  33. should := require.New(t)
  34. iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
  35. should.Equal(output, string(iter.ReadStringAsSlice()))
  36. })
  37. }
  38. }
  39. func Test_read_exotic_string(t *testing.T) {
  40. cases := map[string]string{
  41. `"hel\"lo"`: `hel"lo`,
  42. `"hel\nlo"`: "hel\nlo",
  43. `"\u4e2d\u6587"`: "中文",
  44. `"\ud83d\udc4a"`: "\xf0\x9f\x91\x8a", // surrogate
  45. }
  46. for input, output := range cases {
  47. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  48. should := require.New(t)
  49. iter := ParseString(ConfigOfDefault, input)
  50. should.Equal(output, iter.ReadString())
  51. })
  52. t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
  53. should := require.New(t)
  54. iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
  55. should.Equal(output, iter.ReadString())
  56. })
  57. }
  58. }
  59. func Test_read_string_as_interface(t *testing.T) {
  60. should := require.New(t)
  61. iter := ParseString(ConfigOfDefault, `"hello"`)
  62. should.Equal("hello", iter.Read())
  63. }
  64. func Test_read_string_as_any(t *testing.T) {
  65. should := require.New(t)
  66. any, err := UnmarshalAnyFromString(`"hello"`)
  67. should.Nil(err)
  68. should.Equal("hello", any.ToString())
  69. should.True(any.ToBool())
  70. any, err = UnmarshalAnyFromString(`" "`)
  71. should.False(any.ToBool())
  72. any, err = UnmarshalAnyFromString(`"false"`)
  73. should.False(any.ToBool())
  74. any, err = UnmarshalAnyFromString(`"123"`)
  75. should.Equal(123, any.ToInt())
  76. }
  77. func Test_wrap_string(t *testing.T) {
  78. should := require.New(t)
  79. any := WrapString("123")
  80. should.Equal(123, any.ToInt())
  81. }
  82. func Test_write_string(t *testing.T) {
  83. should := require.New(t)
  84. str, err := MarshalToString("hello")
  85. should.Equal(`"hello"`, str)
  86. should.Nil(err)
  87. str, err = MarshalToString(`hel"lo`)
  88. should.Equal(`"hel\"lo"`, str)
  89. should.Nil(err)
  90. }
  91. func Test_write_val_string(t *testing.T) {
  92. should := require.New(t)
  93. buf := &bytes.Buffer{}
  94. stream := NewStream(ConfigOfDefault, buf, 4096)
  95. stream.WriteVal("hello")
  96. stream.Flush()
  97. should.Nil(stream.Error)
  98. should.Equal(`"hello"`, buf.String())
  99. }
  100. func Test_decode_slash(t *testing.T) {
  101. should := require.New(t)
  102. var obj interface{}
  103. should.NotNil(json.Unmarshal([]byte("\\"), &obj))
  104. should.NotNil(UnmarshalFromString("\\", &obj))
  105. }
  106. func Test_html_escape(t *testing.T) {
  107. should := require.New(t)
  108. output, err := json.Marshal(`>`)
  109. should.Nil(err)
  110. should.Equal(`"\u003e"`, string(output))
  111. output, err = ConfigCompatibleWithStandardLibrary.Marshal(`>`)
  112. should.Nil(err)
  113. should.Equal(`"\u003e"`, string(output))
  114. }
  115. func Test_string_encode_with_std(t *testing.T) {
  116. should := require.New(t)
  117. for i := 0; i < utf8.RuneSelf; i++ {
  118. input := string([]byte{byte(i)})
  119. stdOutputBytes, err := json.Marshal(input)
  120. should.Nil(err)
  121. stdOutput := string(stdOutputBytes)
  122. jsoniterOutputBytes, err := ConfigCompatibleWithStandardLibrary.Marshal(input)
  123. should.Nil(err)
  124. jsoniterOutput := string(jsoniterOutputBytes)
  125. should.Equal(stdOutput, jsoniterOutput)
  126. }
  127. }
  128. func Test_string_encode_with_std_without_html_escape(t *testing.T) {
  129. should := require.New(t)
  130. for i := 0; i < utf8.RuneSelf; i++ {
  131. input := string([]byte{byte(i)})
  132. buf := &bytes.Buffer{}
  133. encoder := json.NewEncoder(buf)
  134. encoder.SetEscapeHTML(false)
  135. err := encoder.Encode(input)
  136. should.Nil(err)
  137. stdOutput := buf.String()
  138. stdOutput = stdOutput[:len(stdOutput) - 1]
  139. jsoniterOutputBytes, err := Marshal(input)
  140. should.Nil(err)
  141. jsoniterOutput := string(jsoniterOutputBytes)
  142. should.Equal(stdOutput, jsoniterOutput)
  143. }
  144. }
  145. func Benchmark_jsoniter_unicode(b *testing.B) {
  146. for n := 0; n < b.N; n++ {
  147. iter := ParseString(ConfigOfDefault, `"\ud83d\udc4a"`)
  148. iter.ReadString()
  149. }
  150. }
  151. func Benchmark_jsoniter_ascii(b *testing.B) {
  152. iter := NewIterator(ConfigOfDefault)
  153. input := []byte(`"hello, world! hello, world!"`)
  154. b.ResetTimer()
  155. for n := 0; n < b.N; n++ {
  156. iter.ResetBytes(input)
  157. iter.ReadString()
  158. }
  159. }
  160. func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
  161. iter := ParseString(ConfigOfDefault, `"hello, world!"`)
  162. b.ResetTimer()
  163. for n := 0; n < b.N; n++ {
  164. iter.ResetBytes(iter.buf)
  165. iter.ReadStringAsSlice()
  166. }
  167. }
  168. func Benchmark_json_unicode(b *testing.B) {
  169. for n := 0; n < b.N; n++ {
  170. result := ""
  171. json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result)
  172. }
  173. }
  174. func Benchmark_json_ascii(b *testing.B) {
  175. for n := 0; n < b.N; n++ {
  176. result := ""
  177. json.Unmarshal([]byte(`"hello"`), &result)
  178. }
  179. }