You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-21 23:07:33 +02:00
#67 time as int64
This commit is contained in:
27
extra/time_as_int64_codec.go
Normal file
27
extra/time_as_int64_codec.go
Normal file
@ -0,0 +1,27 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"unsafe"
|
||||
"time"
|
||||
)
|
||||
|
||||
// keep epoch milliseconds
|
||||
func RegisterTimeAsInt64Codec() {
|
||||
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{})
|
||||
}
|
||||
|
||||
type timeAsInt64Codec struct {
|
||||
}
|
||||
|
||||
func (codec *timeAsInt64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
ts := *((*time.Time)(ptr))
|
||||
return ts.UnixNano() == 0
|
||||
}
|
||||
func (codec *timeAsInt64Codec) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
ts := *((*time.Time)(ptr))
|
||||
stream.WriteInt64(ts.UnixNano())
|
||||
}
|
||||
func (codec *timeAsInt64Codec) EncodeInterface(val interface{}, stream *jsoniter.Stream) {
|
||||
jsoniter.WriteToStream(val, stream, codec)
|
||||
}
|
Reference in New Issue
Block a user