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
- Variables
- func Decode(payload []byte, target any, options DecodeOptions) error
- func DecodeReader(reader io.Reader, target any, options DecodeOptions) error
- func Encode(value any, options EncodeOptions) ([]byte, error)
- func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error
- func Normalize(payload []byte, options NormalizeOptions) ([]byte, error)
- func Validate(payload []byte, options DecodeOptions) error
- func ValidateReader(reader io.Reader, options DecodeOptions) error
- type DecodeOptions
- type EncodeOptions
- type NormalizeOptions
Constants ¶
const DefaultMaxBytes int64 = 1 << 20
DefaultMaxBytes is the default maximum payload size accepted by decoders.
Variables ¶
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 by default decodes exactly one UTF-8 JSON value into a non-nil pointer. Untyped numbers follow encoding/json float64 mapping unless UseNumber keeps their exact text. Duplicate rejection, structural budgets and invalid UTF-8 replacement are 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 by default decodes exactly one UTF-8 JSON value into a non-nil pointer. It never closes reader. Duplicate-name and structural-budget 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 and budget 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.
func Validate ¶ added in v3.2.0
func Validate(payload []byte, options DecodeOptions) error
Validate checks that payload is exactly one UTF-8 JSON value within MaxBytes and the structural options, without decoding into a target. It applies DisallowDuplicateNames, MaxNestedLevels, MaxValues, MaxObjectMembers, MaxNumberBytes and ReplaceInvalidUTF8; DisallowUnknownFields and UseNumber only affect target decoding and are ignored. Numbers outside the float64 range are valid JSON and are accepted.
func ValidateReader ¶ added in v3.2.0
func ValidateReader(reader io.Reader, options DecodeOptions) error
ValidateReader buffers borrowed input through EOF or a size refusal and applies Validate. It never closes reader.
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
// MaxNestedLevels caps nested arrays and objects before target decoding; a
// root array or object is level one. Zero retains encoding/json's own 10,000
// level limit; negative values and values above 10,000 fail. Refusals match
// wire.ErrSizeLimit.
MaxNestedLevels int
// ReplaceInvalidUTF8 admits invalid UTF-8 inside strings and replaces each
// invalid byte with U+FFFD in decoded strings and names, as encoding/json
// does, instead of rejecting the payload. json.RawMessage and json.Unmarshaler
// targets receive the original invalid bytes. Invalid bytes outside strings
// remain malformed JSON. Use it only for third-party input whose producer
// cannot be held to RFC 8259.
ReplaceInvalidUTF8 bool
// MaxValues caps every JSON value in the payload, counting each object,
// array, string, number, boolean and null but not object names, before
// target decoding. Zero disables the cap; negative values fail. Refusals
// match wire.ErrSizeLimit.
MaxValues int
// MaxObjectMembers caps the members of each object, not the payload total,
// before target decoding. Zero disables the cap; negative values fail.
// Refusals match wire.ErrSizeLimit.
MaxObjectMembers int
// MaxNumberBytes caps the text of each number token before target decoding.
// Zero disables the cap; negative values fail. Refusals match
// wire.ErrSizeLimit.
MaxNumberBytes int
// UseNumber decodes numbers in interface-typed destinations, such as any or
// map[string]any, as json.Number with their exact text instead of float64.
// Typed numeric destinations and json.Unmarshaler implementations, including
// their own inner decoding, are unaffected.
UseNumber 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.