Tao Wen 8 лет назад
Родитель
Сommit
6bd835aeb1
2 измененных файлов с 19 добавлено и 0 удалено
  1. 5 0
      feature_reflect.go
  2. 14 0
      jsoniter_customize_test.go

+ 5 - 0
feature_reflect.go

@@ -154,6 +154,11 @@ func CleanDecoders() {
 	fieldDecoders = map[string]Decoder{}
 }
 
+func CleanEncoders() {
+	typeEncoders = map[string]Encoder{}
+	fieldEncoders = map[string]Encoder{}
+}
+
 type optionalDecoder struct {
 	valueType    reflect.Type
 	valueDecoder Decoder

+ 14 - 0
jsoniter_customize_test.go

@@ -6,6 +6,7 @@ import (
 	"testing"
 	"time"
 	"unsafe"
+	"github.com/json-iterator/go/require"
 )
 
 func Test_customize_type_decoder(t *testing.T) {
@@ -29,6 +30,19 @@ func Test_customize_type_decoder(t *testing.T) {
 	}
 }
 
+func Test_customize_type_encoder(t *testing.T) {
+	should := require.New(t)
+	RegisterTypeEncoder("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
+		t := *((*time.Time)(ptr))
+		stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
+	})
+	defer CleanEncoders()
+	val := time.Unix(0, 0)
+	str, err := MarshalToString(val)
+	should.Nil(err)
+	should.Equal(`"1970-01-01 00:00:00"`, str)
+}
+
 type Tom struct {
 	field1 string
 }