Documentation
¶
Index ¶
- Variables
- func Compact(dst *[]byte, src []byte) error
- func Decode(input interface{}, output interface{}) error
- func DecodeFromReader(r io.Reader, v interface{}) error
- func EncodeToWriter(w io.Writer, v interface{}) error
- func Get(data []byte, path ...interface{}) (interface{}, error)
- func GetBool(data []byte, path ...interface{}) (bool, error)
- func GetFloat64(data []byte, path ...interface{}) (float64, error)
- func GetInt64(data []byte, path ...interface{}) (int64, error)
- func GetString(data []byte, path ...interface{}) (string, error)
- func HTMLEscape(dst *[]byte, src []byte)
- func Indent(dst *[]byte, src []byte, prefix, indent string) error
- func MarshalWithOptions(v interface{}, opts MarshalOptions) ([]byte, error)
- func UnmarshalWithOptions(data []byte, v interface{}, opts UnmarshalOptions) error
- type Decoder
- type Encoder
- type MarshalOptions
- type Marshaler
- type Number
- type RawMessage
- type UnmarshalOptions
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
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 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 ¶
DecodeFromReader 从 reader 读取并解码 JSON 数据
func EncodeToWriter ¶
EncodeToWriter 将 Go 值编码为 JSON 并写入 writer
func GetFloat64 ¶
GetFloat64 从 JSON 数据中获取浮点数值
func HTMLEscape ¶
HTMLEscape 将 JSON 数据中的 HTML 特殊字符转义 HTMLEscape 将 JSON 数据中的 HTML 特殊字符转义
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 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 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 反序列化选项