codec

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package codec 提供事件序列化和反序列化接口

支持的编解码格式: - JSON: 应用广泛,可读性好,适合调试 - Protobuf: 高效紧凑,适合高性能场景

使用示例:

// JSON 编解码
jsonCodec := codec.NewJSON()
data, _ := jsonCodec.Encode(event)
var evt MyEvent
jsonCodec.Decode(data, &evt)

// Protobuf 编解码
protoCodec := codec.NewProto()
data, _ := protoCodec.Encode(protoEvent)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	// Encode 将值序列化为字节
	// v 可以是任意类型,具体支持类型取决于实现
	Encode(v any) ([]byte, error)

	// Decode 将字节反序列化为指定值
	// data 是序列化后的字节
	// v 必须是指针,用于接收反序列化结果
	Decode(data []byte, v any) error

	// ContentType 返回此编解码器的 MIME 类型
	// 例如: "application/json", "application/protobuf"
	ContentType() string
}

Codec 定义事件编解码器接口 实现者负责将 Go 结构体序列化为字节流,以及反向反序列化

func NewJSON

func NewJSON() Codec

NewJSON 创建新的 JSON 编解码器

type JSON

type JSON struct{}

JSON 实现 Codec 接口,使用 JSON 格式编解码

func (*JSON) ContentType

func (c *JSON) ContentType() string

ContentType 返回 JSON 的 MIME 类型

func (*JSON) Decode

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

Decode 使用 json.Unmarshal 将 JSON 反序列化到 v

func (*JSON) Encode

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

Encode 使用 json.Marshal 序列化值为 JSON

Jump to

Keyboard shortcuts

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