代码拉取完成,页面将自动刷新
package FlyJSON
import (
"time"
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/modern-go/reflect2"
)
type timeExtension struct {
jsoniter.DummyExtension
}
func (e *timeExtension) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
if typ.Type1().Name() == "Time" {
return &timeAsInt64Codec{time.Microsecond}
}
return nil
}
func (e *timeExtension) CreateEncoder(typ reflect2.Type) jsoniter.ValEncoder {
if typ.Type1().Name() == "Time" {
return &timeAsInt64Codec{time.Microsecond}
}
return nil
}
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))
return ts.UnixNano() == 0
}
func (codec *timeAsInt64Codec) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
ts := *((*time.Time)(ptr))
stream.WriteInt64(ts.UnixNano() / codec.precision.Nanoseconds())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。