Documentation ¶
Index ¶
- func DecodeBool(data []byte, v *bool) (p int, err error)
- func DecodeFloat64(data []byte, v *float64) (p int, err error)
- func DecodeInt(data []byte, v *int) (p int, err error)
- func DecodeInt32(data []byte, v *int32) (p int, err error)
- func DecodeInt64(data []byte, v *int64) (p int, err error)
- func DecodeString(data []byte, v *string, buf *[]byte) (p int, err error)
- func DecodeUint(data []byte, v *uint) (p int, err error)
- func DecodeUint32(data []byte, v *uint32) (p int, err error)
- func DecodeUint64(data []byte, v *uint64) (p int, err error)
- func HandleArrayValues(data []byte, handler ArrayValueHandler, buffer *Buffer) (p int, err error)
- func HandleObjectValues(data []byte, handler ObjectValueHandler, buffer *Buffer) (p int, err error)
- func NextToken(data []byte) (token byte, p int, err error)
- func ReadArray(data []byte) (val []interface{}, p int, err error)
- func ReadBool(data []byte) (val bool, p int, err error)
- func ReadFloat64(data []byte) (val float64, p int, err error)
- func ReadInt(data []byte) (val, p int, err error)
- func ReadInt32(data []byte) (val int32, p int, err error)
- func ReadInt64(data []byte) (val int64, p int, err error)
- func ReadNull(data []byte) (p int, err error)
- func ReadObject(data []byte) (val map[string]interface{}, p int, err error)
- func ReadString(data []byte, buf *[]byte) (val string, p int, err error)
- func ReadStringBytes(data, buf []byte) (val []byte, p int, err error)
- func ReadUint(data []byte) (val uint, p int, err error)
- func ReadUint32(data []byte) (val uint32, p int, err error)
- func ReadUint64(data []byte) (val uint64, p int, err error)
- func ReadValue(data []byte) (val interface{}, p int, err error)
- func SkipValue(data []byte, buffer *Buffer) (p int, err error)
- func SkipValueFast(data []byte, buffer *Buffer) (p int, err error)
- func StdLibCompatibleMap(rjsonMap map[string]interface{}) map[string]interface{}
- func StdLibCompatibleSlice(rjsonSlice []interface{}) []interface{}
- func StdLibCompatibleString(rjsonString string) string
- func StdLibCompatibleStringBytes(rjsonString, buf []byte) []byte
- func UnescapeStringContent(data, dst []byte) (val []byte, p int, err error)
- func Valid(data []byte, buffer *Buffer) bool
- type ArrayValueHandler
- type ArrayValueHandlerFunc
- type Buffer
- type ObjectValueHandler
- type ObjectValueHandlerFunc
- type TokenType
- type ValueReader
- func (h *ValueReader) HandleArrayValue(data []byte) (p int, err error)
- func (h *ValueReader) HandleObjectValue(fieldname, data []byte) (p int, err error)
- func (h *ValueReader) ReadArray(data []byte) (val []interface{}, p int, err error)
- func (h *ValueReader) ReadObject(data []byte) (val map[string]interface{}, p int, err error)
- func (h *ValueReader) ReadValue(data []byte) (val interface{}, p int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeBool ¶ added in v0.1.0
DecodeBool reads a boolean value at the beginning of data. If data begins with null, v is untouched.
func DecodeFloat64 ¶ added in v0.1.0
DecodeFloat64 reads a float64 value at the beginning of data. If data begins with null, v is untouched.
func DecodeInt ¶ added in v0.1.0
DecodeInt reads an int value at the beginning of data. If data begins with null, v is untouched.
func DecodeInt32 ¶ added in v0.1.0
DecodeInt32 reads an int32 value at the beginning of data. If data begins with null, v is untouched.
func DecodeInt64 ¶ added in v0.1.0
DecodeInt64 reads an int64 value at the beginning of data. If data begins with null, v is untouched.
func DecodeString ¶ added in v0.1.0
DecodeString reads a string value at the beginning of data. If data begins with null, v is untouched. If buf is not nil, it will be used as a working for building the string value.
func DecodeUint ¶ added in v0.1.0
DecodeUint reads a uint value at the beginning of data. If data begins with null, v is untouched.
func DecodeUint32 ¶ added in v0.1.0
DecodeUint32 reads a uint32 value at the beginning of data. If data begins with null, v is untouched.
func DecodeUint64 ¶ added in v0.1.0
DecodeUint64 reads a uint64 value at the beginning of data. If data begins with null, v is untouched.
func HandleArrayValues ¶
func HandleArrayValues(data []byte, handler ArrayValueHandler, buffer *Buffer) (p int, err error)
HandleArrayValues runs handler.HandleArrayValue on each item in the array at the beginning of data until it encounters an error or reaches the end of the array. p is the position after the last byte it read. When err is nil, p will be the position after the object. buffer is optional. Reusing a buffer can reduce memory allocations.
func HandleObjectValues ¶
func HandleObjectValues(data []byte, handler ObjectValueHandler, buffer *Buffer) (p int, err error)
HandleObjectValues runs handler.HandleObjectValue on each field in the object at the beginning of data until it encounters an error or reaches the end of the object. p is the position after the last byte it read. When err is nil, p will be the position after the object. buffer is optional. Reusing a buffer can reduce memory allocations.
Example ¶
package main import ( "fmt" "log" "github.com/willabides/rjson" ) func main() { data := ` { "foo": "bar", "bar": [1,2,3], "baz": true } ` var foo string var baz bool handler := rjson.ObjectValueHandlerFunc(func(fieldname, data []byte) (p int, err error) { switch string(fieldname) { case "foo": foo, p, err = rjson.ReadString(data, nil) case "baz": baz, p, err = rjson.ReadBool(data) } return p, err }) var buffer rjson.Buffer p, err := rjson.HandleObjectValues([]byte(data), handler, &buffer) if err != nil { log.Fatal(err) } fmt.Printf("We read %d bytes and got foo: %q, baz: %v\n", p, foo, baz) }
Output: We read 52 bytes and got foo: "bar", baz: true
func NextToken ¶
NextToken finds the first json token in data. token is the token itself, p is the position in data where the token was found. NextToken errors if it finds anything besides json whitespace before the first valid token. It returns io.EOF if data is empty or contains only whitespace.
func ReadArray ¶
ReadArray allocates a ValueReader and returns ValueReader.ReadArray. You should probably use ValueReader.ReadArray instead so you don't have to allocate a new ValueReader for each call.
func ReadBool ¶
ReadBool reads a boolean value at the beginning of data. p is the first position in data after the value.
func ReadFloat64 ¶
ReadFloat64 reads a float64 value at the beginning of data. p is the first position in data after the value.
func ReadInt ¶
ReadInt reads an int value at the beginning of data. p is the first position in data after the value.
func ReadInt32 ¶
ReadInt32 reads an int32 value at the beginning of data. p is the first position in data after the value.
func ReadInt64 ¶
ReadInt64 reads an int64 value at the beginning of data. p is the first position in data after the value.
func ReadNull ¶
ReadNull reads 'null' at the beginning of data. p is the first position after 'null'
func ReadObject ¶
ReadObject allocates a ValueReader and returns ValueReader.ReadObject. You should probably use ValueReader.ReadObject instead so you don't have to allocate a new ValueReader for each call.
func ReadString ¶
ReadString reads a string value at the beginning of data. p is the first position in data after the value. If buf is not nil, it will be used as a working buffer for building the string value.
If you are concerned about memory allocation, try using ReadStringBytes instead.
func ReadStringBytes ¶
ReadStringBytes reads a string value at the beginning of data and appends it to buf. p is the first position in data after the value.
func ReadUint ¶
ReadUint reads a uint value at the beginning of data. p is the first position in data after the value.
func ReadUint32 ¶
ReadUint32 reads a uint32 value at the beginning of data. p is the first position in data after the value.
func ReadUint64 ¶
ReadUint64 reads a uint64 value at the beginning of data. p is the first position in data after the value.
func ReadValue ¶
ReadValue allocates a ValueReader and returns ValueReader.ReadValue. You should probably use ValueReader.ReadValue instead so you don't have to allocate a new ValueReader for each call.
func SkipValue ¶
SkipValue skips the first json value in data. p is the position after the skipped value. buffer is optional. Reusing a buffer can reduce memory allocations.
func SkipValueFast ¶
SkipValueFast is like SkipValue but it speeds things up by skipping validation on objects and arrays. buffer is optional. Reusing a buffer can reduce memory allocations.
func StdLibCompatibleMap ¶
StdLibCompatibleMap returns a copy of rjsonMap with StdLibCompatibleString applied to all map keys and string values recursively.
func StdLibCompatibleSlice ¶
func StdLibCompatibleSlice(rjsonSlice []interface{}) []interface{}
StdLibCompatibleSlice returns a copy of rjsonSlice with StdLibCompatibleString applied to all string values recursively.
func StdLibCompatibleString ¶
StdLibCompatibleString replaces invalid utf8 with utf8.RuneError to match the standard library's behavior.
func StdLibCompatibleStringBytes ¶
StdLibCompatibleStringBytes replaces invalid utf8 with utf8.RuneError to match the standard library's behavior.
func UnescapeStringContent ¶
UnescapeStringContent unescapes the content of a raw json string. data must be the content of the raw string without starting and ending double quotes. This function is useful with ObjectValueHandler.HandleObjectValue.
Types ¶
type ArrayValueHandler ¶
ArrayValueHandler is a handler for values in a json array
type ArrayValueHandlerFunc ¶
ArrayValueHandlerFunc is a function that implements ArrayValueHandler
func (ArrayValueHandlerFunc) HandleArrayValue ¶
func (fn ArrayValueHandlerFunc) HandleArrayValue(data []byte) (int, error)
HandleArrayValue implements ArrayValueHandler.HandleArrayValue
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a reusable stack buffer for functions that read nested objects and arrays. Buffer is not thread-safe.
type ObjectValueHandler ¶
ObjectValueHandler is a handler for json objects.
type ObjectValueHandlerFunc ¶
ObjectValueHandlerFunc is a function that implements ObjectValueHandler
func (ObjectValueHandlerFunc) HandleObjectValue ¶
func (fn ObjectValueHandlerFunc) HandleObjectValue(fieldname, data []byte) (int, error)
HandleObjectValue implements ObjectValueHandler.HandleObjectValue
type TokenType ¶
type TokenType uint8
TokenType is the type of a json token
const ( InvalidType TokenType = iota NullType StringType NumberType TrueType FalseType ObjectStartType ObjectEndType ArrayStartType ArrayEndType CommaType ColonType )
TokenTypes
func NextTokenType ¶
NextTokenType finds the first json token in data and returns its TokenType. p is the position in data immediately after where the token was found. NextToken errors if it finds anything besides json whitespace before the first valid token. It returns io.EOF if data is empty or contains only whitespace.
type ValueReader ¶
type ValueReader struct {
// contains filtered or unexported fields
}
ValueReader is a handler for reading complex json data types (Objects and Arrays).
func (*ValueReader) HandleArrayValue ¶
func (h *ValueReader) HandleArrayValue(data []byte) (p int, err error)
HandleArrayValue implements ArrayValueHandler.HandleArrayValue
func (*ValueReader) HandleObjectValue ¶
func (h *ValueReader) HandleObjectValue(fieldname, data []byte) (p int, err error)
HandleObjectValue implements ValueObjectHandler.HandleObjectValue.
func (*ValueReader) ReadArray ¶
func (h *ValueReader) ReadArray(data []byte) (val []interface{}, p int, err error)
ReadArray reads an array from the front of data and returns it as a []interface{}. p is the first position in data after the value.
func (*ValueReader) ReadObject ¶
func (h *ValueReader) ReadObject(data []byte) (val map[string]interface{}, p int, err error)
ReadObject reads an object value from the front of data and returns it as a map[string]interface{}. p is the first position in data after the value.
func (*ValueReader) ReadValue ¶
func (h *ValueReader) ReadValue(data []byte) (val interface{}, p int, err error)
ReadValue reads a value at the beginning of data. The result will be a string, bool, nil, float64, []interface{} or map[string]interface{} depending on the json data type. p is the first position in data after the value.