Documentation
¶
Overview ¶
Package json provides a configurable JSON encoding/decoding layer. It defaults to encoding/json but can be swapped for faster implementations like github.com/goccy/go-json or github.com/bytedance/sonic.
Usage:
import json "github.com/antflydb/antfly/pkg/libaf/json" // Works like encoding/json data, err := json.Marshal(v) err = json.Unmarshal(data, &v)
To use a different JSON library:
import (
json "github.com/antflydb/antfly/pkg/libaf/json"
gojson "github.com/goccy/go-json"
)
func init() {
json.SetConfig(json.Config{
Marshal: gojson.Marshal,
MarshalIndent: gojson.MarshalIndent,
MarshalString: gojson.MarshalString,
Unmarshal: gojson.Unmarshal,
UnmarshalString: gojson.UnmarshalString,
NewEncoder: func(w io.Writer) json.Encoder {
return gojson.NewEncoder(w)
},
NewDecoder: func(r io.Reader) json.Decoder {
return gojson.NewDecoder(r)
},
})
}
Index ¶
- func EncodeIndented(v any, prefix, indent string, opts ...EncodeOption) (result []byte, err error)
- func Marshal(v any) ([]byte, error)
- func MarshalIndent(v any, prefix, indent string) ([]byte, error)
- func MarshalString(v any) (string, error)
- func SetConfig(c Config)
- func Unmarshal(data []byte, v any) error
- func UnmarshalString(s string, v any) error
- type Config
- type Decoder
- type EncodeOption
- type Encoder
- type Marshaler
- type Number
- type RawMessage
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeIndented ¶
func EncodeIndented(v any, prefix, indent string, opts ...EncodeOption) (result []byte, err error)
EncodeIndented returns the JSON encoding of v with indentation. It accepts optional EncodeOptions like SortMapKeys. Note: SortMapKeys is a no-op with encoding/json as it always sorts keys.
If the configured JSON library panics (e.g. goccy/go-json on nil union fields from oapi-codegen), it falls back to encoding/json. Note: opts and SortMapKeys are currently unused; they exist for future alternative JSON library support.
func MarshalIndent ¶
MarshalIndent is like Marshal but applies Indent to format the output.
func MarshalString ¶
MarshalString returns the JSON encoding of v as a string.
func SetConfig ¶
func SetConfig(c Config)
SetConfig sets the global JSON configuration. Call this before using any JSON functions to use a custom JSON library.
func UnmarshalString ¶
UnmarshalString parses the JSON-encoded string and stores the result in v.
Types ¶
type Config ¶
type Config struct {
Marshal func(v any) ([]byte, error)
MarshalIndent func(v any, prefix, indent string) ([]byte, error)
MarshalString func(v any) (string, error)
Unmarshal func(data []byte, v any) error
UnmarshalString func(s string, v any) error
NewEncoder func(w io.Writer) Encoder
NewDecoder func(r io.Reader) Decoder
}
Config holds the JSON encoding/decoding functions.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default configuration using encoding/json.
type Decoder ¶
Decoder is the interface for streaming JSON decoding. Both encoding/json and alternative libraries satisfy this interface.
func NewDecoder ¶
NewDecoder returns a new Decoder that reads from r.
type EncodeOption ¶
type EncodeOption func(*encodeOptions)
EncodeOption represents an option for JSON encoding.
var SortMapKeys EncodeOption = func(o *encodeOptions) { o.sortMapKeys = true }
SortMapKeys is an option that sorts map keys in the output.
type Encoder ¶
Encoder is the interface for streaming JSON encoding. Both encoding/json and alternative libraries satisfy this interface.
func NewEncoder ¶
NewEncoder returns a new Encoder that writes to w.
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves into valid JSON.
type RawMessage ¶
type RawMessage = stdjson.RawMessage
RawMessage is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding.
type Unmarshaler ¶
type Unmarshaler = stdjson.Unmarshaler
Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.