jsonutil

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonical

func Canonical(raw string) string

Canonical returns a key-order-independent, whitespace-normalized form of a JSON value (encoding/json marshals map keys sorted). Non-JSON input is returned unchanged, so unresolved ${...} tokens pass through untouched.

func DeleteJSON

func DeleteJSON(existing []byte, path string) ([]byte, error)

DeleteJSON removes the value at a dotted path, preserving the rest of the document. A missing path is not an error — the delete is already done.

func EnsureArrayElem

func EnsureArrayElem(existing []byte, path, elem string) ([]byte, error)

EnsureArrayElem appends string elem to the array at path if absent. An existing non-array value at path is an error: the ".-1" append would turn it into `{"-1": elem}` — silent corruption.

func EscapePath

func EscapePath(segment string) string

EscapePath escapes one config-supplied name for use as a single segment of a gjson/sjson dotted path, so the segment addresses a literal key instead of being interpreted as path syntax. gjson and sjson honor the same backslash escaping, so escaped reads and writes address the same key.

func GetJSON

func GetJSON(existing []byte, path string) (string, bool)

GetJSON returns the raw JSON of the value at path and whether it exists.

func ObjectRoot

func ObjectRoot(doc []byte) error

ObjectRoot verifies the document's root value is a JSON object. Managed configs are always objects; a keyed write into an array or scalar root would silently corrupt it, so anything else is rejected up front.

func RemoveArrayElem

func RemoveArrayElem(existing []byte, path, elem string) ([]byte, error)

RemoveArrayElem deletes every occurrence of string elem from the array at path; an absent elem (or array) leaves the document untouched. An existing non-array value is an error, matching EnsureArrayElem — a silent no-op would leave a corrupt value in place unnoticed.

func SetJSON

func SetJSON(existing []byte, path string, value any) ([]byte, error)

SetJSON sets a dotted path to value, preserving the rest of the document.

func Standardize

func Standardize(jsonc []byte) ([]byte, error)

Standardize converts JSONC to plain JSON (dropping comments). Empty -> "{}".

Types

This section is empty.

Jump to

Keyboard shortcuts

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