Documentation ¶
Index ¶
- Constants
- func MustRegister(name string, codec TimeCodec)
- func NewTimeDecoder(dec TimeDecoder, typ reflect.Type) jsoniter.ValDecoder
- func NewTimeEncoder(enc TimeEncoder, typ reflect.Type) jsoniter.ValEncoder
- func OverrideEncoders(enc TimeEncoder) jsoniter.Extension
- func Register(name string, codec TimeCodec) error
- func Split(codec TimeCodec) (TimeDecoder, TimeEncoder)
- func UnixMicroseconds(n int64) time.Time
- func UnixMilliseconds(n int64) time.Time
- func UnixNanoseconds(n int64) time.Time
- func UnixSeconds(sec float64) time.Time
- type DecoderDecorator
- type EncoderDecorator
- type Extension
- type Registry
- type Time
- type TimeCodec
- func In(loc *time.Location, codec TimeCodec) TimeCodec
- func Join(decode TimeDecoder, encode TimeEncoder) TimeCodec
- func LayoutCodec(layout string) TimeCodec
- func Lookup(name string) TimeCodec
- func StdCodec() TimeCodec
- func StrftimeCodec(format string) TimeCodec
- func UnixMicrosecondsCodec() TimeCodec
- func UnixMillisecondsCodec() TimeCodec
- func UnixNanosecondsCodec() TimeCodec
- func UnixSecondsCodec() TimeCodec
- type TimeDecoder
- type TimeDecoderFunc
- type TimeEncoder
- type TimeEncoderFunc
Constants ¶
const DefaultTagName = "tcodec"
DefaultTagName is the struct tag name used for defining time decoders for a time.Time field.
Variables ¶
This section is empty.
Functions ¶
func MustRegister ¶
func NewTimeDecoder ¶
func NewTimeDecoder(dec TimeDecoder, typ reflect.Type) jsoniter.ValDecoder
func NewTimeEncoder ¶
func NewTimeEncoder(enc TimeEncoder, typ reflect.Type) jsoniter.ValEncoder
func OverrideEncoders ¶
func OverrideEncoders(enc TimeEncoder) jsoniter.Extension
OverrideEncoders returns an extension that forces all time.Time values to be encoded using `enc`
func Split ¶
func Split(codec TimeCodec) (TimeDecoder, TimeEncoder)
Split is a helper to split a TimeCodec into a decoder and an encoder.
func UnixMicroseconds ¶ added in v1.10.0
UnixMicroseconds reads a timestamp from microseconds since UNIX epoch. It decodes both string and number JSON values and encodes always to number.
func UnixMilliseconds ¶
UnixMilliseconds reads a timestamp from milliseconds since UNIX epoch.
func UnixNanoseconds ¶ added in v1.10.0
UnixNanoseconds reads a timestamp from nanoseconds since UNIX epoch. It decodes both string and number JSON values and encodes always to number.
func UnixSeconds ¶
UnixSeconds reads a timestamp from seconds since UNIX epoch. Fractions of a second can be set using the fractional part of a float. Precision is kept up to microseconds to avoid float64 precision issues.
Types ¶
type DecoderDecorator ¶
type DecoderDecorator interface {
DecorateDecoder(typ reflect2.Type, dec jsoniter.ValDecoder) jsoniter.ValDecoder
}
type EncoderDecorator ¶
type EncoderDecorator interface {
DecorateEncoder(typ reflect2.Type, dec jsoniter.ValEncoder) jsoniter.ValEncoder
}
type Extension ¶
type Extension struct { jsoniter.DummyExtension // Codecs overrides the TimeCodec registry to use for resolving TimeCodecs. // If this option is `nil` the default registry is used. Codecs *Registry // TagName sets the struct tag name to use for tcodec options. // If this option is not set the `DefaultTagName` will be used. TagName string }
Extension is a jsoniter.Extension that decodes JSON values to time.Time and encodes back to JSON. The extension reads `tcodec` struct tags and matches to registered TimeCodecs. ```
type Foo struct { Timestamp time.Time `json:"ts" tcodec:"rfc3339"` }
```
To decode/encode a field using a specific layout use `layout=GO_TIME_LAYOUT` tag value.
```
type Foo struct { CustomTimestamp time.Time `json:"ts_custom" tcodec:"layout=2006/01/02 15:04"` }
```
func (*Extension) UpdateStructDescriptor ¶
func (ext *Extension) UpdateStructDescriptor(desc *jsoniter.StructDescriptor)
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
func DefaultRegistry ¶
func DefaultRegistry() *Registry
func NewRegistry ¶
func NewRegistry() *Registry
func (*Registry) MustRegister ¶
type TimeCodec ¶
type TimeCodec interface { TimeEncoder TimeDecoder }
TimeCodec can decode/encode time.Time values using jsoniter.
func Join ¶
func Join(decode TimeDecoder, encode TimeEncoder) TimeCodec
Join is a helper to compose a TimeCodec from a decoder and an encoder.
func LayoutCodec ¶
LayoutCodec uses a time layout to decode/encode a timestamp from a JSON value.
func StdCodec ¶
func StdCodec() TimeCodec
StdCodec behaves like the default UnmarshalJSON/MarshalJSON for time.Time values. The tcodec extension uses this TimeCodec when no `tcodec` tag is present on a field of type time.Time and the extension has no Config.DefaultCodec defined.
func StrftimeCodec ¶ added in v1.10.0
StrftimeCodec uses strftime format to decode/encode a timestamp from a JSON string. https://strftime.org/ https://linux.die.net/man/3/strftime
func UnixMicrosecondsCodec ¶ added in v1.10.0
func UnixMicrosecondsCodec() TimeCodec
UnixMicrosecondsCodec decodes/encodes a timestamps in UNIX millisecond epoch. It decodes both string and number JSON values and encodes always to number.
func UnixMillisecondsCodec ¶
func UnixMillisecondsCodec() TimeCodec
UnixMillisecondsCodec decodes/encodes a timestamps in UNIX millisecond epoch. It decodes both string and number JSON values and encodes always to number.
func UnixNanosecondsCodec ¶ added in v1.10.0
func UnixNanosecondsCodec() TimeCodec
UnixNanosecondsCodec decodes/encodes a timestamps in UNIX millisecond epoch. It decodes both string and number JSON values and encodes always to number.
func UnixSecondsCodec ¶
func UnixSecondsCodec() TimeCodec
UnixSecondsCodec decodes/encodes a timestamp from seconds since UNIX epoch. Fractions of a second can be set using the fractional part of a float. Precision is kept up to Microseconds to avoid float64 precision issues.
type TimeDecoder ¶
TimeDecoder can decode time.Time values from a jsoniter.Iterator.
func DecodeIn ¶
func DecodeIn(loc *time.Location, dec TimeDecoder) TimeDecoder
DecodeIn forces a `time.Location` on all decoded timestamps
func TryDecoders ¶
func TryDecoders(dec TimeDecoder, fallback ...TimeDecoder) TimeDecoder
TryDecoders returns a TimeDecoder that tries to decode a time.Time using `dec` and then each of the `fallback` decoders in order.
type TimeDecoderFunc ¶
TimeDecoderFunc is a helper to easily define TimeDecoder values.
func (TimeDecoderFunc) DecodeTime ¶
func (fn TimeDecoderFunc) DecodeTime(iter *jsoniter.Iterator) time.Time
type TimeEncoder ¶
TimeEncoder can encode time.Time values onto a jsoniter.Stream.
func EncodeIn ¶
func EncodeIn(loc *time.Location, enc TimeEncoder) TimeEncoder
EncodeIn forces a `time.Location` on all encoded timestamps
type TimeEncoderFunc ¶
TimeEncoderFunc is a helper to easily define TimeEncoder values.
func (TimeEncoderFunc) EncodeTime ¶
func (fn TimeEncoderFunc) EncodeTime(tm time.Time, stream *jsoniter.Stream)