decoder_test.go 4.0 KB

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