codec

package
v0.0.0-...-86df4a9 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMessage = errors.New("message type is invalid")
)

Functions

func RegisterCodec

func RegisterCodec(t Type, codec Codec)

RegisterCodec 注册编解码器

Types

type Codec

type Codec interface {
	// Encode 将对象序列化为字节数组
	Encode(v interface{}) ([]byte, error)

	// Decode 将字节数组反序列化为对象
	Decode(data []byte, v interface{}) error

	// Name 返回编解码器的名称
	Name() string
}

Codec 定义了序列化和反序列化的接口

func GetCodec

func GetCodec(t Type) Codec

GetCodec 获取编解码器

type JsonCodec

type JsonCodec struct{}

JsonCodec 实现了 Codec 接口

func (*JsonCodec) Decode

func (c *JsonCodec) Decode(data []byte, v interface{}) error

Decode 将 JSON 字节数组反序列化为对象

func (*JsonCodec) Encode

func (c *JsonCodec) Encode(v interface{}) ([]byte, error)

Encode 将对象序列化为 JSON 字节数组

func (*JsonCodec) Name

func (c *JsonCodec) Name() string

Name 返回编解码器的名称

type ProtobufCodec

type ProtobufCodec struct{}

ProtobufCodec 实现了 Codec 接口

func (*ProtobufCodec) Decode

func (c *ProtobufCodec) Decode(data []byte, v interface{}) error

Decode 将 Protobuf 字节数组反序列化为对象

func (*ProtobufCodec) Encode

func (c *ProtobufCodec) Encode(v interface{}) ([]byte, error)

Encode 将对象序列化为 Protobuf 字节数组

func (*ProtobufCodec) Name

func (c *ProtobufCodec) Name() string

Name 返回编解码器的名称

type Type

type Type uint8

Type 定义了支持的序列化类型

const (
	JSON Type = iota
	Protobuf
)

Jump to

Keyboard shortcuts

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