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:
parent
90888390bc
commit
6bd835aeb1
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user