You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-12-02 22:48:20 +02:00
#67 time as int64 with decoder
This commit is contained in:
@@ -9,11 +9,16 @@ import (
|
||||
// keep epoch milliseconds
|
||||
func RegisterTimeAsInt64Codec(precision time.Duration) {
|
||||
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{precision})
|
||||
jsoniter.RegisterTypeDecoder("time.Time", &timeAsInt64Codec{precision})
|
||||
}
|
||||
|
||||
type timeAsInt64Codec struct {
|
||||
precision time.Duration
|
||||
}
|
||||
func (codec *timeAsInt64Codec) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
nanoseconds := iter.ReadInt64() * codec.precision.Nanoseconds()
|
||||
*((*time.Time)(ptr)) = time.Unix(0, nanoseconds)
|
||||
}
|
||||
|
||||
func (codec *timeAsInt64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
ts := *((*time.Time)(ptr))
|
||||
|
||||
Reference in New Issue
Block a user