Documentation
¶
Overview ¶
Package codec provides encoder/decoder interfaces and implementations.
Package codec provides encoder/decoder implementations for various formats.
Index ¶
- Constants
- Variables
- func Marshal(v any) ([]byte, error)
- func Register(contentType string, codec Codec)
- func RegisterFast(contentType string, c Codec)
- func Unmarshal(data []byte, v any) error
- type Codec
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type JSONCodec
- type Registry
- func (r *Registry) ContentTypes() []string
- func (r *Registry) GetCodec(contentType string) (Codec, bool)
- func (r *Registry) GetDecoder(contentType string) (Decoder, bool)
- func (r *Registry) GetEncoder(contentType string) (Encoder, bool)
- func (r *Registry) Has(contentType string) bool
- func (r *Registry) Register(contentType string, codec Codec)
- func (r *Registry) Remove(contentType string)
- type SonicCodec
- type XMLCodec
Constants ¶
const ( // ContentTypeXML is the MIME type for XML. ContentTypeXML = "application/xml" // ContentTypeTextXML is the text MIME type for XML. ContentTypeTextXML = "text/xml" )
const (
// ContentTypeJSON is the MIME type for JSON.
ContentTypeJSON = "application/json"
)
Variables ¶
var ( // Sonic is the default Sonic codec instance. Sonic = NewSonicCodec() // SonicFastest is the fastest Sonic codec instance. SonicFastest = NewSonicCodecFastest() // JSON is the default JSON codec instance (alias for Sonic). JSON = Sonic )
Codec instances
var DefaultRegistry = NewRegistry()
DefaultRegistry is the global default codec registry.
var XML = NewXMLCodec()
XML is the default XML codec instance.
Functions ¶
func RegisterFast ¶ added in v0.1.1
RegisterFast registers a codec in both the default registry and fast cache. Use this for frequently accessed content types to avoid lock contention.
Types ¶
type Decoder ¶
type Decoder interface {
// Decode decodes the given bytes into the destination.
Decode(data []byte, v any) error
}
Decoder defines the interface for decoding data.
func GetDecoder ¶
GetDecoder returns a decoder from the default registry.
type DecoderFunc ¶
DecoderFunc is a function adapter for Decoder interface.
type Encoder ¶
type Encoder interface {
// Encode encodes the given value to bytes.
Encode(v any) ([]byte, error)
// ContentType returns the MIME type for this encoder.
ContentType() string
}
Encoder defines the interface for encoding data.
func GetEncoder ¶
GetEncoder returns an encoder from the default registry.
type EncoderFunc ¶
EncoderFunc is a function adapter for Encoder interface.
func (EncoderFunc) ContentType ¶
func (f EncoderFunc) ContentType() string
ContentType implements Encoder.
type JSONCodec ¶
type JSONCodec = SonicCodec
JSONCodec is an alias for SonicCodec for backward compatibility.
func NewJSONCodec ¶
func NewJSONCodec() *JSONCodec
NewJSONCodec creates a new JSON codec (alias for NewSonicCodec).
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages codec registration by content type.
func (*Registry) ContentTypes ¶
ContentTypes returns all registered content types.
func (*Registry) GetCodec ¶
GetCodec returns the codec for the given content type. Uses fast path for commonly registered codecs to avoid lock contention.
func (*Registry) GetDecoder ¶
GetDecoder returns the decoder for the given content type.
func (*Registry) GetEncoder ¶
GetEncoder returns the encoder for the given content type.
type SonicCodec ¶
type SonicCodec struct {
// contains filtered or unexported fields
}
SonicCodec implements Codec using bytedance/sonic for high-performance JSON. Sonic is significantly faster than encoding/json on amd64 and arm64 platforms.
func NewSonicCodec ¶
func NewSonicCodec() *SonicCodec
NewSonicCodec creates a new Sonic JSON codec with default settings.
func NewSonicCodecFastest ¶
func NewSonicCodecFastest() *SonicCodec
NewSonicCodecFastest creates a Sonic codec optimized for maximum speed. Note: This may sacrifice some compatibility for performance.
func NewSonicCodecStd ¶
func NewSonicCodecStd() *SonicCodec
NewSonicCodecStd creates a Sonic codec with standard library compatibility.
func (*SonicCodec) ContentType ¶
func (c *SonicCodec) ContentType() string
ContentType returns the JSON MIME type.