json
v0.0.0-20261117200652-e144bc0a713c
Documentation
pkg.go.dev ↗Package json provides fast JSON encoding and decoding functionality.
On supported platforms; Linux, Darwin, or Windows on amd64, or on arm64 with Go >= 1.20 and <= 1.25, the package uses the high-performance sonic library. On any other systems, it gracefully falls back to using the [go-json] implementation.
This package acts as a wrapper around the underlying JSON APIs, offering standard operations such as marshaling, unmarshalling, and working with JSON encoders/decoders. It maintains compatibility with the standard encoding/json interfaces while delivering improved performance when possible.
Additionally, it defines the customary Marshaler and Unmarshaler interfaces to facilitate custom JSON encoding and decoding implementations.
Functions
Marshal
func Marshal(v any) ([]byte, error)
Marshal encodes a Go value as JSON using the current API config.
MarshalIndent
func MarshalIndent(v any, prefix, indent string) ([]byte, error)
MarshalIndent encodes a Go value as indented JSON using the current API config.
SetConfig
func SetConfig(config *sonic.Config)
SetConfig sets the configuration for the JSON package.
Unmarshal
func Unmarshal(data []byte, v any) error
Unmarshal decodes a JSON payload into the provided destination using the current API config.
Types
Decoder
type Decoder = sonic.Decoder
Decoder is a JSON decoder.
NewDecoder
func NewDecoder(r io.Reader) Decoder
NewDecoder creates a streaming decoder using the current API config.
Encoder
type Encoder = sonic.Encoder
Encoder is a JSON encoder.
NewEncoder
func NewEncoder(w io.Writer) Encoder
NewEncoder creates a streaming encoder using the current API config.
JSONCodec
type JSONCodec interface {
Marshaler
Unmarshaler
}
JSONCodec is the interface implemented by types that can marshal and unmarshal themselves into valid JSON.
Marshaler
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
Marshaler is the interface implemented by types that can marshal themselves into valid JSON.
Message
type Message []byte
Message is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding or precompute a JSON encoding.
Copied from: https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/encoding/json/stream.go;l=256-276
Message.MarshalJSON
func (m Message) MarshalJSON() ([]byte, error)
MarshalJSON returns m as the JSON encoding of m.
Copied from: https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/encoding/json/stream.go;l=256-276
Message.UnmarshalJSON
func (m *Message) UnmarshalJSON(data []byte) error
UnmarshalJSON sets *m to a copy of data.
Copied from: https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/encoding/json/stream.go;l=256-276
Unmarshaler
type Unmarshaler interface {
UnmarshalJSON([]byte) error
}
Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning.
By convention, to approximate the behavior of Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.