1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00

test type encoder

This commit is contained in:
Tao Wen 2017-05-05 08:22:19 +08:00
parent 90888390bc
commit 6bd835aeb1
2 changed files with 19 additions and 0 deletions

View File

@ -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

View File

@ -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
}