json

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Marshal 将 Go 值编码为 JSON
	Marshal = api.Marshal

	// MarshalIndent 将 Go 值编码为格式化的 JSON
	MarshalIndent = api.MarshalIndent

	// MarshalToString 将 Go 值编码为 JSON 字符串
	MarshalToString = api.MarshalToString

	// Unmarshal 将 JSON 解码为 Go 值
	Unmarshal = api.Unmarshal

	// UnmarshalFromString 从 JSON 字符串解码为 Go 值
	UnmarshalFromString = api.UnmarshalFromString

	// NewEncoder 创建一个将数据写入 w 的编码器
	NewEncoder = api.NewEncoder

	// NewDecoder 创建一个从 r 读取数据的解码器
	NewDecoder = api.NewDecoder

	// Valid 检查 JSON 数据是否有效
	Valid = api.Valid
)

Functions

func Compact

func Compact(dst *[]byte, src []byte) error

Compact 将 JSON 数据压缩为最小表示形式

func Decode added in v0.7.0

func Decode(input interface{}, output interface{}) error

Decode 使用 mapstructure 将输入值解码到输出结构

默认配置: - 使用 "json" 标签 - 允许弱类型转换 (WeaklyTypedInput: true) - 允许元数据 (Metadata: nil)

这是一个便捷函数,用于替代 json.Unmarshal(json.Marshal(input), output) 的常见模式 性能更高,因为它避免了 JSON 序列化/反序列化的开销

func DecodeFromReader

func DecodeFromReader(r io.Reader, v interface{}) error

DecodeFromReader 从 reader 读取并解码 JSON 数据

func EncodeToWriter

func EncodeToWriter(w io.Writer, v interface{}) error

EncodeToWriter 将 Go 值编码为 JSON 并写入 writer

func Get

func Get(data []byte, path ...interface{}) (interface{}, error)

Get 从 JSON 数据中获取指定路径的值

使用 sonic 的高性能查询 API

func GetBool

func GetBool(data []byte, path ...interface{}) (bool, error)

GetBool 从 JSON 数据中获取布尔值

func GetFloat64

func GetFloat64(data []byte, path ...interface{}) (float64, error)

GetFloat64 从 JSON 数据中获取浮点数值

func GetInt64

func GetInt64(data []byte, path ...interface{}) (int64, error)

GetInt64 从 JSON 数据中获取整数值

func GetString

func GetString(data []byte, path ...interface{}) (string, error)

GetString 从 JSON 数据中获取字符串值

func HTMLEscape

func HTMLEscape(dst *[]byte, src []byte)

HTMLEscape 将 JSON 数据中的 HTML 特殊字符转义 HTMLEscape 将 JSON 数据中的 HTML 特殊字符转义

func Indent

func Indent(dst *[]byte, src []byte, prefix, indent string) error

Indent 将 JSON 数据格式化为易读的缩进形式

func MarshalWithOptions

func MarshalWithOptions(v interface{}, opts MarshalOptions) ([]byte, error)

MarshalWithOptions 使用指定选项将 Go 值编码为 JSON

This function uses pre-frozen configurations for optimal performance. All 32 possible combinations of options are cached at package initialization.

func UnmarshalWithOptions

func UnmarshalWithOptions(data []byte, v interface{}, opts UnmarshalOptions) error

UnmarshalWithOptions 使用指定选项将 JSON 解码为 Go 值

This function uses pre-frozen configurations for optimal performance. All 8 possible combinations of options are cached at package initialization.

Types

type Decoder

type Decoder = sonic.Decoder

Decoder 从输入流读取并解码 JSON 数据

type Encoder

type Encoder = sonic.Encoder

Encoder 将 Go 值编码为 JSON 并写入输出流

type MarshalOptions

type MarshalOptions struct {
	// EscapeHTML 指定是否在 JSON 引号字符串中转义有问题的 HTML 字符
	// 默认值为 true,与标准库保持一致
	EscapeHTML bool

	// SortMapKeys 指定是否对 map 的键进行排序
	// 默认值为 false
	SortMapKeys bool

	// ValidateString 指定是否验证字符串为有效的 UTF-8
	// 默认值为 false
	ValidateString bool

	// NoNullSliceOrMap 指定是否将 nil slice/map 编码为空 slice/map
	// 默认值为 false
	NoNullSliceOrMap bool

	// NoQuoteTextMarshaler 指定是否对实现 encoding.TextMarshaler 的类型不加引号
	// 默认值为 false
	NoQuoteTextMarshaler bool
}

MarshalOptions JSON 序列化选项

type Marshaler

type Marshaler = stdjson.Marshaler

Marshaler 是由可以将自身序列化为有效 JSON 的类型实现的接口

type Number

type Number = stdjson.Number

Number 代表 JSON 数字字面量

type RawMessage

type RawMessage = stdjson.RawMessage

RawMessage 是原始编码的 JSON 值 它实现了 Marshaler 和 Unmarshaler 接口

type UnmarshalOptions

type UnmarshalOptions struct {
	// UseNumber 指定是否将数字解码为 Number 而不是 float64
	// 默认值为 false
	UseNumber bool

	// DisallowUnknownFields 指定是否在遇到未知字段时返回错误
	// 默认值为 false
	DisallowUnknownFields bool

	// CopyString 指定是否复制 JSON 字符串而不是引用
	// 默认值为 false
	CopyString bool
}

UnmarshalOptions JSON 反序列化选项

type Unmarshaler

type Unmarshaler = stdjson.Unmarshaler

Unmarshaler 是由可以从 JSON 描述反序列化自身的类型实现的接口

Jump to

Keyboard shortcuts

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