jsonscan

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package jsonscan walks a JSON document in place, reporting where object members sit as offsets into the original bytes. It lets a caller read or replace one value in a large metadata document without decoding the rest of it into Go values, which for an npm packument costs many times the document's size.

It checks structure only as far as it needs to find its way through the document. Callers that copy unvisited bytes into a response validate the whole document first with json.Valid.

Index

Constants

This section is empty.

Variables

View Source
var ErrMalformed = errors.New("malformed JSON")

ErrMalformed is returned when the document is not well-formed JSON.

View Source
var ErrNotObject = errors.New("JSON value is not an object")

ErrNotObject is returned when a value expected to be an object is not.

View Source
var ErrStop = errors.New("stop scanning")

ErrStop is returned by a callback to end a walk early once it has found what it was looking for.

Functions

func EachElement

func EachElement(arr []byte, fn func(element []byte) error) error

EachElement calls fn with each element of the array arr holds, in order, and stops early with fn's error if fn returns one.

func EachMember

func EachMember(obj []byte, fn func(Member) error) error

EachMember calls fn for each member of the object obj holds, in document order. It returns ErrNotObject if obj is not an object, and stops early with fn's error if fn returns one.

func Key

func Key(raw []byte) (string, error)

Key decodes a quoted key as EachMember reports it.

func KeyIs

func KeyIs(raw []byte, name string) bool

KeyIs reports whether a quoted key decodes to name.

func Lookup

func Lookup(doc []byte, path ...string) ([]byte, error)

Lookup follows path through nested objects in doc and returns the value at its end. It returns nil and no error when a key along the path is missing or names something other than an object.

func LookupString

func LookupString(doc []byte, path ...string) (string, bool, error)

LookupString is Lookup for a string value. ok is false when the value is missing or is not a string.

func RewriteMembers

func RewriteMembers(out *bytes.Buffer, obj []byte, rewrite func(out *bytes.Buffer, m Member) (bool, error)) error

RewriteMembers copies the object obj holds to out byte for byte, except that rewrite may write a member's value itself. It returns true when it did; otherwise the original value is copied.

func StringIs

func StringIs(raw []byte, s string) bool

StringIs reports whether raw is a JSON string that decodes to s.

func WriteFilteredObject

func WriteFilteredObject(out *bytes.Buffer, obj []byte, keep func(string) bool, extra [][2]string) error

WriteFilteredObject writes the object obj holds, keeping the members whose decoded key keep accepts, followed by extra string members.

Types

type Member

type Member struct {
	KeyStart, KeyEnd int
	ValStart, ValEnd int
}

Member is one member of a JSON object as offsets into the bytes that were scanned. The key span includes its quotes.

func FindMember

func FindMember(obj []byte, name string) (Member, bool, error)

FindMember returns the member of obj named name. When the key repeats, the last one wins, as it does for JSON.parse and encoding/json.

func (Member) Key

func (m Member) Key(data []byte) []byte

func (Member) Value

func (m Member) Value(data []byte) []byte

Jump to

Keyboard shortcuts

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