jsonparser: github.com/buger/jsonparser Index | Files | Directories

package jsonparser

import "github.com/buger/jsonparser"

Index

Package Files

bytes.go bytes_unsafe.go escape.go parser.go

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")
)

Errors

func ArrayEach Uses

func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err 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 Uses

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

Del - Receives existing data structure, path to delete.

Returns: `data` - return modified data

func EachKey Uses

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

func GetBoolean Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func ParseFloat Uses

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

ParseNumber parses a Number ValueType into a Go float64

func ParseInt Uses

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

ParseInt parses a Number ValueType into a Go int64

func ParseString Uses

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 Uses

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 StringToBytes Uses

func StringToBytes(s string) []byte

func Unescape Uses

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.

type ValueType Uses

type ValueType int

Data types available in valid JSON data.

func Get Uses

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 Uses

func (vt ValueType) String() string

Directories

PathSynopsis
benchmark

Package jsonparser imports 8 packages (graph) and is imported by 127 packages. Updated 2018-11-15. Refresh now. Tools for package owners.