You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	test type encoder
This commit is contained in:
		| @@ -154,6 +154,11 @@ func CleanDecoders() { | |||||||
| 	fieldDecoders = map[string]Decoder{} | 	fieldDecoders = map[string]Decoder{} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func CleanEncoders() { | ||||||
|  | 	typeEncoders = map[string]Encoder{} | ||||||
|  | 	fieldEncoders = map[string]Encoder{} | ||||||
|  | } | ||||||
|  |  | ||||||
| type optionalDecoder struct { | type optionalDecoder struct { | ||||||
| 	valueType    reflect.Type | 	valueType    reflect.Type | ||||||
| 	valueDecoder Decoder | 	valueDecoder Decoder | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ import ( | |||||||
| 	"testing" | 	"testing" | ||||||
| 	"time" | 	"time" | ||||||
| 	"unsafe" | 	"unsafe" | ||||||
|  | 	"github.com/json-iterator/go/require" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func Test_customize_type_decoder(t *testing.T) { | 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 { | type Tom struct { | ||||||
| 	field1 string | 	field1 string | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user