json

package
v1.160.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (

	// JSONExtensions is the list of file extensions that are considered JSON files.
	JSONExtensions = []string{".json"}
)

Functions

func IsJSON added in v1.159.0

func IsJSON(extension string) bool

IsJSON returns true if the extension is a JSON file

func Marshal

func Marshal(v any) ([]byte, error)

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

func MarshalWithContext(ctx context.Context, v any) (content []byte, err error)

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 ToYAML

func ToYAML(rawJSON []byte) (yaml []byte, err error)

ToYAML converts JSON data to YAML.

func Unmarshal

func Unmarshal(data []byte, v any) error

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

func UnmarshallWithContext(ctx context.Context, data []byte, v any) error

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

func NewDecoder(ctx context.Context, r io.Reader) *Decoder

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

func (d *Decoder) Decode(v any) error

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

func NewEncoder(ctx context.Context, w io.Writer) *Encoder

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

func (e *Encoder) Encode(v any) error

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.

Directories

Path Synopsis
jsontest

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL