jsonwire

package
v3.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 11 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonwire provides bounded, strict JSON helpers for interoperability boundaries where encoding/json's low-level defaults need explicit policy.

Use Decode/Encode for RFC 8259 text; Normalize is presentation normalization, not a signing canonicalization profile. Reader calls buffer a complete bounded unit; they do not stream values or close the reader. Inputs/callbacks remain caller-owned and targets may be partially mutated on error. No context API, transport, schema, cgo or background workers are owned here.

Index

Constants

View Source
const DefaultMaxBytes int64 = 1 << 20

DefaultMaxBytes is the default maximum payload size accepted by decoders.

Variables

View Source
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")

ErrPayloadTooLarge identifies payloads that exceed the configured limit.

Functions

func Decode

func Decode(payload []byte, target any, options DecodeOptions) error

Decode borrows payload until return and decodes exactly one UTF-8 JSON value into a non-nil pointer. Untyped numbers follow encoding/json float64 mapping; choose typed numeric destinations when that representation is insufficient. Duplicate rejection is opt-in and failed targets are indeterminate.

func DecodeReader

func DecodeReader(reader io.Reader, target any, options DecodeOptions) error

DecodeReader buffers borrowed input through EOF or a size refusal and decodes exactly one UTF-8 JSON value into a non-nil pointer. It never closes reader. Duplicate preflight, when enabled, precedes assignment; other errors, including trailing values, can leave target mutated. Blocking/custom codec work is not cancellable through this API. Size refusals match wire.ErrSizeLimit.

func Encode

func Encode(value any, options EncodeOptions) ([]byte, error)

Encode borrows a stable acyclic value, orders built-in map keys using encoding/json and returns owned bytes without the encoder newline. MaxBytes bounds final output, not graph preflight or custom marshaler work. Errors return no payload; caller marshalers can change determinism and must terminate.

func EncodeWriter

func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error

EncodeWriter completes serialization before touching a borrowed writer, which it never closes. Encoding failure writes nothing; a later write error can leave a prefix visible and matches wire.ErrWrite. It is not atomic publication.

func Normalize

func Normalize(payload []byte, options NormalizeOptions) ([]byte, error)

Normalize strips a UTF-8 BOM and insignificant whitespace, validates that the payload contains exactly one value, and emits compact, key-ordered JSON. Number lexemes are retained so normalization does not silently change vendor precision or representation. Duplicate names follow encoding/json replacement behavior; no RFC 8785/signing profile is claimed. MaxBytes only admits input: re-encoding uses the default 1 MiB output limit, even when input admission is raised. Source bytes are not mutated; failure returns no payload.

Types

type DecodeOptions

type DecodeOptions struct {
	// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// DisallowUnknownFields rejects unknown object fields when decoding into structs.
	DisallowUnknownFields bool
	// DisallowDuplicateNames rejects recursively repeated object names before target decoding;
	// default retains encoding/json replacement/merge behavior.
	DisallowDuplicateNames bool
}

DecodeOptions controls JSON decoding behavior.

type EncodeOptions

type EncodeOptions struct {
	// MaxBytes caps returned encoded bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// Indent selects encoding/json indentation; empty produces compact output.
	Indent string
	// DisableHTMLEscaping disables escaping of HTML-sensitive characters; it does not make
	// output safe for HTML embedding.
	DisableHTMLEscaping bool
}

EncodeOptions controls JSON encoding behavior.

type NormalizeOptions

type NormalizeOptions struct {
	// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
}

NormalizeOptions controls JSON normalization behavior.

Jump to

Keyboard shortcuts

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