Documentation ¶
Index ¶
- Variables
- func DumpBytes(obj interface{}, options ...DumpOption) ([]byte, error)
- func DumpString(obj interface{}, options ...DumpOption) (string, error)
- func Marshal(v interface{}) ([]byte, error)
- func MustDumpBytes(obj interface{}, options ...DumpOption) []byte
- func MustDumpString(obj interface{}, options ...DumpOption) string
- func Unmarshal(data []byte, v interface{}) error
- func Version() string
- type Bag
- type DumpOption
- 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) Data() interface{}
- func (j *Json) Encode() ([]byte, error)
- func (j *Json) Float64() (float64, 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) MakeArray() []interface{}
- func (j *Json) Map() (map[string]interface{}, error)
- func (j *Json) MarshalJSON() ([]byte, error)
- func (j *Json) MustArray(args ...[]interface{}) []interface{}
- 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) Nil() bool
- func (j *Json) Set(key string, val interface{})
- func (j *Json) String() (string, error)
- func (j *Json) StringArray() ([]string, error)
- func (j *Json) UnmarshalJSON(p []byte) error
- type Raw
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoMap = errors.New("type assertion to map[string]interface{} failed") ErrNoArray = errors.New("type assertion to []interface{} failed") ErrNoBool = errors.New("type assertion to bool failed") ErrNoString = errors.New("type assertion to string failed") ErrNoFloat = errors.New("type assertion to float64 failed") ErrNoByteArray = errors.New("type assertion to []byte failed") )
Functions ¶
func DumpBytes ¶
func DumpBytes(obj interface{}, options ...DumpOption) ([]byte, error)
DumpBytes convert Go data object to JSON []byte
func DumpString ¶
func DumpString(obj interface{}, options ...DumpOption) (string, error)
DumpString encode Go data object to JSON string
func MustDumpBytes ¶
func MustDumpBytes(obj interface{}, options ...DumpOption) []byte
MustDumpBytes encode Go data object to JSON []byte (panic in case of error)
func MustDumpString ¶
func MustDumpString(obj interface{}, options ...DumpOption) string
MustDumpString encode Go data object to JSON string (panic in case of error)
Types ¶
type DumpOption ¶
DumpOption is the type of DumpBytes/DumpString options
func EscapeHTML ¶
func EscapeHTML(escape bool) DumpOption
EscapeHTML instruct DumpBytes/DumpString to escape HTML in JSON valus (if true)
func Indent ¶
func Indent(s string) DumpOption
Indent sets the indentation level (passed as string of spaces) for DumpBytes/DumpString
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
func LoadPartial ¶
LoadPartial load JSON from `body` []byte and return a new `Json` object. It also returns any remaining bytes from the input.
func LoadPartialString ¶
LoadPartialString load JSON from `body` string and return a new `Json` object. It also returns any remaining bytes from the input.
func LoadString ¶
LoadString loads JSON from `body` string and return a new `Json` object
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) 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 resturns 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) MakeArray ¶
func (j *Json) MakeArray() []interface{}
MakeArray always return an `array` (this is useful for HAL responses that can return either an array or a single element ):
func (*Json) MarshalJSON ¶
Implements the json.Marshaler interface.
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 interate over array values in a succinct manner:
for i, v := range js.Get("results").MustArray() { fmt.Println(i, v) }
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 interate 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) Set ¶
Set modifies `Json` map by `key` and `value` Useful for changing single key/value in a `Json` object easily.
func (*Json) StringArray ¶
StringArray type asserts to an `array` of `string`
func (*Json) UnmarshalJSON ¶
Implements the json.Unmarshaler interface.
type Raw ¶
type Raw = json.RawMessage
this is an alias for json.RawMessage, for clients that don't include encoding/json