tcodec

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2020 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
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 MustRegister(name string, codec TimeCodec)

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 Register

func Register(name string, codec TimeCodec) error

func Split

func Split(codec TimeCodec) (TimeDecoder, TimeEncoder)

Split is a helper to split a TimeCodec into a decoder and an encoder.

func UnixMilliseconds

func UnixMilliseconds(n int64) time.Time

UnixMilliseconds reads a timestamp from milliseconds since UNIX epoch.

func UnixSeconds

func UnixSeconds(sec float64) time.Time

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) Extend

func (r *Registry) Extend(others ...*Registry)

func (*Registry) Lookup

func (r *Registry) Lookup(name string) TimeCodec

func (*Registry) MustRegister

func (r *Registry) MustRegister(name string, codec TimeCodec)

func (*Registry) Register

func (r *Registry) Register(name string, codec TimeCodec) error

type Time

type Time = time.Time

type TimeCodec

type TimeCodec interface {
	TimeEncoder
	TimeDecoder
}

TimeCodec can decode/encode time.Time values using jsoniter.

func In

func In(loc *time.Location, codec TimeCodec) TimeCodec

In forces a `time.Location` on all decoded/encoded timestamps

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

func LayoutCodec(layout string) TimeCodec

LayoutCodec uses a time layout to decode/encode a timestamp from a JSON value.

func Lookup

func Lookup(name string) TimeCodec

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 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 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

type TimeDecoder interface {
	DecodeTime(iter *jsoniter.Iterator) time.Time
}

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

type TimeDecoderFunc func(iter *jsoniter.Iterator) time.Time

TimeDecoderFunc is a helper to easily define TimeDecoder values.

func (TimeDecoderFunc) DecodeTime

func (fn TimeDecoderFunc) DecodeTime(iter *jsoniter.Iterator) time.Time

type TimeEncoder

type TimeEncoder interface {
	EncodeTime(tm time.Time, stream *jsoniter.Stream)
}

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

type TimeEncoderFunc func(tm time.Time, stream *jsoniter.Stream)

TimeEncoderFunc is a helper to easily define TimeEncoder values.

func (TimeEncoderFunc) EncodeTime

func (fn TimeEncoderFunc) EncodeTime(tm time.Time, stream *jsoniter.Stream)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL