decoder_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/json-iterator/go"
  6. "github.com/stretchr/testify/require"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "unsafe"
  11. )
  12. func Test_customize_type_decoder(t *testing.T) {
  13. t.Skip()
  14. jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  15. t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
  16. if err != nil {
  17. iter.Error = err
  18. return
  19. }
  20. *((*time.Time)(ptr)) = t
  21. })
  22. //defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
  23. val := time.Time{}
  24. err := jsoniter.Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. year, month, day := val.Date()
  29. if year != 2016 || month != 12 || day != 5 {
  30. t.Fatal(val)
  31. }
  32. }
  33. func Test_customize_byte_array_encoder(t *testing.T) {
  34. t.Skip()
  35. //jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
  36. should := require.New(t)
  37. jsoniter.RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
  38. t := *((*[]byte)(ptr))
  39. stream.WriteString(string(t))
  40. }, nil)
  41. //defer jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
  42. val := []byte("abc")
  43. str, err := jsoniter.MarshalToString(val)
  44. should.Nil(err)
  45. should.Equal(`"abc"`, str)
  46. }
  47. type CustomEncoderAttachmentTestStruct struct {
  48. Value int32 `json:"value"`
  49. }
  50. type CustomEncoderAttachmentTestStructEncoder struct {}
  51. func (c *CustomEncoderAttachmentTestStructEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
  52. attachVal, ok := stream.Attachment.(int)
  53. stream.WriteRaw(`"`)
  54. stream.WriteRaw(fmt.Sprintf("%t %d", ok, attachVal))
  55. stream.WriteRaw(`"`)
  56. }
  57. func (c *CustomEncoderAttachmentTestStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  58. return false
  59. }
  60. func Test_custom_encoder_attachment(t *testing.T) {
  61. jsoniter.RegisterTypeEncoder("test.CustomEncoderAttachmentTestStruct", &CustomEncoderAttachmentTestStructEncoder{})
  62. expectedValue := 17
  63. should := require.New(t)
  64. buf := &bytes.Buffer{}
  65. stream := jsoniter.NewStream(jsoniter.Config{SortMapKeys: true}.Froze(), buf, 4096)
  66. stream.Attachment = expectedValue
  67. val := map[string]CustomEncoderAttachmentTestStruct{"a": {}}
  68. stream.WriteVal(val)
  69. stream.Flush()
  70. should.Nil(stream.Error)
  71. should.Equal("{\"a\":\"true 17\"}", buf.String())
  72. }
  73. func Test_customize_field_decoder(t *testing.T) {
  74. type Tom struct {
  75. field1 string
  76. }
  77. jsoniter.RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  78. *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
  79. })
  80. //defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
  81. tom := Tom{}
  82. err := jsoniter.Unmarshal([]byte(`{"field1": 100}`), &tom)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. }
  87. func Test_recursive_empty_interface_customization(t *testing.T) {
  88. t.Skip()
  89. var obj interface{}
  90. jsoniter.RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  91. switch iter.WhatIsNext() {
  92. case jsoniter.NumberValue:
  93. *(*interface{})(ptr) = iter.ReadInt64()
  94. default:
  95. *(*interface{})(ptr) = iter.Read()
  96. }
  97. })
  98. should := require.New(t)
  99. jsoniter.Unmarshal([]byte("[100]"), &obj)
  100. should.Equal([]interface{}{int64(100)}, obj)
  101. }
  102. type MyInterface interface {
  103. Hello() string
  104. }
  105. type MyString string
  106. func (ms MyString) Hello() string {
  107. return string(ms)
  108. }
  109. func Test_read_custom_interface(t *testing.T) {
  110. t.Skip()
  111. should := require.New(t)
  112. var val MyInterface
  113. jsoniter.RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  114. *((*MyInterface)(ptr)) = MyString(iter.ReadString())
  115. })
  116. err := jsoniter.UnmarshalFromString(`"hello"`, &val)
  117. should.Nil(err)
  118. should.Equal("hello", val.Hello())
  119. }
  120. const flow1 = `
  121. {"A":"hello"}
  122. {"A":"hello"}
  123. {"A":"hello"}
  124. {"A":"hello"}
  125. {"A":"hello"}`
  126. const flow2 = `
  127. {"A":"hello"}
  128. {"A":"hello"}
  129. {"A":"hello"}
  130. {"A":"hello"}
  131. {"A":"hello"}
  132. `
  133. type (
  134. Type1 struct {
  135. A string
  136. }
  137. Type2 struct {
  138. A string
  139. }
  140. )
  141. func (t *Type2) UnmarshalJSON(data []byte) error {
  142. return nil
  143. }
  144. func (t *Type2) MarshalJSON() ([]byte, error) {
  145. return nil, nil
  146. }
  147. func TestType1NoFinalLF(t *testing.T) {
  148. reader := bytes.NewReader([]byte(flow1))
  149. dec := jsoniter.NewDecoder(reader)
  150. i := 0
  151. for dec.More() {
  152. data := &Type1{}
  153. if err := dec.Decode(data); err != nil {
  154. t.Errorf("at %v got %v", i, err)
  155. }
  156. i++
  157. }
  158. }
  159. func TestType1FinalLF(t *testing.T) {
  160. reader := bytes.NewReader([]byte(flow2))
  161. dec := jsoniter.NewDecoder(reader)
  162. i := 0
  163. for dec.More() {
  164. data := &Type1{}
  165. if err := dec.Decode(data); err != nil {
  166. t.Errorf("at %v got %v", i, err)
  167. }
  168. i++
  169. }
  170. }
  171. func TestType2NoFinalLF(t *testing.T) {
  172. reader := bytes.NewReader([]byte(flow1))
  173. dec := jsoniter.NewDecoder(reader)
  174. i := 0
  175. for dec.More() {
  176. data := &Type2{}
  177. if err := dec.Decode(data); err != nil {
  178. t.Errorf("at %v got %v", i, err)
  179. }
  180. i++
  181. }
  182. }
  183. func TestType2FinalLF(t *testing.T) {
  184. reader := bytes.NewReader([]byte(flow2))
  185. dec := jsoniter.NewDecoder(reader)
  186. i := 0
  187. for dec.More() {
  188. data := &Type2{}
  189. if err := dec.Decode(data); err != nil {
  190. t.Errorf("at %v got %v", i, err)
  191. }
  192. i++
  193. }
  194. }