Documentation
¶
Overview ¶
Package json mirrors the standard library's encoding/json helpers while adding two service-focused behaviours:
- stream helpers are context aware, so long reads and writes can be cancelled through the supplied context
- marshal and unmarshal operations automatically use generated fast JSON encoders and decoders when a type exposes those interfaces
The intent is to keep call sites as close as possible to encoding/json while letting services benefit from non-reflective JSON code generation without each caller having to know which implementation a type uses. Examples of the supported fast-path libraries are github.com/mailru/easyjson and github.com/pquerna/ffjson.
Index ¶
- func Marshal(v any) ([]byte, error)
- func MarshalWithContext(ctx context.Context, v any) (content []byte, err error)
- func ToYAML(rawJSON []byte) (yaml []byte, err error)
- func Unmarshal(data []byte, v any) error
- func UnmarshallWithContext(ctx context.Context, data []byte, v any) error
- type Decoder
- type Encoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal encodes a value to a JSON byte slice. It matches encoding/json.Marshal.
If the value implements a generated fast JSON marshaler interface, that non-reflective path is used. Otherwise, it falls back to the regular runtime path available through the supported fast JSON backend. The rationale is to keep one familiar helper for callers while automatically taking a faster serialisation path when generated code exists. Examples of supported generators are github.com/mailru/easyjson and github.com/pquerna/ffjson.
func MarshalWithContext ¶
MarshalWithContext encodes a value to a JSON byte slice using a context-aware Encoder. It follows the same helper shape as Marshal, but routes the write through the package's context-aware streaming helpers.
func Unmarshal ¶
Unmarshal decodes a JSON byte slice into a destination value. It matches encoding/json.Unmarshal.
If the destination implements a generated fast JSON unmarshaler interface, that non-reflective path is used. Otherwise it falls back to the regular runtime path available through the supported fast JSON backend. This lets calling code stay implementation-agnostic while types that opt into generated JSON code get faster deserialisation automatically. Examples of supported generators are github.com/mailru/easyjson and github.com/pquerna/ffjson.
func UnmarshallWithContext ¶
UnmarshallWithContext decodes a JSON byte slice into a destination value using a context-aware Decoder. It follows the same helper shape as Unmarshal, but routes the read through the package's context-aware streaming helpers.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes successive JSON values from a reader. It matches encoding/json.Decoder but wraps the supplied reader so reads obey context cancellation, and it uses generated fast JSON decoders when the target type supports those non-reflective decoding paths. This should not be used by more than one goroutine at a time.
func NewDecoder ¶
NewDecoder creates a Decoder that reads JSON values from r. It matches encoding/json.NewDecoder.
It keeps the familiar Decoder workflow while adding context-aware reads and automatic use of generated fast JSON deserialisers. Examples of supported generators are github.com/mailru/easyjson and github.com/pquerna/ffjson.
func (*Decoder) Decode ¶
Decode reads the next JSON value from the decoder into v. It matches encoding/json.Decoder.Decode.
If v implements a generated fast JSON unmarshaler interface, Decode uses that non-reflective path. Otherwise, it falls back to the regular runtime path available through the supported fast JSON backend. The goal is to preserve a standard-library style API while making the faster implementation an internal detail. Examples of supported generators are github.com/mailru/easyjson and github.com/pquerna/ffjson.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes successive JSON values to a writer. It matches encoding/json.Encoder but wraps the supplied writer so writes obey context cancellation, and it uses generated fast JSON encoders when the value supports those non-reflective encoding paths. It allows encoding many objects to a single writer. This should not be used by more than one goroutine at a time.
func NewEncoder ¶
NewEncoder creates an Encoder that writes JSON values to w. It matches encoding/json.NewEncoder.
It keeps the standard Encoder pattern while adding context-aware writes and automatic use of generated fast JSON serialisers when a type provides them. Examples of supported generators are github.com/mailru/easyjson and github.com/pquerna/ffjson.
func (*Encoder) Encode ¶
Encode writes v as the next JSON value to the encoder's writer. It matches encoding/json.Encoder.Encode.
If v implements a generated fast JSON marshaler interface, Encode uses that non-reflective path. Otherwise, it falls back to the regular runtime path available through the supported fast JSON backend. This preserves a familiar API for callers while making fast serialisers an internal optimisation instead of a caller concern. Examples of supported generators are github.com/mailru/easyjson and github.com/pquerna/ffjson.