jsoniter_string_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Benchmark_jsoniter_unicode(b *testing.B) {
  129. for n := 0; n < b.N; n++ {
  130. iter := ParseString(ConfigOfDefault, `"\ud83d\udc4a"`)
  131. iter.ReadString()
  132. }
  133. }
  134. func Benchmark_jsoniter_ascii(b *testing.B) {
  135. iter := NewIterator(ConfigOfDefault)
  136. input := []byte(`"hello, world! hello, world!"`)
  137. b.ResetTimer()
  138. for n := 0; n < b.N; n++ {
  139. iter.ResetBytes(input)
  140. iter.ReadString()
  141. }
  142. }
  143. func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
  144. iter := ParseString(ConfigOfDefault, `"hello, world!"`)
  145. b.ResetTimer()
  146. for n := 0; n < b.N; n++ {
  147. iter.ResetBytes(iter.buf)
  148. iter.ReadStringAsSlice()
  149. }
  150. }
  151. func Benchmark_json_unicode(b *testing.B) {
  152. for n := 0; n < b.N; n++ {
  153. result := ""
  154. json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result)
  155. }
  156. }
  157. func Benchmark_json_ascii(b *testing.B) {
  158. for n := 0; n < b.N; n++ {
  159. result := ""
  160. json.Unmarshal([]byte(`"hello"`), &result)
  161. }
  162. }