jsonstrict

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 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 jsonstrict holds the strict-JSON checks encoding/json does not make: the one place a repeated object key is refused rather than read last-wins, shared by every reader whose input is a trust boundary (the rules overlay, the release-gate receipts, the layered configuration files, the reading presets).

Index

Constants

This section is empty.

Variables

View Source
var ErrTrailing = errors.New("content after the one JSON document")

ErrTrailing is Decode's answer for data that carries anything after its one JSON document: a second document, or bytes that are not JSON at all.

Functions

func Decode

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

Decode is the strict decode a trust-boundary reader makes: data is exactly one JSON document, no object in it repeats a key (a *DuplicateKeyError, judged as NoDuplicateKeys judges it), no field is one v's type does not name (encoding/json's unknown-field error), and nothing follows the document (ErrTrailing). Whitespace after the document is not content.

func NoDuplicateKeys

func NoDuplicateKeys(data []byte) error

NoDuplicateKeys walks the JSON token stream and refuses any object that carries a repeated key at any nesting level, answering a *DuplicateKeyError. It runs before the unmarshal precisely because encoding/json would otherwise collapse the duplicate silently.

"Repeated" is judged the way encoding/json matches a key to a struct field: after unescaping, and case-insensitively under Unicode simple folding (its foldName, which agrees with strings.EqualFold), so "VerificationResult", "VERIFICATIONRESULT" and "\u0056erificationResult" all repeat "verificationResult". The checker does not know the target type, so it folds in every object, map-shaped ones included: a map encoding/json would keep two entries in ("PII" and "pii") is refused too, because one name in two spellings is illegible to the reader the refusal protects.

A malformed document answers nil and is left for the unmarshal to report with its position. The stdlib decoder enforces a max nesting depth, so no separate depth guard is needed.

Types

type DuplicateKeyError

type DuplicateKeyError struct {
	Path  []string
	Key   string
	First string
}

DuplicateKeyError names one object key a document repeats. Key is the later spelling and First the earlier one it collides with: equal for an exact repeat, different for a case twin encoding/json binds to the same field. Path is the chain of enclosing object keys from the root, "[]" standing for an array element. Callers that phrase their own refusal read the fields; Error is the general form.

func (*DuplicateKeyError) Error

func (e *DuplicateKeyError) Error() string

Jump to

Keyboard shortcuts

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