codec

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package codec provides encoder/decoder interfaces and implementations.

Package codec provides encoder/decoder implementations for various formats.

Index

Constants

View Source
const (
	// ContentTypeXML is the MIME type for XML.
	ContentTypeXML = "application/xml"
	// ContentTypeTextXML is the text MIME type for XML.
	ContentTypeTextXML = "text/xml"
)
View Source
const (
	// ContentTypeJSON is the MIME type for JSON.
	ContentTypeJSON = "application/json"
)

Variables

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

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the global default codec registry.

View Source
var XML = NewXMLCodec()

XML is the default XML codec instance.

Functions

func Marshal

func Marshal(v any) ([]byte, error)

Marshal is a convenience function for JSON encoding using Sonic.

func Register

func Register(contentType string, codec Codec)

Register registers a codec in the default registry.

func RegisterFast added in v0.1.1

func RegisterFast(contentType string, c Codec)

RegisterFast registers a codec in both the default registry and fast cache. Use this for frequently accessed content types to avoid lock contention.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal is a convenience function for JSON decoding using Sonic.

Types

type Codec

type Codec interface {
	Encoder
	Decoder
}

Codec combines Encoder and Decoder interfaces.

func GetCodec

func GetCodec(contentType string) (Codec, bool)

GetCodec returns a codec from the default registry.

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

func GetDecoder(contentType string) (Decoder, bool)

GetDecoder returns a decoder from the default registry.

type DecoderFunc

type DecoderFunc func(data []byte, v any) error

DecoderFunc is a function adapter for Decoder interface.

func (DecoderFunc) Decode

func (f DecoderFunc) Decode(data []byte, v any) error

Decode implements Decoder.

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

func GetEncoder(contentType string) (Encoder, bool)

GetEncoder returns an encoder from the default registry.

type EncoderFunc

type EncoderFunc struct {
	EncodeFunc      func(v any) ([]byte, error)
	ContentTypeFunc func() string
}

EncoderFunc is a function adapter for Encoder interface.

func (EncoderFunc) ContentType

func (f EncoderFunc) ContentType() string

ContentType implements Encoder.

func (EncoderFunc) Encode

func (f EncoderFunc) Encode(v any) ([]byte, error)

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

func NewRegistry() *Registry

NewRegistry creates a new codec registry.

func (*Registry) ContentTypes

func (r *Registry) ContentTypes() []string

ContentTypes returns all registered content types.

func (*Registry) GetCodec

func (r *Registry) GetCodec(contentType string) (Codec, bool)

GetCodec returns the codec for the given content type. Uses fast path for commonly registered codecs to avoid lock contention.

func (*Registry) GetDecoder

func (r *Registry) GetDecoder(contentType string) (Decoder, bool)

GetDecoder returns the decoder for the given content type.

func (*Registry) GetEncoder

func (r *Registry) GetEncoder(contentType string) (Encoder, bool)

GetEncoder returns the encoder for the given content type.

func (*Registry) Has

func (r *Registry) Has(contentType string) bool

Has checks if a codec is registered for the given content type.

func (*Registry) Register

func (r *Registry) Register(contentType string, codec Codec)

Register registers a codec for the given content type.

func (*Registry) Remove

func (r *Registry) Remove(contentType string)

Remove removes the codec 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.

func (*SonicCodec) Decode

func (c *SonicCodec) Decode(data []byte, v any) error

Decode decodes JSON bytes into the destination using Sonic.

func (*SonicCodec) Encode

func (c *SonicCodec) Encode(v any) ([]byte, error)

Encode encodes the value to JSON bytes using Sonic.

type XMLCodec

type XMLCodec struct{}

XMLCodec implements Codec for XML encoding/decoding.

func NewXMLCodec

func NewXMLCodec() *XMLCodec

NewXMLCodec creates a new XML codec.

func (*XMLCodec) ContentType

func (c *XMLCodec) ContentType() string

ContentType returns the XML MIME type.

func (*XMLCodec) Decode

func (c *XMLCodec) Decode(data []byte, v any) error

Decode decodes XML bytes into the destination.

func (*XMLCodec) Encode

func (c *XMLCodec) Encode(v any) ([]byte, error)

Encode encodes the value to XML bytes.

Jump to

Keyboard shortcuts

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