strictjson

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package strictjson reads native Ceph JSON without the silent choices of encoding/json. A document is exactly one JSON value: no object repeats a key at any depth, nesting stays within MaxDepth and nothing follows the value. When the document is decoded into a typed value, a key that differs from a known field's name only by case is refused instead of adopted by the decoder's case-insensitive matching, and unrelated unknown keys are allowed.

Errors are the sentinels below and never copy native data, which can carry credentials, into their text.

Index

Constants

View Source
const MaxDepth = 64

MaxDepth bounds the nesting of objects and arrays. Native Ceph output nests far less deeply; the bound keeps a hostile document from growing the stack.

Variables

View Source
var (
	// ErrSize reports an empty document or one larger than Options.MaxBytes.
	ErrSize = errors.New("JSON document is empty or too large")
	// ErrMalformed reports invalid JSON, or a value of the wrong type for the
	// field it is decoded into.
	ErrMalformed = errors.New("malformed JSON document")
	// ErrDuplicateKey reports an object that repeats a key.
	ErrDuplicateKey = errors.New("duplicate JSON object key")
	// ErrNoncanonicalKey reports a key that matches a known field only when
	// case is ignored.
	ErrNoncanonicalKey = errors.New("noncanonical JSON object key")
	// ErrNull reports null for a known field when Options.RejectNull is set.
	ErrNull = errors.New("null JSON value for a known field")
	// ErrDepth reports nesting beyond MaxDepth.
	ErrDepth = errors.New("JSON nesting exceeds limit")
	// ErrTrailing reports data after the first JSON value.
	ErrTrailing = errors.New("trailing data after the JSON value")
)

Functions

func Check

func Check(data []byte, opts Options) error

Check validates the shape of data without decoding it.

func Decode

func Decode(data []byte, value any, opts Options) error

Decode validates data against the shape of value, then decodes it into value.

Types

type Options

type Options struct {
	// MaxBytes refuses a larger document. Zero sets no size limit; an empty
	// document is refused either way.
	MaxBytes int
	// RejectNull refuses null for any field whose type is known from the
	// decoded value, so a present but null field cannot pass as absent.
	RejectNull bool
	// UseNumber decodes numbers in interface values as json.Number.
	UseNumber bool
}

Options holds the rules that legitimately differ between callers.

Jump to

Keyboard shortcuts

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