serializer

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GobSerializer

type GobSerializer struct{}

GobSerializer gob编码序列化器 使用Go标准库的encoding/gob包 优点:类型安全,支持复杂结构体,支持nil值 缺点:仅限Go语言使用,不能跨语言

func NewGob

func NewGob() *GobSerializer

NewGob 创建Gob序列化器

func (*GobSerializer) Decode

func (g *GobSerializer) Decode(data []byte, obj any) error

Decode 使用gob反序列化

func (*GobSerializer) Encode

func (g *GobSerializer) Encode(value interface{}) ([]byte, error)

Encode 使用gob序列化缓存值

func (*GobSerializer) Name

func (g *GobSerializer) Name() string

Name 返回序列化器名称

type JsonSerializer

type JsonSerializer struct{}

JsonSerializer JSON序列化器 使用Go标准库的encoding/json包 优点:人类可读,跨语言支持,易于调试 缺点:性能较gob慢,某些类型支持不完整(如复杂指针、interface{})

func NewJson

func NewJson() *JsonSerializer

NewJson 创建JSON序列化器

func (*JsonSerializer) Decode

func (j *JsonSerializer) Decode(data []byte, obj any) error

Decode 使用JSON反序列化

func (*JsonSerializer) Encode

func (j *JsonSerializer) Encode(value interface{}) ([]byte, error)

Encode 使用JSON序列化缓存值

func (*JsonSerializer) Name

func (j *JsonSerializer) Name() string

Name 返回序列化器名称

type Serializer

type Serializer interface {
	// Encode 将值序列化为字节数组
	Encode(value interface{}) ([]byte, error)

	// Decode 将字节数组反序列化为值
	// obj 必须是指针类型
	Decode(data []byte, obj any) error

	// Name 返回序列化器的名称
	Name() string
}

Serializer 序列化器接口 定义了缓存值的编码和解码方法

Jump to

Keyboard shortcuts

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