jsoniter_string_test.go 3.9 KB

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