serialization

package
v0.44.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package serialization 序列化相关的操作

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = localeutil.Error("unsupported serialization")

ErrUnsupported 返回不支持序列化的错误信息

当一个对象无法被正常的序列化或是反序列化是,返回此错误。

Functions

This section is empty.

Types

type Files

type Files struct {
	*Serialization
}

Files 提供针对文件的序列化操作

func NewFiles

func NewFiles(c int) *Files

NewFiles 返回 Files 实例

func (*Files) Load

func (f *Files) Load(p string, v interface{}) error

Load 加载文件到 v

func (*Files) LoadFS

func (f *Files) LoadFS(fsys fs.FS, name string, v interface{}) error

LoadFS 加载文件到 v

func (*Files) Save

func (f *Files) Save(p string, v interface{}) error

Save 保存 v 到文件 p

type Locale

type Locale struct {
	// contains filtered or unexported fields
}

Locale 提供了从文件中加载本地化信息的方法

func NewLocale

func NewLocale(b *catalog.Builder, f *Files) *Locale

NewLocale 返回 Locale 实例

f 表示用于加载本地化文件的序列化方法,根据文件扩展名在 f 中查找相应的序列化方法; 加载后的内容被应用在 b 之上。

func (*Locale) Builder

func (l *Locale) Builder() *catalog.Builder

Builder 返回本地化操作的相关接口

func (*Locale) Files

func (l *Locale) Files() *Files

Files 返回用于序列化文件的实例

func (*Locale) LoadFile

func (l *Locale) LoadFile(glob string) error

LoadFile 从文件中加载本地化内容

func (*Locale) LoadFileFS

func (l *Locale) LoadFileFS(fsys fs.FS, glob string) error

LoadFileFS 从文件中加载本地化内容

func (*Locale) Printer

func (l *Locale) Printer(tag language.Tag) *message.Printer

type MarshalFunc

type MarshalFunc func(v interface{}) ([]byte, error)

MarshalFunc 序列化函数原型

type Mimetypes

type Mimetypes struct {
	*Serialization
}

Mimetypes 提供 mimetype 的序列化操作

func NewMimetypes

func NewMimetypes(c int) *Mimetypes

NewMimetypes 返回 Mimetypes 实例

func (*Mimetypes) MarshalFunc

func (ms *Mimetypes) MarshalFunc(header string) (string, MarshalFunc, bool)

MarshalFunc 从 header 解析出当前请求所需要的 mimetype 名称和对应的解码函数

*/* 或是空值 表示匹配任意内容,一般会选择第一个元素作匹配; xx/* 表示匹配以 xx/ 开头的任意元素,一般会选择 xx/* 开头的第一个元素; xx/ 表示完全匹配以 xx/ 的内容 如果传递的内容如下:

application/json;q=0.9,*/*;q=1

则因为 */* 的 q 值比较高,而返回 */* 匹配的内容

在不完全匹配的情况下,返回值的名称依然是具体名称。

text/*;q=0.9

返回的名称可能是:

text/plain

func (*Mimetypes) UnmarshalFunc

func (ms *Mimetypes) UnmarshalFunc(name string) (UnmarshalFunc, bool)

UnmarshalFunc 查找指定名称的 UnmarshalFunc

type Serialization

type Serialization struct {
	// contains filtered or unexported fields
}

Serialization 管理注册的序列化函数

func New

func New(c int) *Serialization

New 声明 Serialization 对象

func (*Serialization) Add

func (s *Serialization) Add(m MarshalFunc, u UnmarshalFunc, name ...string) error

Add 添加序列化函数

m 和 u 可以为 nil,表示仅作为一个占位符使用,具体处理要在 ServeHTTP 中另作处理;

name 表示之后用于查找该序列化函数的唯一 ID, 后期用户可以根据 name 从 c.Search 直接查找相应的序列化函数。

func (*Serialization) Delete

func (s *Serialization) Delete(name string)

Delete 删除指定名称的数据

func (*Serialization) Len

func (s *Serialization) Len() int

Len 返回注册的数量

func (*Serialization) Search

func (s *Serialization) Search(name string) (string, MarshalFunc, UnmarshalFunc)

func (*Serialization) SearchFunc

func (s *Serialization) SearchFunc(match func(string) bool) (string, MarshalFunc, UnmarshalFunc)

SearchFunc 如果返回的 name 为空,表示没有找到

func (*Serialization) Set

func (s *Serialization) Set(name string, m MarshalFunc, u UnmarshalFunc)

Set 修改或是添加

type UnmarshalFunc

type UnmarshalFunc func([]byte, interface{}) error

UnmarshalFunc 反序列化函数原型

Directories

Path Synopsis
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") return web.Object(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package form 用于处理 www-form-urlencoded 编码 func read(ctx *web.Context) { vals := urls.Values{} ctx.Read(vals) } func write(ctx *web.Context) { vals := urls.Values{} vals.Add("name", "caixw") return web.Object(http.StatusOK, vals, nil) } form 用户可以通过定义 form 标签自定义输出的名称,比如: type Username struct { Name string `form:"name"` Age int } 转换成 form-data 可能是以下样式: name=jjj&age=18 该方式对数据类型有一定限制: 1.
Package gob 提供 GOB 格式的编解码
Package gob 提供 GOB 格式的编解码
Package html 提供输出 HTML 内容的解码函数 srv := NewServer() tpl := template.ParseFiles(...) srv.Mimetypes().Add("text/html", html.Marshal, nil) func handle(ctx *web.Context) Responser { return Object(200, html.Tpl(tpl, "index", map[string]interface{}{...}), nil) }
Package html 提供输出 HTML 内容的解码函数 srv := NewServer() tpl := template.ParseFiles(...) srv.Mimetypes().Add("text/html", html.Marshal, nil) func handle(ctx *web.Context) Responser { return Object(200, html.Tpl(tpl, "index", map[string]interface{}{...}), nil) }
Package jsonp JSONP 序列化操作
Package jsonp JSONP 序列化操作
Package text 针对文本内容的编解码实现
Package text 针对文本内容的编解码实现
testobject
Package testobject 用于测试 mimetype 的对象
Package testobject 用于测试 mimetype 的对象

Jump to

Keyboard shortcuts

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