配置在上面的自定义流式编码解码器,细心的朋友可能看到我们创建编码器和解码器的时候,是通过sonic.ConfigDefault.NewEncoder() / sonic.ConfigDefault.NewDecoder()这两个函数进行调用的 。那么sonic.ConfigDefault是什么?
我们可以通过查看源码:
var (// ConfigDefault is the default config of APIs, AIming at efficiency and safty.// ConfigDefault api的默认配置,针对效率和安全 。ConfigDefault = Config{}.Froze()// ConfigStd is the standard config of APIs, aiming at being compatible with encoding/json.// ConfigStd是api的标准配置,旨在与encoding/json兼容 。ConfigStd = Config{Escapehtml : true,SortMapKeys: true,CompactMarshaler: true,CopyString : true,ValidateString : true,}.Froze()// ConfigFastest is the fastest config of APIs, aiming at speed.// ConfigFastest是api的最快配置,旨在提高速度 。ConfigFastest = Config{NoQuoteTextMarshaler: true,}.Froze())sonic提供了三种常用的Config配置 。这些配置中对一些场景已经预定义好了对应的Config 。
其实我们使用的sonic.Marshal()函数就是调用了默认的ConfigDefault
// Marshal returns the JSON encoding bytes of v.func Marshal(val interface{}) ([]byte, error) {return ConfigDefault.Marshal(val)}// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.// NOTICE: This API copies given buffer by default,// if you want to pass JSON more efficiently, use UnmarshalString instead.func Unmarshal(buf []byte, val interface{}) error {return ConfigDefault.Unmarshal(buf, val)}但是在一些场景下我们不满足于sonic预定义的三个Config 。此时我们可以定义自己的Config进行个性化的编码和解码 。
首先先看一下Config的结构 。
// Config is a combination of sonic/encoder.Options and sonic/decoder.Optionstype Config struct {// EscapeHTML indicates encoder to escape all HTML characters// after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape).// WARNING: This hurts performance A LOT, USE WITH CARE.EscapeHTMLbool// SortMapKeys indicates encoder that the keys of a map needs to be sorted// before serializing into JSON.// WARNING: This hurts performance A LOT, USE WITH CARE.SortMapKeysbool// CompactMarshaler indicates encoder that the output JSON from json.Marshaler// is always compact and needs no validationCompactMarshalerbool// NoQuoteTextMarshaler indicates encoder that the output text from encoding.TextMarshaler// is always escaped string and needs no quotingNoQuoteTextMarshalerbool// NoNullSliceOrMap indicates encoder that all empty Array or Object are encoded as '[]' or '{}',// instead of 'null'NoNullSliceOrMapbool// UseInt64 indicates decoder to unmarshal an integer into an interface{} as an// int64 instead of as a float64.UseInt64bool// UseNumber indicates decoder to unmarshal a number into an interface{} as a// json.Number instead of as a float64.UseNumberbool// UseUnicodeErrors indicates decoder to return an error when encounter invalid// UTF-8 escape sequences.UseUnicodeErrorsbool// DisallowUnknownFields indicates decoder to return an error when the destination// is a struct and the input contains object keys which do not match any// non-ignored, exported fields in the destination.DisallowUnknownFieldsbool// CopyString indicates decoder to decode string values by copying instead of referring.CopyStringbool// ValidateString indicates decoder and encoder to valid string values: decoder will return errors// when unescaped control chars(u0000-u001f) in the string value of JSON.ValidateStringbool}由于字段较多 。笔者就选择几个字段进行演示,其他字段使用方式都是一致 。
假设我们希望对JSON序列化按照key进行排序以及将JSON编码成紧凑的格式 。我们可以配置Config进行Marshal操作
func base() { snc := sonic.Config{CompactMarshaler: true,SortMapKeys:true, }.Froze() snc.Marshal(obj)}考虑到排序带来的性能损失(约 10% ),sonic 默认不会启用这个功能 。
Sonic 默认将基本类型( struct , map 等)编码为紧凑格式的 JSON,除非使用 json.RawMessage or json.Marshaler 进行编码:sonic 确保输出的 JSON 合法,但出于性能考虑,不会加工成紧凑格式 。我们提供选项 encoder.CompactMarshaler 来添加此过程,Ast.Nodesonic提供了Ast.Node的功能 。Sonic/ast.Node 是完全独立的 JSON 抽象语法树库 。它实现了序列化和反序列化,并提供了获取和修改通用数据的鲁棒的 API 。
先来简单介绍一下Ast.Node:ast.Node 通常指的是编程语言中的抽象语法树(Abstract Syntax Tree)节点 。抽象语法树是编程语言代码在编译器中的内部表示,它以树状结构展现代码的语法结构,便于编译器进行语法分析、语义分析、优化等操作 。
推荐阅读
- EasyNetQ库:让你的分布式系统消息开发快人一步!
- C# 中的 ref 已经被放开,或许你已经不认识了
- Oracle数据库分区技术:优化大型数据集的存储效率!
- Docker容器中的Postgresql备份脚本异常解决办法
- 目前有哪些比较成功的人工智能应用?
- 为什么色准越高的显示设备就越贵
- AI要被卡脖子了?训练大模型的数据或在2026年耗尽
- 什么是知识图谱
- 什么是智能网联技术
- AI可以读取人心了 心中的小秘密还能藏多久?
