jsonptr

package
v0.0.0-dev.5 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package jsonptr implements RFC 6901 JSON Pointers over raw JSON bytes and arbitrary Go values.

A Pointer is a slash-separated sequence of reference tokens that identifies a location within a JSON document. The empty pointer "" refers to the root value; "/foo" refers to the value of object member "foo"; "/0" refers to the first element of an array; tokens use "~1" to encode "/" and "~0" to encode "~".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Find

func Find(data []byte, p Pointer, opts ...jsontext.Options) (jsontext.Value, error)

Find returns the JSON value located at p within data. The returned value preserves the original byte representation (whitespace, number form, member order). Non-existent members, missing indices, and pointers that descend into a scalar all return errors. Caller-supplied options are forwarded to jsontext.NewDecoder.

func FindValue

func FindValue(p Pointer, in any, opts ...json.Options) (jsontext.Value, any, error)

FindValue navigates to p within in by walking the Go value via reflection and returns both the JSON form at that location and the live Go value. Any json.Options are forwarded to every Marshal / decoder call the implementation makes — both when producing the returned bytes and when decoding through a json.Marshaler boundary.

Descent rules, in order of precedence:

  • If the current value implements Walker, JSONPointerStep is called for the next token. Identity is preserved.
  • Pointers and interfaces are dereferenced.
  • Maps with string-kinded keys are looked up by token.
  • Slices and arrays are indexed by integer token.
  • Structs are looked up by `json:"name"` tag, falling back to the field name when the tag is absent. Unexported fields are skipped.
  • When the current value implements json.Marshaler or jsontext.MarshalerTo, the remaining tokens are resolved by marshaling the value and calling Find on the bytes. Identity is lost across this boundary; the second return value is then a freshly decoded value: map[string]any for objects, []any for arrays, *big.Rat for every JSON number (callers convert to int / float64 themselves), and the natural Go counterpart for strings, booleans, and null.

The first return is the JSON encoding at the location (for the in-Go path, produced by json.Marshal of the live value).

Types

type Builder

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

Builder constructs a Pointer by appending tokens to an underlying strings.Builder. The zero value is ready to use and produces "" until any token is appended; pass an existing prefix via NewBuilder.

func NewBuilder

func NewBuilder(prefix string) *Builder

NewBuilder returns a Builder seeded with the given prefix (typically a parent Pointer or a free-form path used in error context).

func (*Builder) Index

func (b *Builder) Index(i int) *Builder

Index appends a non-negative integer token (no escaping needed).

func (*Builder) Pointer

func (b *Builder) Pointer() Pointer

Pointer returns the Pointer built so far.

func (*Builder) Raw

func (b *Builder) Raw(s string) *Builder

Raw appends raw bytes to the builder without escaping or a leading slash. Use sparingly — it bypasses RFC 6901 escaping.

func (*Builder) Reset

func (b *Builder) Reset()

Reset discards all accumulated content.

func (*Builder) String

func (b *Builder) String() string

String returns the built path as a plain string.

func (*Builder) Token

func (b *Builder) Token(token string) *Builder

Token appends an RFC 6901 reference token. The token is escaped.

type Pointer

type Pointer string

Pointer is an RFC 6901 JSON Pointer in string form.

The zero value, "", refers to the root of any JSON document. A non-empty Pointer must begin with "/" and is a sequence of "/"-separated reference tokens. Tokens are unescaped per RFC 6901 §4: "~1" -> "/", "~0" -> "~" (decoded in that order).

func (Pointer) Append

func (p Pointer) Append(token string) Pointer

Append returns a new Pointer that descends through token. The token is escaped per RFC 6901.

func (Pointer) Head

func (p Pointer) Head() (token string, rest Pointer, ok bool)

Head splits p into its first token and the remaining Pointer. ok is false when p is the root pointer.

func (Pointer) IsRoot

func (p Pointer) IsRoot() bool

IsRoot reports whether p is the root pointer ("").

func (Pointer) Tokens

func (p Pointer) Tokens() iter.Seq[string]

Tokens yields the sequence of reference tokens that make up p, unescaped per RFC 6901. The root pointer yields nothing.

func (Pointer) Validate

func (p Pointer) Validate() error

Validate reports an error if p is not a syntactically valid RFC 6901 pointer. The empty pointer and any string starting with "/" are valid. Inside a token, "~" must be followed by "0" or "1".

type Walker

type Walker interface {
	FindJSONPtrValue(ptr Pointer, opts ...json.Options) (rest Pointer, value any, err error)
}

Walker lets a type control how FindValue descends into it. The method receives the unconsumed Pointer and returns:

  • rest: the tail the implementation did NOT consume — FindValue continues descending from value with rest. Return "" to indicate "I resolved the whole thing".
  • value: the Go value reached after consuming the prefix of ptr.
  • err: any error.

A type may consume one or several tokens per call. Returning the full input as rest signals "I don't know how to handle this prefix"; an error is the preferred signal.

Walker takes precedence over the json.Marshaler / jsontext.MarshalerTo fallback in FindValue, so types that implement Walker preserve Go identity across descent rather than round-tripping through JSON bytes.

Jump to

Keyboard shortcuts

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