jsonstrict

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package jsonstrict decodes JSON files into typed structs with DisallowUnknownFields, with a one-time recovery path that strips unknown keys into a sibling backup on first encounter.

This is the "breaking-change mitigation" described in docs/robustness-audit.md §5 for audit item #4. Strictness catches typos in hand-edited state files (config.json, sessions.json) from then on. On the first strict load that encounters unknowns, we do not hard-fail: we copy the original bytes to a sibling ".bak.unknowns.<unix-nano>", emit a WARN-level slog line naming the dropped keys, rewrite the file without them, and re-decode strictly. The mitigation is self-limiting — after the rewrite, subsequent loads see no unknowns and strict decode succeeds outright.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(path string, v any) error

Decode reads path and strictly decodes into v (which must be a pointer to a struct). Behaviour:

  • path missing → returns the underlying os.IsNotExist error so the caller can decide what absent means in context. v is untouched.
  • file valid + all keys known → v populated, nil returned.
  • file valid + unknown keys → strip-to-.bak mitigation: original bytes saved to "<path>.bak.unknowns.<unix-nano>", file rewritten without the stripped keys, WARN slog line names each dropped key, v populated with the cleaned result, nil returned.
  • malformed JSON or type mismatch → error returned, file untouched, v left in whatever partial state the decoder produced.

The known-key set is derived from the `json` struct tags on v. Fields without a tag fall back to their Go name, matching how encoding/json's strict decoder lookups behave.

Types

This section is empty.

Jump to

Keyboard shortcuts

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