decoder_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package test
  2. import (
  3. "testing"
  4. "unsafe"
  5. "time"
  6. "github.com/json-iterator/go"
  7. "github.com/stretchr/testify/require"
  8. "strconv"
  9. )
  10. func Test_customize_type_decoder(t *testing.T) {
  11. t.Skip()
  12. jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  13. t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
  14. if err != nil {
  15. iter.Error = err
  16. return
  17. }
  18. *((*time.Time)(ptr)) = t
  19. })
  20. //defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
  21. val := time.Time{}
  22. err := jsoniter.Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. year, month, day := val.Date()
  27. if year != 2016 || month != 12 || day != 5 {
  28. t.Fatal(val)
  29. }
  30. }
  31. func Test_customize_byte_array_encoder(t *testing.T) {
  32. t.Skip()
  33. //jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
  34. should := require.New(t)
  35. jsoniter.RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
  36. t := *((*[]byte)(ptr))
  37. stream.WriteString(string(t))
  38. }, nil)
  39. //defer jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
  40. val := []byte("abc")
  41. str, err := jsoniter.MarshalToString(val)
  42. should.Nil(err)
  43. should.Equal(`"abc"`, str)
  44. }
  45. func Test_customize_field_decoder(t *testing.T) {
  46. type Tom struct {
  47. field1 string
  48. }
  49. jsoniter.RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  50. *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
  51. })
  52. //defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
  53. tom := Tom{}
  54. err := jsoniter.Unmarshal([]byte(`{"field1": 100}`), &tom)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. }
  59. func Test_recursive_empty_interface_customization(t *testing.T) {
  60. t.Skip()
  61. var obj interface{}
  62. jsoniter.RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  63. switch iter.WhatIsNext() {
  64. case jsoniter.NumberValue:
  65. *(*interface{})(ptr) = iter.ReadInt64()
  66. default:
  67. *(*interface{})(ptr) = iter.Read()
  68. }
  69. })
  70. should := require.New(t)
  71. jsoniter.Unmarshal([]byte("[100]"), &obj)
  72. should.Equal([]interface{}{int64(100)}, obj)
  73. }
  74. type MyInterface interface {
  75. Hello() string
  76. }
  77. type MyString string
  78. func (ms MyString) Hello() string {
  79. return string(ms)
  80. }
  81. func Test_read_custom_interface(t *testing.T) {
  82. t.Skip()
  83. should := require.New(t)
  84. var val MyInterface
  85. jsoniter.RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
  86. *((*MyInterface)(ptr)) = MyString(iter.ReadString())
  87. })
  88. err := jsoniter.UnmarshalFromString(`"hello"`, &val)
  89. should.Nil(err)
  90. should.Equal("hello", val.Hello())
  91. }