encoder

package
v0.0.0-...-b559f02 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: GPL-3.0 Imports: 6 Imported by: 45

Documentation

Overview

Package encoder supports to encode and decode structs in multiple formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddEncodersExtension

func AddEncodersExtension(t hint.Type, ext string) (bool, error)

func Decode

func Decode[T any](enc Encoder, b []byte, v *T) error

func DecodeReader

func DecodeReader[T any](enc Encoder, r io.Reader, v *T) error

func EncodersExtension

func EncodersExtension(t hint.Type) (string, bool)

func Ptr

func Ptr(i interface{}) (ptr reflect.Value, elem reflect.Value)

Types

type DecodeDetail

type DecodeDetail struct {
	Instance interface{}
	Decode   DecodeFunc
	Desc     string
	Hint     hint.Hint
}

func AnalyzeSetHinter

func AnalyzeSetHinter(d DecodeDetail, v interface{}) DecodeDetail

func (DecodeDetail) IsValid

func (d DecodeDetail) IsValid([]byte) error

type DecodeFunc

type DecodeFunc func([]byte, hint.Hint) (interface{}, error)

type Encoder

type Encoder interface {
	hint.Hinter
	Add(DecodeDetail) error
	AddHinter(hint.Hinter) error
	Marshal(interface{}) ([]byte, error) // NOTE native marshaler func
	Unmarshal([]byte, interface{}) error // NOTE native unmarshaler func
	StreamEncoder(io.Writer) util.StreamEncoder
	StreamDecoder(io.Reader) util.StreamDecoder
	Decode([]byte) (interface{}, error)                        // NOTE decode by hint inside []byte
	DecodeWithHint([]byte, hint.Hint) (interface{}, error)     // NOTE decode []byte by given hint
	DecodeWithHintType([]byte, hint.Type) (interface{}, error) // NOTE decode []byte by given type
	DecodeWithFixedHintType(string, int) (interface{}, error)  // NOTE decode string by fixed type size
	DecodeSlice([]byte) ([]interface{}, error)                 // NOTE decode sliced data
}

type Encoders

type Encoders struct {
	*hint.CompatibleSet[Encoder]
	// contains filtered or unexported fields
}

func NewEncoders

func NewEncoders(defaultenc, jenc Encoder) *Encoders

func (*Encoders) AddDetail

func (encs *Encoders) AddDetail(d DecodeDetail) error

func (*Encoders) AddEncoder

func (encs *Encoders) AddEncoder(enc Encoder) error

func (*Encoders) AddHinter

func (encs *Encoders) AddHinter(hr hint.Hinter) error

func (*Encoders) Default

func (encs *Encoders) Default() Encoder

func (*Encoders) JSON

func (encs *Encoders) JSON() Encoder

Directories

Path Synopsis
Package jsonenc support encoding and decoding by json.
Package jsonenc support encoding and decoding by json.

Jump to

Keyboard shortcuts

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