jsonparser

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NotExist = ValueType(iota)
	String
	Number
	Object
	Array
	Boolean
	Null
	Unknown
)

Variables

View Source
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

func Delete(data []byte, keys ...string) []byte

Del - Receives existing data structure, path to delete.

Returns: `data` - return modified data

func EachKey

func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int

func GetBoolean

func GetBoolean(data []byte, keys ...string) (val bool, err error)

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

func GetFloat(data []byte, keys ...string) (val float64, err error)

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

func GetInt(data []byte, keys ...string) (val int64, err error)

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

func GetString(data []byte, keys ...string) (val string, err error)

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

func GetUnsafeString(data []byte, keys ...string) (val string, err error)

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

func ParseBoolean(b []byte) (bool, error)

ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)

func ParseFloat

func ParseFloat(b []byte) (float64, error)

ParseNumber parses a Number ValueType into a Go float64

func ParseInt

func ParseInt(b []byte) (int64, error)

ParseInt parses a Number ValueType into a Go int64

func ParseString

func ParseString(b []byte) (string, error)

ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)

func Set

func Set(data []byte, setValue []byte, keys ...string) (value []byte, err error)

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

func Unescape(in, out []byte) ([]byte, error)

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

func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset int, err error)

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.

func (ValueType) String

func (vt ValueType) String() string

Jump to

Keyboard shortcuts

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