jsonx

package
v0.2.22 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jsonx builds JSON documents with the exact byte-level semantics of a chain of sjson v1.0.4 Set/SetRaw calls — new keys PREPEND (so sequential inserts land in reverse order), existing values replace in place, "-1" appends to arrays, numeric indexes null-pad — but serialized ONCE instead of re-copying the whole document per call. Envelope byte-identity (golden tests, traces) depends on these semantics; sjson is pinned at v1.0.4 in go.mod for the same reason.

Error semantics also mirror the ubiquitous `doc, _ = sjson.Set(...)` call shape: an invalid path (unescaped *?#, empty path) or an unmarshalable value poisons the document to "" and later Sets rebuild from empty, exactly like the chained form.

Limitations (enforced by convention, exercised by the equivalence fuzz): SetRaw values must be valid JSON — descending a path *through* an invalid raw blob is undefined; and a Builder is not safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStringify

func AppendStringify(buf []byte, s string) []byte

func MustMarshalString

func MustMarshalString(s string) bool

MustMarshalString / AppendStringify replicate sjson v1.0.4's string encoder (MIT, github.com/tidwall/sjson): fast-quote when every byte is plain printable ASCII, else encoding/json (which HTML-escapes).

func SetMany

func SetMany(doc string, sets []PathVal) string

SetMany applies the sets to doc with the exact semantics of the sequential `doc, _ = sjson.Set(doc, path, v)` chain. When every target path already exists (the steady-state for periodic re-stamps like budget sync), it resolves all spans in one pass and splices one output buffer instead of copying the whole doc per set; otherwise it falls back to the sequential chain, so byte-identity holds by construction.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder accumulates Set/SetRaw operations into a node tree and serializes once. The zero value / New() is the empty document "" (matching a chain that starts from ""); NewObject() matches a chain that starts from "{}" — the two differ when the first path segment is numeric (sjson turns an empty doc into an array, but inserts a string key into "{}").

func New

func New() *Builder

func NewArray

func NewArray() *Builder

NewArray starts from "[]" (the `sjson.Set("[]", "-1", ...)` append seed used by list builders).

func NewObject

func NewObject() *Builder

func (*Builder) Set

func (b *Builder) Set(path string, v any)

Set encodes v exactly as sjson.Set would (strings and []byte via appendStringify, sized ints via FormatInt, float32 via the same widen-then-'f' quirk, everything else via encoding/json) and applies it at path.

func (*Builder) SetRaw

func (b *Builder) SetRaw(path, raw string)

SetRaw splices raw (which must be valid JSON) at path verbatim.

func (*Builder) String

func (b *Builder) String() string

String serializes the document. An empty (or poisoned) Builder returns "", matching a zero-op / errored sjson chain.

type PathVal

type PathVal struct {
	Path string
	Val  any
}

PathVal is one Set for SetMany.

Jump to

Keyboard shortcuts

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