Documentation ¶
Index ¶
- Constants
- func CopyFrom(m map[string]interface{}) map[string]interface{}
- func GetString(data []byte, keys ...string) string
- func IfZero(arg interface{}) bool
- func SmartPrint(i interface{}, escapeZero ...bool)
- type JsonCracker
- func (jc JsonCracker) Add(data []byte, k string, v interface{}, keys ...string) ([]byte, error)
- func (jc JsonCracker) Delete(vtype int, safe bool, data []byte, keys ...string) (interface{}, error)
- func (jc JsonCracker) Get(vtype int, data []byte, keys ...string) (interface{}, error)
- func (jc JsonCracker) Marshal(dest interface{}) ([]byte, error)
- func (jc JsonCracker) MustUpdate(data []byte, k string, v interface{}, keys ...string) ([]byte, error)
- func (jc JsonCracker) SafeUpdate(data []byte, k string, v interface{}, keys ...string) ([]byte, error)
- func (jc *JsonCracker) SetJsoner(jsoner JsonMarshaller)
- func (jc JsonCracker) Unmarshal(data []byte, dest interface{}) error
- func (jc JsonCracker) Update(data []byte, k string, v interface{}, keys ...string) ([]byte, error)
- type JsonMarshaller
- type Jsoner
- type Time
Constants ¶
const ( MAP = 1 + iota ARRAY BYTES )
Variables ¶
This section is empty.
Functions ¶
func SmartPrint ¶
func SmartPrint(i interface{}, escapeZero ...bool)
Types ¶
type JsonCracker ¶
type JsonCracker struct {
Json JsonMarshaller
}
func NewCracker ¶
func NewCracker(jsoner JsonMarshaller) JsonCracker
New a json-cracker instance jsoner == nil , official json marshaler will be put to use,otherwise use jsoner as specific
func (JsonCracker) Delete ¶
func (jc JsonCracker) Delete(vtype int, safe bool, data []byte, keys ...string) (interface{}, error)
vtype: jsoncrack.MAP,jsoncrack.Array,jsoncrack.BYTES When vtype is 'jsoncrack.Map', it returns (map[string]interface{}, error), otherwise it returns (json.RawMessage,error) or ([]byte, error)
when safe is true, delete the specific key while not existed, throws an error. when safe is false, do nothing if not existed keys
Delete() returns the data modified already, while keeping the former data unchanged. When data is input , function will marshal its unmarshal object again to get a copy of data
func (JsonCracker) Get ¶
func (jc JsonCracker) Get(vtype int, data []byte, keys ...string) (interface{}, error)
get a value via keys through a json []byte. value can specifc type as jsoncrack.BYTES or jsoncrack.MAP.The formmer returns json []bytes boxing in interface{},the other is a map[string]interface{} boxing in interface{}
func (JsonCracker) Marshal ¶
func (jc JsonCracker) Marshal(dest interface{}) ([]byte, error)
json marshal via its jsoner realization
func (JsonCracker) MustUpdate ¶
func (jc JsonCracker) MustUpdate(data []byte, k string, v interface{}, keys ...string) ([]byte, error)
When exists the key 'k' , it replace the origin value with v
func (JsonCracker) SafeUpdate ¶
func (jc JsonCracker) SafeUpdate(data []byte, k string, v interface{}, keys ...string) ([]byte, error)
when exists the key 'k', it throws an error without replacing
func (*JsonCracker) SetJsoner ¶
func (jc *JsonCracker) SetJsoner(jsoner JsonMarshaller)
set cracker's jsoner make sure the function executing while app is init, don't set while app is running. good:
func init(){ jc.SetJsoner(yourJsoner) }
bad:
func service() { jc.Marshal() ... jc.SetJsoner(yourJsoner) ... }
func (JsonCracker) Unmarshal ¶
func (jc JsonCracker) Unmarshal(data []byte, dest interface{}) error
json unmarshal via its jsoner realization
func (JsonCracker) Update ¶
update a json raw-message with a new key-value pair assume data:
{ "class1": { "master": { "name": "Li Hua", "Age": 28 }, "students": [ { "name": "Li Lei", "Age": 12 }, { "name": "Tom", "Age": 11 } ] } }
after exec jc.Update([]byte(data), "sub_name", "Li li", "class1", "master") returns:
{ "class1": { "master": { "name": "Li Hua", "Age": 28, "sub_name":"Li li" }, "students": [ { "name": "Li Lei", "Age": 12 }, { "name": "Tom", "Age": 11 } ] } }