jsoniter_customize_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package jsoniter
  2. import (
  3. "reflect"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "unsafe"
  8. "github.com/json-iterator/go/require"
  9. )
  10. func Test_customize_type_decoder(t *testing.T) {
  11. RegisterTypeDecoder("time.Time", func(ptr unsafe.Pointer, iter *Iterator) {
  12. t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
  13. if err != nil {
  14. iter.Error = err
  15. return
  16. }
  17. *((*time.Time)(ptr)) = t
  18. })
  19. defer CleanDecoders()
  20. val := time.Time{}
  21. err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. year, month, day := val.Date()
  26. if year != 2016 || month != 12 || day != 5 {
  27. t.Fatal(val)
  28. }
  29. }
  30. func Test_customize_type_encoder(t *testing.T) {
  31. should := require.New(t)
  32. RegisterTypeEncoder("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
  33. t := *((*time.Time)(ptr))
  34. stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
  35. })
  36. defer CleanEncoders()
  37. val := time.Unix(0, 0)
  38. str, err := MarshalToString(val)
  39. should.Nil(err)
  40. should.Equal(`"1970-01-01 00:00:00"`, str)
  41. }
  42. func Test_customize_byte_array_encoder(t *testing.T) {
  43. should := require.New(t)
  44. RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
  45. t := *((*[]byte)(ptr))
  46. stream.WriteString(string(t))
  47. })
  48. defer CleanEncoders()
  49. val := []byte("abc")
  50. str, err := MarshalToString(val)
  51. should.Nil(err)
  52. should.Equal(`"abc"`, str)
  53. }
  54. func Test_customize_float_marshal(t *testing.T) {
  55. should := require.New(t)
  56. EnableLossyFloatMarshalling()
  57. defer CleanEncoders()
  58. str, err := MarshalToString(float32(1.23456789))
  59. should.Nil(err)
  60. should.Equal("1.234568", str)
  61. }
  62. type Tom struct {
  63. field1 string
  64. }
  65. func Test_customize_field_decoder(t *testing.T) {
  66. RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
  67. *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
  68. })
  69. defer CleanDecoders()
  70. tom := Tom{}
  71. err := Unmarshal([]byte(`{"field1": 100}`), &tom)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. }
  76. type TestObject1 struct {
  77. field1 string
  78. }
  79. func Test_customize_field_by_extension(t *testing.T) {
  80. should := require.New(t)
  81. RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
  82. if type_.String() == "jsoniter.TestObject1" && field.Name == "field1" {
  83. encode := func(ptr unsafe.Pointer, stream *Stream) {
  84. str := *((*string)(ptr))
  85. val, _ := strconv.Atoi(str)
  86. stream.WriteInt(val)
  87. }
  88. decode := func(ptr unsafe.Pointer, iter *Iterator) {
  89. *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
  90. }
  91. return []string{"field-1"}, encode, decode
  92. }
  93. return nil, nil, nil
  94. })
  95. obj := TestObject1{}
  96. err := UnmarshalFromString(`{"field-1": 100}`, &obj)
  97. should.Nil(err)
  98. should.Equal("100", obj.field1)
  99. str, err := MarshalToString(obj)
  100. should.Nil(err)
  101. should.Equal(`{"field-1":100}`, str)
  102. }
  103. func Test_unexported_fields(t *testing.T) {
  104. EnableUnexportedStructFieldsSupport()
  105. should := require.New(t)
  106. type TestObject struct {
  107. field1 string
  108. field2 string `json:"field-2"`
  109. }
  110. obj := TestObject{}
  111. obj.field1 = "hello"
  112. should.Nil(UnmarshalFromString(`{}`, &obj))
  113. should.Equal("hello", obj.field1)
  114. should.Nil(UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
  115. should.Equal("world", obj.field1)
  116. should.Equal("abc", obj.field2)
  117. str, err := MarshalToString(obj)
  118. should.Nil(err)
  119. should.Equal(`{"field1":"world","field-2":"abc"}`, str)
  120. }