jsoniter_string_test.go 5.4 KB

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