Documentation ¶
Index ¶
- Constants
- Variables
- func ArrayEach(data []byte, ...) (offset int, err error)
- func Delete(data []byte, keys ...string) []byte
- func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int
- 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 GetString(data []byte, keys ...string) (val string, err 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 ParseString(b []byte) (string, error)
- func Set(data []byte, setValue []byte, keys ...string) (value []byte, err error)
- func Unescape(in, out []byte) ([]byte, error)
- 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") MalformedStringEscapeError = errors.New("Encountered an invalid escape sequence in a string") )
Errors
Functions ¶
func ArrayEach ¶
func ArrayEach(data []byte, cb func(index int, value []byte, dataType ValueType, offset int) error, keys ...string) (offset int, err error)
ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
func Delete ¶
Del - Receives existing data structure, path to delete.
Returns: `data` - return modified data
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.
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.
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.
func GetString ¶
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 GetUnsafeString ¶
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)
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)
func ParseFloat ¶
ParseNumber parses a Number ValueType into a Go float64
func ParseString ¶
ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
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
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 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.