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 ¶
- Variables
- func EachElement(arr []byte, fn func(element []byte) error) error
- func EachMember(obj []byte, fn func(Member) error) error
- func Key(raw []byte) (string, error)
- func KeyIs(raw []byte, name string) bool
- func Lookup(doc []byte, path ...string) ([]byte, error)
- func LookupString(doc []byte, path ...string) (string, bool, error)
- func RewriteMembers(out *bytes.Buffer, obj []byte, ...) error
- func StringIs(raw []byte, s string) bool
- func WriteFilteredObject(out *bytes.Buffer, obj []byte, keep func(string) bool, extra [][2]string) error
- type Member
Constants ¶
This section is empty.
Variables ¶
var ErrMalformed = errors.New("malformed JSON")
ErrMalformed is returned when the document is not well-formed JSON.
var ErrNotObject = errors.New("JSON value is not an object")
ErrNotObject is returned when a value expected to be an object is not.
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 ¶
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 ¶
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 Lookup ¶
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 ¶
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.
Types ¶
type Member ¶
Member is one member of a JSON object as offsets into the bytes that were scanned. The key span includes its quotes.
func FindMember ¶
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.