Documentation
¶
Overview ¶
对gjson源码进行了部分改造
Index ¶
- Variables
- func AddModifier(name string, fn func(json, arg string) string)
- func ForEachLine(json string, iterator func(line Result) bool)
- func ModifierExists(name string, fn func(json, arg string) string) bool
- func StringToDate(s string) (time.Time, error)
- func ToBool(i interface{}) bool
- func ToBoolE(i interface{}) (bool, error)
- func ToBoolSlice(i interface{}) []bool
- func ToBoolSliceE(i interface{}) ([]bool, error)
- func ToDuration(i interface{}) time.Duration
- func ToDurationE(i interface{}) (d time.Duration, err error)
- func ToDurationSlice(i interface{}) []time.Duration
- func ToDurationSliceE(i interface{}) ([]time.Duration, error)
- func ToFloat32(i interface{}) float32
- func ToFloat32E(i interface{}) (float32, error)
- func ToFloat64(i interface{}) float64
- func ToFloat64E(i interface{}) (float64, error)
- func ToInt(i interface{}) int
- func ToInt16(i interface{}) int16
- func ToInt16E(i interface{}) (int16, error)
- func ToInt32(i interface{}) int32
- func ToInt32E(i interface{}) (int32, error)
- func ToInt64(i interface{}) int64
- func ToInt64E(i interface{}) (int64, error)
- func ToInt8(i interface{}) int8
- func ToInt8E(i interface{}) (int8, error)
- func ToIntE(i interface{}) (int, error)
- func ToIntSlice(i interface{}) []int
- func ToIntSliceE(i interface{}) ([]int, error)
- func ToSlice(i interface{}) []interface{}
- func ToSliceE(i interface{}) ([]interface{}, error)
- func ToString(i interface{}) string
- func ToStringE(i interface{}) (string, error)
- func ToStringMap(i interface{}) map[string]interface{}
- func ToStringMapBool(i interface{}) map[string]bool
- func ToStringMapBoolE(i interface{}) (map[string]bool, error)
- func ToStringMapE(i interface{}) (map[string]interface{}, error)
- func ToStringMapInt(i interface{}) map[string]int
- func ToStringMapInt64(i interface{}) map[string]int64
- func ToStringMapInt64E(i interface{}) (map[string]int64, error)
- func ToStringMapIntE(i interface{}) (map[string]int, error)
- func ToStringMapString(i interface{}) map[string]string
- func ToStringMapStringE(i interface{}) (map[string]string, error)
- func ToStringMapStringSlice(i interface{}) map[string][]string
- func ToStringMapStringSliceE(i interface{}) (map[string][]string, error)
- func ToStringSlice(i interface{}) []string
- func ToStringSliceE(i interface{}) ([]string, error)
- func ToTime(i interface{}) time.Time
- func ToTimeE(i interface{}) (tim time.Time, err error)
- func ToUint(i interface{}) uint
- func ToUint16(i interface{}) uint16
- func ToUint16E(i interface{}) (uint16, error)
- func ToUint32(i interface{}) uint32
- func ToUint32E(i interface{}) (uint32, error)
- func ToUint64(i interface{}) uint64
- func ToUint64E(i interface{}) (uint64, error)
- func ToUint8(i interface{}) uint8
- func ToUint8E(i interface{}) (uint8, error)
- func ToUintE(i interface{}) (uint, error)
- func Valid(json string) bool
- func ValidBytes(json []byte) bool
- type Any
- func (a Any) ToBool() bool
- func (a Any) ToBoolSlice() []bool
- func (a Any) ToDuration() time.Duration
- func (a Any) ToDurationSlice() []time.Duration
- func (a Any) ToFloat32() float32
- func (a Any) ToFloat64() float64
- func (a Any) ToInt() int
- func (a Any) ToInt16() int16
- func (a Any) ToInt32() int32
- func (a Any) ToInt64() int64
- func (a Any) ToInt8() int8
- func (a Any) ToIntSlice() []int
- func (a Any) ToSlice() []interface{}
- func (a Any) ToString() string
- func (a Any) ToStringMap() map[string]interface{}
- func (a Any) ToStringMapBool() map[string]bool
- func (a Any) ToStringMapInt() map[string]int
- func (a Any) ToStringMapInt64() map[string]int64
- func (a Any) ToStringMapString() map[string]string
- func (a Any) ToStringMapStringSlice() map[string][]string
- func (a Any) ToStringSlice() []string
- func (a Any) ToTime() time.Time
- func (a Any) ToUint() uint
- func (a Any) ToUint16() uint16
- func (a Any) ToUint32() uint32
- func (a Any) ToUint64() uint64
- func (a Any) ToUint8() uint8
- type Result
- func (t Result) Array() []Result
- func (t Result) Bool() bool
- func (t Result) Exists() bool
- func (t Result) Float() float64
- func (t Result) ForEach(iterator func(key, value Result) bool)
- func (t Result) Get(path string) Result
- func (t Result) GetAny(path string) Result
- func (t Result) Int() int64
- func (t Result) IsArray() bool
- func (t Result) IsObject() bool
- func (t Result) Less(token Result, caseSensitive bool) bool
- func (t Result) Map() map[string]Result
- func (t Result) String() string
- func (t Result) Time() time.Time
- func (t Result) Uint() uint64
- func (t Result) Value() interface{}
- type Type
Constants ¶
This section is empty.
Variables ¶
var DisableModifiers = false
DisableModifiers will disable the modifier syntax
Functions ¶
func AddModifier ¶
AddModifier binds a custom modifier command to the GJSON syntax. This operation is not thread safe and should be executed prior to using all other gjson function.
func ForEachLine ¶
ForEachLine iterates through lines of JSON as specified by the JSON Lines format (http://jsonlines.org/). Each line is returned as a GJSON Result.
func ModifierExists ¶
ModifierExists returns true when the specified modifier exists.
func StringToDate ¶ added in v1.0.1
StringToDate attempts to parse a string into a time.Time type using a predefined list of formats. If no suitable format is found, an error is returned.
func ToBool ¶ added in v1.0.1
func ToBool(i interface{}) bool
ToBool casts an interface to a bool type.
func ToBoolSlice ¶ added in v1.0.1
func ToBoolSlice(i interface{}) []bool
ToBoolSlice casts an interface to a []bool type.
func ToBoolSliceE ¶ added in v1.0.1
ToBoolSliceE casts an interface to a []bool type.
func ToDuration ¶ added in v1.0.1
ToDuration casts an interface to a time.Duration type.
func ToDurationE ¶ added in v1.0.1
ToDurationE casts an interface to a time.Duration type.
func ToDurationSlice ¶ added in v1.0.1
ToDurationSlice casts an interface to a []time.Duration type.
func ToDurationSliceE ¶ added in v1.0.1
ToDurationSliceE casts an interface to a []time.Duration type.
func ToFloat32 ¶ added in v1.0.1
func ToFloat32(i interface{}) float32
ToFloat32 casts an interface to a float32 type.
func ToFloat32E ¶ added in v1.0.1
ToFloat32E casts an interface to a float32 type.
func ToFloat64 ¶ added in v1.0.1
func ToFloat64(i interface{}) float64
ToFloat64 casts an interface to a float64 type.
func ToFloat64E ¶ added in v1.0.1
ToFloat64E casts an interface to a float64 type.
func ToInt16 ¶ added in v1.0.1
func ToInt16(i interface{}) int16
ToInt16 casts an interface to an int16 type.
func ToInt32 ¶ added in v1.0.1
func ToInt32(i interface{}) int32
ToInt32 casts an interface to an int32 type.
func ToInt64 ¶ added in v1.0.1
func ToInt64(i interface{}) int64
ToInt64 casts an interface to an int64 type.
func ToInt8 ¶ added in v1.0.1
func ToInt8(i interface{}) int8
ToInt8 casts an interface to an int8 type.
func ToIntSlice ¶ added in v1.0.1
func ToIntSlice(i interface{}) []int
ToIntSlice casts an interface to a []int type.
func ToIntSliceE ¶ added in v1.0.1
ToIntSliceE casts an interface to a []int type.
func ToSlice ¶ added in v1.0.1
func ToSlice(i interface{}) []interface{}
ToSlice casts an interface to a []interface{} type.
func ToSliceE ¶ added in v1.0.1
func ToSliceE(i interface{}) ([]interface{}, error)
ToSliceE casts an interface to a []interface{} type.
func ToString ¶ added in v1.0.1
func ToString(i interface{}) string
ToString casts an interface to a string type.
func ToStringMap ¶ added in v1.0.1
func ToStringMap(i interface{}) map[string]interface{}
ToStringMap casts an interface to a map[string]interface{} type.
func ToStringMapBool ¶ added in v1.0.1
ToStringMapBool casts an interface to a map[string]bool type.
func ToStringMapBoolE ¶ added in v1.0.1
ToStringMapBoolE casts an interface to a map[string]bool type.
func ToStringMapE ¶ added in v1.0.1
ToStringMapE casts an interface to a map[string]interface{} type.
func ToStringMapInt ¶ added in v1.0.1
ToStringMapInt casts an interface to a map[string]int type.
func ToStringMapInt64 ¶ added in v1.0.1
ToStringMapInt64 casts an interface to a map[string]int64 type.
func ToStringMapInt64E ¶ added in v1.0.1
ToStringMapInt64E casts an interface to a map[string]int64{} type.
func ToStringMapIntE ¶ added in v1.0.1
ToStringMapIntE casts an interface to a map[string]int{} type.
func ToStringMapString ¶ added in v1.0.1
ToStringMapString casts an interface to a map[string]string type.
func ToStringMapStringE ¶ added in v1.0.1
ToStringMapStringE casts an interface to a map[string]string type.
func ToStringMapStringSlice ¶ added in v1.0.1
ToStringMapStringSlice casts an interface to a map[string][]string type.
func ToStringMapStringSliceE ¶ added in v1.0.1
ToStringMapStringSliceE casts an interface to a map[string][]string type.
func ToStringSlice ¶ added in v1.0.1
func ToStringSlice(i interface{}) []string
ToStringSlice casts an interface to a []string type.
func ToStringSliceE ¶ added in v1.0.1
ToStringSliceE casts an interface to a []string type.
func ToUint ¶ added in v1.0.1
func ToUint(i interface{}) uint
ToUint casts an interface to a uint type.
func ToUint16 ¶ added in v1.0.1
func ToUint16(i interface{}) uint16
ToUint16 casts an interface to a uint16 type.
func ToUint32 ¶ added in v1.0.1
func ToUint32(i interface{}) uint32
ToUint32 casts an interface to a uint32 type.
func ToUint64 ¶ added in v1.0.1
func ToUint64(i interface{}) uint64
ToUint64 casts an interface to a uint64 type.
func ToUint8 ¶ added in v1.0.1
func ToUint8(i interface{}) uint8
ToUint8 casts an interface to a uint8 type.
func Valid ¶
Valid returns true if the input is valid json.
if !gjson.Valid(json) { return errors.New("invalid json") } value := gjson.Get(json, "name.last")
func ValidBytes ¶
ValidBytes returns true if the input is valid json.
if !gjson.Valid(json) { return errors.New("invalid json") } value := gjson.Get(json, "name.last")
If working with bytes, this method preferred over ValidBytes(string(data))
Types ¶
type Any ¶
type Any struct {
// contains filtered or unexported fields
}
func (Any) ToBoolSlice ¶
func (Any) ToDuration ¶
func (Any) ToDurationSlice ¶
func (Any) ToIntSlice ¶
func (Any) ToStringMap ¶
func (Any) ToStringMapBool ¶
func (Any) ToStringMapInt ¶
func (Any) ToStringMapInt64 ¶
func (Any) ToStringMapString ¶
func (Any) ToStringMapStringSlice ¶
func (Any) ToStringSlice ¶
type Result ¶
type Result struct { Any // Type is the json type Type Type // Raw is the raw json Raw string // Str is the json string Str string // Num is the json number Num float64 // Index of raw value in original json, zero means index unknown Index int }
Result represents a json value that is returned from Get().
func Get ¶
Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". When the value is found it's returned immediately.
A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the key. To get the number of elements in an array or to access a child path, use the '#' character. The dot and wildcard character can be escaped with '\'.
{ "name": {"first": "Tom", "last": "Anderson"}, "age":37, "children": ["Sara","Alex","Jack"], "friends": [ {"first": "James", "last": "Murphy"}, {"first": "Roger", "last": "Craig"} ] } "name.last" >> "Anderson" "age" >> 37 "children" >> ["Sara","Alex","Jack"] "children.#" >> 3 "children.1" >> "Alex" "child*.2" >> "Jack" "c?ildren.0" >> "Sara" "friends.#.first" >> ["James","Roger"]
This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.
func GetBytes ¶
GetBytes searches json for the specified path. If working with bytes, this method preferred over Get(string(data), path)
func GetMany ¶
GetMany searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.
func GetManyBytes ¶
GetManyBytes searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.
func Parse ¶
Parse parses the json and returns a result.
This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.
func ParseBytes ¶
ParseBytes parses the json and returns a result. If working with bytes, this method preferred over Parse(string(data))
func (Result) Array ¶
Array returns back an array of values. If the result represents a non-existent value, then an empty array will be returned. If the result is not a JSON array, the return value will be an array containing one result.
func (Result) Exists ¶
Exists returns true if value exists.
if gjson.Get(json, "name.last").Exists(){ println("value exists") }
func (Result) ForEach ¶
ForEach iterates through values. If the result represents a non-existent value, then no values will be iterated. If the result is an Object, the iterator will pass the key and value of each item. If the result is an Array, the iterator will only pass the value of each item. If the result is not a JSON array or object, the iterator will pass back one value equal to the result.
func (Result) Get ¶
Get searches result for the specified path. The result should be a JSON array or object.
func (Result) Less ¶
Less return true if a token is less than another token. The caseSensitive paramater is used when the tokens are Strings. The order when comparing two different type is:
Null < False < Number < String < True < JSON