Documentation ¶
Overview ¶
Package simplejson provides a wrapper for arbitrary JSON objects that adds methods to access properties. Use of this package in place of types and the standard library's encoding/json package is strongly discouraged.
Don't lint for stale code, since it's a copied library and we might as well keep the whole thing. nolint:unused
Index ¶
- func Version() string
- type Json
- func (j *Json) Array() ([]interface{}, error)
- func (j *Json) Bool() (bool, error)
- func (j *Json) Bytes() ([]byte, error)
- func (j *Json) CheckGet(key string) (*Json, bool)
- func (j *Json) CheckGetIndex(index int) (*Json, bool)
- func (j *Json) Del(key string)
- func (j *Json) Encode() ([]byte, error)
- func (j *Json) EncodePretty() ([]byte, error)
- func (j *Json) Float64() (float64, error)
- func (j *Json) FromDB(data []byte) error
- func (j *Json) Get(key string) *Json
- func (j *Json) GetIndex(index int) *Json
- func (j *Json) GetPath(branch ...string) *Json
- func (j *Json) Int() (int, error)
- func (j *Json) Int64() (int64, error)
- func (j *Json) Interface() interface{}
- func (j *Json) Map() (map[string]interface{}, error)
- func (j *Json) MarshalJSON() ([]byte, error)
- func (j *Json) MarshalYAML() (interface{}, error)
- func (j *Json) MustArray(args ...[]interface{}) []interface{}
- func (j *Json) MustBool(args ...bool) bool
- func (j *Json) MustFloat64(args ...float64) float64
- func (j *Json) MustInt(args ...int) int
- func (j *Json) MustInt64(args ...int64) int64
- func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{}
- func (j *Json) MustString(args ...string) string
- func (j *Json) MustStringArray(args ...[]string) []string
- func (j *Json) MustUint64(args ...uint64) uint64
- func (j *Json) Scan(val interface{}) error
- func (j *Json) Set(key string, val interface{})
- func (j *Json) SetIndex(index int, val interface{})
- func (j *Json) SetPath(branch []string, val interface{})
- func (j *Json) String() (string, error)
- func (j *Json) StringArray() ([]string, error)
- func (j *Json) ToDB() ([]byte, error)
- func (j *Json) Uint64() (uint64, error)
- func (j *Json) UnmarshalJSON(p []byte) error
- func (j *Json) UnmarshalYAML(unmarshal func(interface{}) error) error
- func (j *Json) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
func MustJson ¶
MustJson returns a pointer to a new `Json` object, panicking if `body` cannot be parsed.
func NewFromAny ¶
func NewFromAny(data interface{}) *Json
NewFromAny returns a pointer to a new `Json` object with provided data.
func NewFromReader ¶
NewFromReader returns a *Json by decoding from an io.Reader
func (*Json) CheckGet ¶
CheckGet returns a pointer to a new `Json` object and a `bool` identifying success or failure
useful for chained operations when success is important:
if data, ok := js.Get("top_level").CheckGet("inner"); ok { log.Println(data) }
func (*Json) CheckGetIndex ¶
CheckGetIndex returns a pointer to a new `Json` object for `index` in its `array` representation, and a `bool` indicating success or failure
useful for chained operations when success is important:
if data, ok := js.Get("top_level").CheckGetIndex(0); ok { log.Println(data) }
func (*Json) EncodePretty ¶
EncodePretty returns its marshaled data as `[]byte` with indentation
func (*Json) Get ¶
Get returns a pointer to a new `Json` object for `key` in its `map` representation
useful for chaining operations (to traverse a nested JSON):
js.Get("top_level").Get("dict").Get("value").Int()
func (*Json) GetIndex ¶
GetIndex returns a pointer to a new `Json` object for `index` in its `array` representation
this is the analog to Get when accessing elements of a json array instead of a json object:
js.Get("top_level").Get("array").GetIndex(1).Get("key").Int()
func (*Json) GetPath ¶
GetPath searches for the item as specified by the branch without the need to deep dive using Get()'s.
js.GetPath("top_level", "dict")
func (*Json) Interface ¶
func (j *Json) Interface() interface{}
Interface returns the underlying data
func (*Json) MarshalJSON ¶
Implements the json.Marshaler interface.
func (*Json) MarshalYAML ¶
MarshalYAML implements yaml.Marshaller.
func (*Json) MustArray ¶
func (j *Json) MustArray(args ...[]interface{}) []interface{}
MustArray guarantees the return of a `[]interface{}` (with optional default)
useful when you want to iterate over array values in a succinct manner:
for i, v := range js.Get("results").MustArray() { fmt.Println(i, v) }
func (*Json) MustBool ¶
MustBool guarantees the return of a `bool` (with optional default)
useful when you explicitly want a `bool` in a single value return context:
myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true))
func (*Json) MustFloat64 ¶
MustFloat64 guarantees the return of a `float64` (with optional default)
useful when you explicitly want a `float64` in a single value return context:
myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150))
func (*Json) MustInt ¶
MustInt guarantees the return of an `int` (with optional default)
useful when you explicitly want an `int` in a single value return context:
myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150))
func (*Json) MustInt64 ¶
MustInt64 guarantees the return of an `int64` (with optional default)
useful when you explicitly want an `int64` in a single value return context:
myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150))
func (*Json) MustMap ¶
MustMap guarantees the return of a `map[string]interface{}` (with optional default)
useful when you want to iterate over map values in a succinct manner:
for k, v := range js.Get("dictionary").MustMap() { fmt.Println(k, v) }
func (*Json) MustString ¶
MustString guarantees the return of a `string` (with optional default)
useful when you explicitly want a `string` in a single value return context:
myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default"))
func (*Json) MustStringArray ¶
MustStringArray guarantees the return of a `[]string` (with optional default)
useful when you want to iterate over array values in a succinct manner:
for i, s := range js.Get("results").MustStringArray() { fmt.Println(i, s) }
func (*Json) MustUint64 ¶
MustUInt64 guarantees the return of an `uint64` (with optional default)
useful when you explicitly want an `uint64` in a single value return context:
myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150))
func (*Json) Set ¶
Set modifies `Json` map by `key` and `value` Useful for changing single key/value in a `Json` object easily.
func (*Json) SetIndex ¶
SetIndex modifies `Json` array by `index` and `value` for `index` in its `array` representation
func (*Json) SetPath ¶
SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, and then finally writing in the value
func (*Json) StringArray ¶
StringArray type asserts to an `array` of `string`
func (*Json) UnmarshalJSON ¶
Implements the json.Unmarshaler interface.
func (*Json) UnmarshalYAML ¶
UnmarshalYAML implements yaml.Unmarshaller.