Documentation ¶
Index ¶
- Variables
- type KeyNotFoundError
- type Object
- func (v *Object) GetBoolean(keys ...string) (bool, error)
- func (v *Object) GetBooleanArray(keys ...string) ([]bool, error)
- func (v *Object) GetFloat64(keys ...string) (float64, error)
- func (v *Object) GetFloat64Array(keys ...string) ([]float64, error)
- func (v *Object) GetInt64(keys ...string) (int64, error)
- func (v *Object) GetInt64Array(keys ...string) ([]int64, error)
- func (v *Object) GetInterface(keys ...string) (interface{}, error)
- func (v *Object) GetNull(keys ...string) error
- func (v *Object) GetNullArray(keys ...string) (int64, error)
- func (v *Object) GetNumber(keys ...string) (json.Number, error)
- func (v *Object) GetNumberArray(keys ...string) ([]json.Number, error)
- func (v *Object) GetObject(keys ...string) (*Object, error)
- func (v *Object) GetObjectArray(keys ...string) ([]*Object, error)
- func (v *Object) GetString(keys ...string) (string, error)
- func (v *Object) GetStringArray(keys ...string) ([]string, error)
- func (v *Object) GetValue(keys ...string) (*Value, error)
- func (v *Object) GetValueArray(keys ...string) ([]*Value, error)
- func (v *Object) Map() map[string]*Value
- func (v *Object) MustGetString(path string, def string) string
- func (v *Object) SetValue(key string, value interface{}) *Value
- func (v *Object) String() string
- type Value
- func (v *Value) Array() ([]*Value, error)
- func (v *Value) Boolean() (bool, error)
- func (v *Value) Float64() (float64, error)
- func (v *Value) Int64() (int64, error)
- func (v *Value) Interface() interface{}
- func (v *Value) Marshal() ([]byte, error)
- func (v *Value) Null() error
- func (v *Value) Number() (json.Number, error)
- func (v *Value) Object() (*Object, error)
- func (v *Value) ObjectArray() ([]*Object, error)
- func (v *Value) String() (string, error)
- func (v *Value) StringMap() map[string]interface{}
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotNull = errors.New("is not null") ErrNotArray = errors.New("Not an array") ErrNotNumber = errors.New("not a number") ErrNotBool = errors.New("no bool") ErrNotObject = errors.New("not an object") ErrNotObjectArray = errors.New("not an object array") ErrNotString = errors.New("not a string") )
Error values returned when validation functions fail
Functions ¶
This section is empty.
Types ¶
type KeyNotFoundError ¶
type KeyNotFoundError struct {
Key string
}
func (KeyNotFoundError) Error ¶
func (k KeyNotFoundError) Error() string
type Object ¶
type Object struct { Value // contains filtered or unexported fields }
Object represents an object JSON object. It inherets from Value but with an additional method to access a map representation of it's content. It's useful when iterating.
func NewFromMap ¶
func NewObjectFromBytes ¶
func (*Object) GetBoolean ¶
Gets the value at key path and attempts to typecast the value into a bool. Returns error if the value is not a json boolean. Example:
married, err := GetBoolean("person", "married")
func (*Object) GetBooleanArray ¶
Gets the value at key path and attempts to typecast the value into an array of bools. Returns error if the value is not a json array or if any of the contained objects are not booleans.
func (*Object) GetFloat64 ¶
Gets the value at key path and attempts to typecast the value into a float64. Returns error if the value is not a json number. Example:
n, err := GetNumber("address", "street_number")
func (*Object) GetFloat64Array ¶
Gets the value at key path and attempts to typecast the value into an array of floats. Returns error if the value is not a json array or if any of the contained objects are not numbers.
func (*Object) GetInt64 ¶
Gets the value at key path and attempts to typecast the value into a float64. Returns error if the value is not a json number. Example:
n, err := GetNumber("address", "street_number")
func (*Object) GetInt64Array ¶
Gets the value at key path and attempts to typecast the value into an array of ints. Returns error if the value is not a json array or if any of the contained objects are not numbers.
func (*Object) GetInterface ¶
Gets the value at key path and attempts to typecast the value into a float64. Returns error if the value is not a json number. Example:
v, err := GetInterface("address", "anything")
func (*Object) GetNull ¶
Gets the value at key path and attempts to typecast the value into null. Returns error if the value is not json null. Example:
err := GetNull("address", "street")
func (*Object) GetNullArray ¶
Gets the value at key path and attempts to typecast the value into an array of nulls. Returns length, or an error if the value is not a json array or if any of the contained objects are not nulls.
func (*Object) GetNumber ¶
Gets the value at key path and attempts to typecast the value into a number. Returns error if the value is not a json number. Example:
n, err := GetNumber("address", "street_number")
func (*Object) GetNumberArray ¶
Gets the value at key path and attempts to typecast the value into an array of numbers. Returns error if the value is not a json array or if any of the contained objects are not numbers. Example:
friendAges, err := GetNumberArray("person", "friend_ages") for i, friendAge := range friendAges { ... // friendAge will be of type float64 here }
func (*Object) GetObject ¶
Gets the value at key path and attempts to typecast the value into an object. Returns error if the value is not a json object. Example:
object, err := GetObject("person", "address")
func (*Object) GetObjectArray ¶
Gets the value at key path and attempts to typecast the value into an array of objects. Returns error if the value is not a json array or if any of the contained objects are not objects. Example:
friends, err := GetObjectArray("person", "friends") for i, friend := range friends { ... // friend will be of type Object here }
func (*Object) GetString ¶
Gets the value at key path and attempts to typecast the value into a string. Returns error if the value is not a json string. Example:
string, err := GetString("address", "street")
func (*Object) GetStringArray ¶
Gets the value at key path and attempts to typecast the value into an array of string. Returns error if the value is not a json array or if any of the contained objects are not strings. Gets the value at key path and attempts to typecast the value into an array of objects. Returns error if the value is not a json array or if any of the contained objects are not objects. Example:
friendNames, err := GetStringArray("person", "friend_names") for i, friendName := range friendNames { ... // friendName will be of type string here }
func (*Object) GetValue ¶
Gets the value at key path. Returns error if the value does not exist. Consider using the more specific Get<Type>(..) methods instead. Example:
value, err := GetValue("address", "street")
func (*Object) GetValueArray ¶
Gets the value at key path and attempts to typecast the value into an array. Returns error if the value is not a json array. Consider using the more specific Get<Type>Array() since it may reduce later type casts. Example:
friends, err := GetValueArray("person", "friends") for i, friend := range friends { ... // friend will be of type Value here }
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value represents an arbitrary JSON value. It may contain a bool, number, string, object, array or null.
func NewValueFromBytes ¶
Creates a new value from bytes. Returns an error if the bytes are not valid json.
func NewValueFromReader ¶
Creates a new value from an io.reader. Returns an error if the reader does not contain valid json. Useful for parsing the body of a net/http response. Example: NewFromReader(res.Body)
func (*Value) Array ¶
Attempts to typecast the current value into an array. Returns error if the current value is not a json array. Example:
friendsArray, err := friendsValue.Array()
func (*Value) Boolean ¶
Attempts to typecast the current value into a bool. Returns error if the current value is not a json boolean. Example:
marriedBool, err := marriedValue.Boolean()
func (*Value) Float64 ¶
Attempts to typecast the current value into a float64. Returns error if the current value is not a json number. Example:
percentage, err := v.Float64()
func (*Value) Int64 ¶
Attempts to typecast the current value into a int64. Returns error if the current value is not a json number. Example:
id, err := v.Int64()
func (*Value) Interface ¶
func (v *Value) Interface() interface{}
Get the interyling data as interface
func (*Value) Number ¶
Attempts to typecast the current value into a number. Returns error if the current value is not a json number. Example:
ageNumber, err := ageValue.Number()
func (*Value) Object ¶
Attempts to typecast the current value into an object. Returns error if the current value is not a json object. Example:
friendObject, err := friendValue.Object()
func (*Value) ObjectArray ¶
Attempts to typecast the current value into an object arrau. Returns error if the current value is not an array of json objects Example:
friendObjects, err := friendValues.ObjectArray()