Documentation
¶
Overview ¶
SYS-REQ-015, SYS-REQ-058, SYS-REQ-059, SYS-REQ-064: integer parsing internals
SYS-REQ-001, SYS-REQ-013, SYS-REQ-014: unsafe build-tag byte utilities
SYS-REQ-014, SYS-REQ-060, SYS-REQ-061, SYS-REQ-062, SYS-REQ-063: string escape and Unicode handling
Index ¶
- Constants
- Variables
- func Append(data []byte, value []byte, keys ...string) ([]byte, error)
- func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), ...) (offset int, err error)
- func ArrayEachErr(data []byte, ...) (count int, err error)
- func ArrayEachWildcard(data []byte, ...) (int, error)
- func Delete(data []byte, keys ...string) []byte
- func DeleteFound(data []byte, keys ...string) (result []byte, found bool)
- func EachArray(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), ...)
- func EachArrayErr(data []byte, ...) (int, error)
- func EachArrayWildcard(data []byte, ...) (int, error)
- func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int
- func EachKeyErr(data []byte, cb func(idx int, value []byte, vt ValueType, err error) error, ...) error
- func EachKeyWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, err error), ...) error
- func EachObject(data []byte, ...) error
- func Escape(in string) []byte
- func FuzzDelete(data []byte) int
- func FuzzEachKey(data []byte) int
- func FuzzGetBoolean(data []byte) int
- func FuzzGetFloat(data []byte) int
- func FuzzGetInt(data []byte) int
- func FuzzGetString(data []byte) int
- func FuzzGetUnsafeString(data []byte) int
- func FuzzObjectEach(data []byte) int
- func FuzzParseBool(data []byte) int
- func FuzzParseFloat(data []byte) int
- func FuzzParseInt(data []byte) int
- func FuzzParseString(data []byte) int
- func FuzzSet(data []byte) int
- func FuzzTokenStart(data []byte) int
- func GetArrayLen(data []byte, keys ...string) (int, error)
- func GetBoolean(data []byte, keys ...string) (val bool, err error)
- func GetFloat(data []byte, keys ...string) (val float64, err error)
- func GetInt(data []byte, keys ...string) (val int64, err error)
- func GetObjectLen(data []byte, keys ...string) (int, error)
- func GetString(data []byte, keys ...string) (val string, err error)
- func GetUint64(data []byte, keys ...string) (uint64, error)
- func GetUnsafeString(data []byte, keys ...string) (val string, err error)
- func ObjectEach(data []byte, ...) (err error)
- func ParseBoolean(b []byte) (bool, error)
- func ParseFloat(b []byte) (float64, error)
- func ParseInt(b []byte) (int64, error)
- func ParsePath(jsonPath string) ([]string, error)
- func ParseString(b []byte) (string, error)
- func Set(data []byte, setValue []byte, keys ...string) (value []byte, err error)
- func SetString(data []byte, val string, keys ...string) ([]byte, error)
- func SetWildcard(data []byte, setValue []byte, keys ...string) ([]byte, error)
- func StringToBytes(s string) []byte
- func Unescape(in, out []byte) ([]byte, error)
- func WriteToBuffer(buffer []byte, str string) int
- type CompiledPath
- func (c CompiledPath) ArrayEach(data []byte, cb func([]byte, ValueType, int, error)) (int, error)
- func (c CompiledPath) Delete(data []byte) []byte
- func (c CompiledPath) EachKey(data []byte, cb func(int, []byte, ValueType, error)) error
- func (c CompiledPath) Get(data []byte) ([]byte, ValueType, int, error)
- func (c CompiledPath) GetInt(data []byte) (int64, error)
- func (c CompiledPath) GetString(data []byte) (string, error)
- func (c CompiledPath) Parts() []string
- func (c CompiledPath) Set(data []byte, value []byte) ([]byte, error)
- type Config
- func (c Config) ArrayEach(data []byte, cb func([]byte, ValueType, int, error), keys ...string) (int, error)
- func (c Config) Delete(data []byte, keys ...string) []byte
- func (c Config) Get(data []byte, keys ...string) ([]byte, ValueType, int, error)
- func (c Config) GetString(data []byte, keys ...string) (string, error)
- func (c Config) ObjectEach(data []byte, cb func([]byte, []byte, ValueType, int) error, keys ...string) error
- func (c Config) Set(data []byte, value []byte, keys ...string) ([]byte, error)
- type ReaderParser
- type ValueType
Constants ¶
const ( NotExist = ValueType(iota) String Number Object Array Boolean Null Unknown )
Variables ¶
var ( KeyPathNotFoundError = errors.New("Key path not found") UnknownValueTypeError = errors.New("Unknown value type") MalformedJsonError = errors.New("Malformed JSON error") MalformedStringError = errors.New("Value is string, but can't find closing '\"' symbol") MalformedArrayError = errors.New("Value is array, but can't find closing ']' symbol") MalformedObjectError = errors.New("Value looks like object, but can't find closing '}' symbol") MalformedValueError = errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol") OverflowIntegerError = errors.New("Value is number, but overflowed while parsing") MalformedStringEscapeError = errors.New("Encountered an invalid escape sequence in a string") NullValueError = errors.New("Value is null") )
Errors
var DefaultConfig = Config{}
DefaultConfig is the strict parser configuration used by package-level functions. SYS-REQ-115
var Lenient = Config{ AllowSingleQuotes: true, AllowUnknownEscapes: true, }
Lenient accepts single-quoted strings and unknown escape sequences. SYS-REQ-115
Functions ¶
func Append ¶ added in v1.6.0
Append adds value to the end of the JSON array addressed by keys.
When keys is empty, Append addresses the top-level value. If a keyed path does not exist, Append creates it as a single-element array using Set's auto-vivification behavior.
SYS-REQ-009, SYS-REQ-110
func ArrayEach ¶
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (offset int, err error)
SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083 ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
func ArrayEachErr ¶ added in v1.4.0
func ArrayEachErr(data []byte, cb func(value []byte, dataType ValueType, offset int, err error) error, keys ...string) (count int, err error)
ArrayEachErr is used when iterating arrays and allows the callback to stop iteration by returning an error. io.EOF stops iteration without returning an error. The returned count includes the element whose callback stopped the iteration. SYS-REQ-004
func ArrayEachWildcard ¶ added in v1.4.0
func ArrayEachWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, offset int, err error) error, keys ...string) (int, error)
ArrayEachWildcard iterates over an array addressed by keys. A terminal [*] component is accepted as an explicit request for all elements; without it, the function behaves like ArrayEachErr. SYS-REQ-113
func Delete ¶
Del - Receives existing data structure, path to delete.
Returns: `data` - return modified data
SYS-REQ-010, SYS-REQ-033, SYS-REQ-034, SYS-REQ-035, SYS-REQ-048, SYS-REQ-049, SYS-REQ-050, SYS-REQ-056
func DeleteFound ¶ added in v1.4.0
DeleteFound removes the value addressed by keys and reports whether the value was found and removed. When found is false, result is data unchanged. SYS-REQ-010
func EachArray ¶ added in v1.5.0
func EachArray(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string)
SYS-REQ-006, SYS-REQ-028, SYS-REQ-029, SYS-REQ-052, SYS-REQ-053, SYS-REQ-055, SYS-REQ-083 EachArray is the canonical name; ArrayEach is kept for backward compatibility.
func EachArrayErr ¶ added in v1.5.0
func EachArrayErr(data []byte, cb func(value []byte, dataType ValueType, offset int, err error) error, keys ...string) (int, error)
SYS-REQ-004 EachArrayErr is the canonical name; ArrayEachErr is kept for backward compatibility.
func EachArrayWildcard ¶ added in v1.5.0
func EachArrayWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, offset int, err error) error, keys ...string) (int, error)
SYS-REQ-113 EachArrayWildcard is the canonical name; ArrayEachWildcard is kept for backward compatibility.
func EachKeyErr ¶ added in v1.4.0
func EachKeyErr(data []byte, cb func(idx int, value []byte, vt ValueType, err error) error, paths ...[]string) error
EachKeyErr finds the requested paths and allows the callback to stop iteration by returning an error. io.EOF stops iteration gracefully. SYS-REQ-008
func EachKeyWildcard ¶ added in v1.4.0
func EachKeyWildcard(data []byte, cb func(idx int, value []byte, vt ValueType, err error), path ...string) error
EachKeyWildcard resolves a path that may contain [*] components. A wildcard requires an array at that position and fans out over its elements before resolving the rest of the path. Multiple wildcards produce one callback for every matching combination, in document order.
idx is the zero-based order in which matches are reported.
SYS-REQ-113
func EachObject ¶ added in v1.5.0
func EachObject(data []byte, cb func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) error
SYS-REQ-007, SYS-REQ-030, SYS-REQ-031, SYS-REQ-032, SYS-REQ-054, SYS-REQ-084 EachObject is the canonical name; ObjectEach is kept for backward compatibility.
func Escape ¶ added in v1.4.0
Escape returns in as a JSON string literal, including the surrounding quotation marks. SYS-REQ-014
func GetArrayLen ¶ added in v1.4.0
GetArrayLen returns the number of elements in the addressed JSON array. It scans the array without invoking a callback. SYS-REQ-112
func GetBoolean ¶
GetBoolean returns the value retrieved by `Get`, cast to a bool if possible. The offset is the same as in `Get`. If key data type do not match, it will return error. SYS-REQ-005, SYS-REQ-079
func GetFloat ¶
GetFloat returns the value retrieved by `Get`, cast to a float64 if possible. The offset is the same as in `Get`. If key data type do not match, it will return an error. SYS-REQ-004
func GetInt ¶
GetInt returns the value retrieved by `Get`, cast to a int64 if possible. If key data type do not match, it will return an error. SYS-REQ-003, SYS-REQ-075, SYS-REQ-076, SYS-REQ-077, SYS-REQ-078
func GetObjectLen ¶ added in v1.4.0
GetObjectLen returns the number of key-value pairs in the addressed JSON object. It scans the object without invoking a callback. SYS-REQ-112
func GetString ¶
SYS-REQ-002, SYS-REQ-071, SYS-REQ-072, SYS-REQ-073, SYS-REQ-074 GetString returns the value retrieved by `Get`, cast to a string if possible, trying to properly handle escape and utf8 symbols If key data type do not match, it will return an error.
func GetUint64 ¶ added in v1.4.0
GetUint64 returns the value retrieved by internalGet, cast to a uint64 if possible. Negative values and malformed integers return MalformedValueError; values larger than uint64 return OverflowIntegerError. SYS-REQ-003
func GetUnsafeString ¶
SYS-REQ-011, SYS-REQ-080, SYS-REQ-081, SYS-REQ-082 GetUnsafeString returns the value retrieved by `Get`, use creates string without memory allocation by mapping string to slice memory. It does not handle escape symbols.
func ObjectEach ¶
func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error)
SYS-REQ-007, SYS-REQ-030, SYS-REQ-031, SYS-REQ-032, SYS-REQ-054, SYS-REQ-084 ObjectEach iterates over the key-value pairs of a JSON object, invoking a given callback for each such entry
func ParseBoolean ¶
ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness) SYS-REQ-012, SYS-REQ-036, SYS-REQ-057, SYS-REQ-066
func ParseFloat ¶
ParseNumber parses a Number ValueType into a Go float64 SYS-REQ-013, SYS-REQ-037, SYS-REQ-065
func ParseInt ¶
ParseInt parses a Number ValueType into a Go int64 SYS-REQ-015, SYS-REQ-039, SYS-REQ-040, SYS-REQ-058, SYS-REQ-059, SYS-REQ-064
func ParsePath ¶ added in v1.4.0
ParsePath converts a JSONPath-style path into the path components accepted by Get, Set, Delete, ArrayEach, and EachKey. SYS-REQ-114
func ParseString ¶
ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string) SYS-REQ-014, SYS-REQ-038, SYS-REQ-060, SYS-REQ-063, SYS-REQ-067
func Set ¶
Set - Receives existing data structure, path to set, and data to set at that key.
Returns: `value` - modified byte array `err` - On any parsing error
SYS-REQ-009, SYS-REQ-051, SYS-REQ-068, SYS-REQ-069, SYS-REQ-070, SYS-REQ-110
func SetString ¶ added in v1.4.0
SetString replaces the value at keys with val encoded as a JSON string. SYS-REQ-009
func SetWildcard ¶ added in v1.4.0
SetWildcard applies Set to every concrete path matched by [*]. Wildcards may appear at any array position and may be repeated. If a wildcard matches an empty array, data is returned unchanged.
SYS-REQ-113
func StringToBytes ¶
func Unescape ¶
unescape unescapes the string contained in 'in' and returns it as a slice. If 'in' contains no escaped characters:
Returns 'in'.
Else, if 'out' is of sufficient capacity (guaranteed if cap(out) >= len(in)):
'out' is used to build the unescaped string and is returned with no extra allocation
Else:
A new slice is allocated and returned.
Types ¶
type CompiledPath ¶ added in v1.4.0
type CompiledPath struct {
// contains filtered or unexported fields
}
CompiledPath stores a parsed path for repeated operations. SYS-REQ-114
func CompilePath ¶ added in v1.4.0
func CompilePath(jsonPath string) (CompiledPath, error)
CompilePath parses jsonPath once for reuse. SYS-REQ-114
func (CompiledPath) ArrayEach ¶ added in v1.4.0
ArrayEach iterates over the array at the compiled path. SYS-REQ-114
func (CompiledPath) Delete ¶ added in v1.4.0
func (c CompiledPath) Delete(data []byte) []byte
Delete removes the value at the compiled path. SYS-REQ-114
func (CompiledPath) EachKey ¶ added in v1.4.0
EachKey resolves the compiled path using EachKey. SYS-REQ-114
func (CompiledPath) Get ¶ added in v1.4.0
Get resolves the compiled path in data. Verifies: SYS-REQ-001 (Get) SYS-REQ-114
func (CompiledPath) GetInt ¶ added in v1.4.0
func (c CompiledPath) GetInt(data []byte) (int64, error)
GetInt resolves the compiled path and returns its integer value. SYS-REQ-114
func (CompiledPath) GetString ¶ added in v1.4.0
func (c CompiledPath) GetString(data []byte) (string, error)
GetString resolves the compiled path and returns its string value. SYS-REQ-114
func (CompiledPath) Parts ¶ added in v1.4.0
func (c CompiledPath) Parts() []string
Parts returns a copy of the compiled path components. SYS-REQ-114
type Config ¶ added in v1.5.0
type Config struct {
AllowSingleQuotes bool // #160: accept 'key':'value' alongside "key":"value"
AllowUnknownEscapes bool // #115: accept unknown escape sequences instead of erroring
MaxBufferSize int // ReaderParser sliding-window target; zero uses the 64 MiB default
}
Config controls opt-in parsing extensions. The zero value is strict and accepts only RFC 8259 JSON strings and escapes. SYS-REQ-115
func (Config) ArrayEach ¶ added in v1.5.0
func (c Config) ArrayEach(data []byte, cb func([]byte, ValueType, int, error), keys ...string) (int, error)
ArrayEach iterates over the addressed array using c's parsing options. SYS-REQ-115
func (Config) Delete ¶ added in v1.5.0
Delete removes the value addressed by keys using c's parsing options. SYS-REQ-115
func (Config) Get ¶ added in v1.5.0
Get returns the value addressed by keys using c's parsing options. SYS-REQ-115
func (Config) GetString ¶ added in v1.5.0
GetString returns the decoded string addressed by keys using c's parsing options. SYS-REQ-115
type ReaderParser ¶ added in v1.5.0
type ReaderParser struct {
// contains filtered or unexported fields
}
ReaderParser provides path-based access to a JSON stream. It is intended for one operation per stream: call Get, GetString, or ArrayEach after creating the parser. ReaderParser is not safe for concurrent use.
The sliding window is bounded by Config.MaxBufferSize while skipping input. A returned value (or one ArrayEach element) may exceed that size because its complete byte slice must remain available to the caller. SYS-REQ-116
func NewReaderParser ¶ added in v1.5.0
func NewReaderParser(r io.Reader, opts ...Config) *ReaderParser
NewReaderParser creates a streaming parser. If opts contains a Config, the first Config controls lenient parsing and the sliding-window size. SYS-REQ-116
func (*ReaderParser) ArrayEach ¶ added in v1.5.0
func (rp *ReaderParser) ArrayEach(cb func(value []byte, vt ValueType, err error)) error
ArrayEach incrementally iterates a root JSON array. Callback value slices are valid for the duration of the callback and must be copied if retained. Memory use is bounded by the largest element plus the sliding read window. SYS-REQ-116
func (*ReaderParser) Get ¶ added in v1.5.0
func (rp *ReaderParser) Get(keys ...string) (value []byte, vt ValueType, err error)
Get returns the value addressed by keys without loading the whole document. As with the package-level Get, string values are returned without quotes but retain their JSON escapes. SYS-REQ-116
type ValueType ¶
type ValueType int
Data types available in valid JSON data.
func Get ¶
Get - Receives data structure, and key path to extract value from.
Returns: `value` - Pointer to original data structure containing key value, or just empty slice if nothing found or error `dataType` - Can be: `NotExist`, `String`, `Number`, `Object`, `Array`, `Boolean` or `Null` `offset` - Offset from provided data structure where key value ends. Used mostly internally, for example for `ArrayEach` helper. `err` - If key not found or any other parsing issue it should return error. If key not found it also sets `dataType` to `NotExist`
Accept multiple keys to specify path to JSON value (in case of quering nested structures). If no keys provided it will try to extract closest JSON value (simple ones or object/array), useful for reading streams or arrays, see `ArrayEach` implementation.
SYS-REQ-001, SYS-REQ-016, SYS-REQ-017, SYS-REQ-018, SYS-REQ-019, SYS-REQ-025, SYS-REQ-026, SYS-REQ-041, SYS-REQ-042, SYS-REQ-043