Documentation
¶
Overview ¶
Package json wraps JSON encoding and decoding behind a pluggable Provider, so an application can swap encoding/json for a faster engine in one place and every library that encodes through this package follows.
By default every operation delegates to encoding/json. A provider, such as Piko's sonic provider (piko.sh/piko/wdk/json/json_provider_sonic), replaces the package-level functions (Marshal, Unmarshal and the rest) and the ConfigStd and ConfigDefault APIs when its Activate method runs, which an application does once while it starts.
Dependency injection ¶
A library that would rather be handed an encoder than reach for package state takes an API. Freeze returns one that resolves lazily on its first call, so an API frozen before the application activates its provider still uses that provider. An API also implements Pretoucher, which a library checks for to compile the codecs of the types it decodes ahead of their first use.
Pretouching ¶
Pretouch compiles the codec for a type ahead of its first use, for a provider whose codecs are compiled, such as sonic's. Before such a provider is active the type is recorded, and the provider pretouches every recorded type when it activates, so packages may call Pretouch from init functions in any order. Pretouch is safe for concurrent use.
Activating a provider is not safe while other goroutines encode or decode, so activate it before serving work.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Marshal encodes v as JSON bytes. Marshal func(v any) ([]byte, error) // Unmarshal decodes JSON data into v. Unmarshal func(data []byte, v any) error // MarshalIndent encodes v as indented JSON bytes. MarshalIndent func(v any, prefix, indent string) ([]byte, error) // MarshalString encodes v as a JSON string. MarshalString func(v any) (string, error) // UnmarshalString decodes a JSON string into v. UnmarshalString func(s string, v any) error // NewEncoder creates a streaming JSON encoder writing to w. NewEncoder func(w io.Writer) Encoder // NewDecoder creates a streaming JSON decoder reading from r. NewDecoder func(r io.Reader) Decoder // FreezeImpl creates a concrete API from a Config. Providers replace FreezeImpl to // supply their own frozen configuration; callers should use Freeze instead, which // returns a lazy proxy. FreezeImpl func(config Config) API )
var ( // ValidString reports whether s is valid JSON. ValidString func(s string) bool // Pretouch pre-compiles JSON codecs for the given type. Before a provider is activated, // calls are buffered and drained when DrainPretouchBuffer is called during provider // activation. Pretouch func(t reflect.Type) error )
Functions ¶
func DrainPretouchBuffer ¶
DrainPretouchBuffer returns all types that were buffered by Pretouch calls before provider activation, then clears the buffer. Providers should call this during Activate and pretouch each returned type.
Returns []reflect.Type which contains the buffered types, or nil if empty.
Types ¶
type API ¶
type API interface {
// Marshal serialises v into JSON bytes.
//
// Takes v (any) which is the value to serialise.
//
// Returns []byte which is the JSON-encoded output.
// Returns error when serialisation fails.
Marshal(v any) ([]byte, error)
// Unmarshal deserialises JSON data into v.
//
// Takes data ([]byte) which is the raw JSON input.
// Takes v (any) which is the target to populate.
//
// Returns error when deserialisation fails.
Unmarshal(data []byte, v any) error
// MarshalIndent serialises v into indented JSON bytes.
//
// Takes v (any) which is the value to serialise.
// Takes prefix (string) which is prepended to each line.
// Takes indent (string) which is the indentation unit.
//
// Returns []byte which is the indented JSON output.
// Returns error when serialisation fails.
MarshalIndent(v any, prefix, indent string) ([]byte, error)
// NewEncoder creates a streaming JSON encoder writing to w.
//
// Takes w (io.Writer) which receives the encoded JSON.
//
// Returns Encoder which wraps the underlying provider.
NewEncoder(w io.Writer) Encoder
// NewDecoder creates a streaming JSON decoder reading from r.
//
// Takes r (io.Reader) which supplies the raw JSON.
//
// Returns Decoder which wraps the underlying provider.
NewDecoder(r io.Reader) Decoder
}
API is a frozen JSON configuration providing encode and decode operations.
func Freeze ¶
Freeze creates a frozen API from a Config. The returned API resolves lazily on first method call, picking up whichever provider is active at that time; this allows frozen configs created in init() to benefit from providers activated later during bootstrap.
Takes config (Config) which specifies the encoding behaviour.
Returns API which lazily resolves to the active provider on first use.
type Config ¶
type Config struct {
// CopyString copies decoded strings instead of referencing the source buffer, preventing
// memory issues when decoded objects outlive the original JSON data. Sonic-only.
CopyString bool
// UseInt64 decodes JSON numbers as int64 when possible. Sonic-only.
UseInt64 bool
// EscapeHTML escapes HTML characters in JSON strings.
EscapeHTML bool
// SortMapKeys produces deterministic output by sorting map keys.
SortMapKeys bool
}
Config describes JSON encoding behaviour. When the sonic provider is active, all fields are honoured; with the stdlib fallback, only EscapeHTML is respected and the remaining fields are silently ignored.
type Decoder ¶
type Decoder interface {
// Decode reads the next JSON value from the underlying stream into v.
//
// Takes v (any) which is a pointer to the destination value.
//
// Returns error when the stream cannot be read or the value cannot be parsed.
Decode(v any) error
}
Decoder reads JSON values from an input stream.
type Encoder ¶
type Encoder interface {
// Encode serialises v as JSON and writes it to the underlying stream.
//
// Takes v (any) which is the value to serialise.
//
// Returns error when the value cannot be serialised or written.
Encode(v any) error
}
Encoder writes JSON values to an output stream.
type Pretoucher ¶
type Pretoucher interface {
// Pretouch compiles the codec for t, or records t until a provider that compiles
// codecs is active.
//
// Takes t (reflect.Type) which is the type to compile for.
//
// Returns error when the codec cannot be compiled.
Pretouch(t reflect.Type) error
}
Pretoucher is implemented by an API that can compile the codec for a type before its first use, as Freeze's result, ConfigStd and ConfigDefault all do. A library given an API through dependency injection checks for it to pretouch the types it decodes.
type Provider ¶
type Provider interface {
// Activate replaces all package-level function and API variables with this provider's
// implementations. It is called once during application bootstrap.
Activate()
}
Provider supplies JSON encoding and decoding operations. Implementations replace the default stdlib functions when activated.