serializer

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedSerializer 不支持的序列化器类型
	ErrUnsupportedSerializer = fmt.Errorf("unsupported serializer type")
)

错误定义

Functions

This section is empty.

Types

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer JSON 序列化器

func (*JSONSerializer) Marshal

func (j *JSONSerializer) Marshal(value any) ([]byte, error)

Marshal 序列化为 JSON

func (*JSONSerializer) Unmarshal

func (j *JSONSerializer) Unmarshal(data []byte, dest any) error

Unmarshal 从 JSON 反序列化

type MessagePackSerializer

type MessagePackSerializer struct{}

MessagePackSerializer MessagePack 序列化器

func (*MessagePackSerializer) Marshal

func (m *MessagePackSerializer) Marshal(value any) ([]byte, error)

Marshal 序列化为 MessagePack MessagePack 比 JSON 更高效:序列化速度快 2-3 倍,数据体积小 20-30%

func (*MessagePackSerializer) Unmarshal

func (m *MessagePackSerializer) Unmarshal(data []byte, dest any) error

Unmarshal 从 MessagePack 反序列化

type Serializer

type Serializer interface {
	Marshal(value any) ([]byte, error)
	Unmarshal(data []byte, dest any) error
}

Serializer 定义序列化接口

func New

func New(serializerType string) (Serializer, error)

New 创建序列化器

支持的序列化器类型:

  • "json": 标准库 JSON 序列化,兼容性最好
  • "msgpack": MessagePack 二进制序列化,性能更优

Jump to

Keyboard shortcuts

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