Documentation ¶
Index ¶
- Variables
- type Builder
- func (s *Builder) Delete(path string) error
- func (s *Builder) Get(path string) Result
- func (s *Builder) GetMany(path ...string) []Result
- func (s *Builder) Interface() (v interface{})
- func (s *Builder) JSONArray() *JSONArray
- func (s *Builder) JSONObject() *JSONObject
- func (s *Builder) Set(path string, value interface{}) error
- func (s *Builder) SetRaw(path, value string) error
- func (s *Builder) String() string
- type JSONArray
- type JSONObject
- type JSONResult
- func (s *JSONResult) Code() int
- func (s *JSONResult) Data() interface{}
- func (s *JSONResult) DataMap() interface{}
- func (s *JSONResult) Fail(format string, v ...interface{}) (err error)
- func (s *JSONResult) Message() string
- func (s *JSONResult) Set(key string, value interface{}) *JSONResult
- func (s *JSONResult) SetCode(code int) *JSONResult
- func (s *JSONResult) SetData(d interface{}) *JSONResult
- func (s *JSONResult) SetMessage(format string, msg ...interface{}) *JSONResult
- func (s *JSONResult) Success(d ...interface{}) (err error)
- func (s *JSONResult) ToJSON() []byte
- func (s *JSONResult) WriteTo(dst io.Writer) (err error)
- func (s *JSONResult) WriteToCtx(ctx gcore.Ctx) (err error)
- type Options
- type Result
Constants ¶
This section is empty.
Variables ¶
var ( // 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. AddModifier = gjson.AddModifier // ModifierExists returns true when the specified modifier exists. ModifierExists = gjson.ModifierExists // Escape returns an escaped path component. // // json := `{ // "user":{ // "first.name": "Janet", // "last.name": "Prichard" // } // }` // user := gjson.Get(json, "user") // println(user.Get(gjson.Escape("first.name")) // println(user.Get(gjson.Escape("last.name")) // // Output: // // Janet // // Prichard Escape = gjson.Escape // ForEachLine iterates through lines of JSON as specified by the JSON Lines // format (http://jsonlines.org/). // Each line is returned as a GJSON Result. ForEachLine = gjson.ForEachLine // 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. Parse = func(json string) Result { r := gjson.Parse(json) return Result{ Result: r, path: r.Path(json), paths: r.Paths(json), } } // ParseBytes parses the json and returns a result. // If working with bytes, this method preferred over Parse(string(data)) ParseBytes = gjson.ParseBytes // Valid returns true if the input is valid json. // // if !gjson.Valid(json) { // return errors.New("invalid json") // } // value := gjson.Get(json, "name.last") Valid = gjson.Valid // 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)) ValidBytes = gjson.ValidBytes )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
func NewBuilder ¶
func NewBuilder(v interface{}) *Builder
func NewBuilderE ¶
func (*Builder) Delete ¶
Delete deletes a value from json for the specified path. path syntax: https://github.com/tidwall/sjson?tab=readme-ov-file#path-syntax
func (*Builder) 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. path syntax: https://github.com/tidwall/gjson/blob/master/SYNTAX.md
func (*Builder) 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 (*Builder) Interface ¶
func (s *Builder) Interface() (v interface{})
Interface convert the *Builder to Go DATA,
func (*Builder) JSONArray ¶
JSONArray convert the *Builder to *JSONArray, if the *Builder is not a json array, nil returned.
func (*Builder) JSONObject ¶
func (s *Builder) JSONObject() *JSONObject
JSONObject convert the *Builder to *JSONObject, if the *Builder is not a json object, nil returned.
func (*Builder) Set ¶
Set sets a json value for the specified path. A path is in dot syntax, such as "name.last" or "age". 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. An error is returned if the path is not valid.
A path is a series of keys separated by a dot.
{ "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.1" >> "Alex"
path syntax: https://github.com/tidwall/sjson?tab=readme-ov-file#path-syntax
type JSONArray ¶
type JSONArray struct {
*Builder
}
func NewJSONArray ¶
func NewJSONArray(v interface{}) *JSONArray
NewJSONArray create a *JSONArray form v, if error occurred nil returned. v can be json array content of []byte and string, or any data which json.Marshal can be processed.
func NewJSONArrayE ¶
NewJSONArrayE create a *JSONArray form v, returned error(if have) v can be json array content of []byte and string, or any data which json.Marshal can be processed.
type JSONObject ¶
type JSONObject struct {
*Builder
}
func NewJSONObject ¶
func NewJSONObject(v interface{}) *JSONObject
NewJSONObject create a *JSONObject form v, if error occurred nil returned. v can be json object content of []byte and string, or any data which json.Marshal can be processed.
func NewJSONObjectE ¶
func NewJSONObjectE(v interface{}) (*JSONObject, error)
NewJSONObjectE create a *JSONObject form v, returned error(if have) v can be json object content of []byte and string, or any data which json.Marshal can be processed.
type JSONResult ¶
type JSONResult struct {
// contains filtered or unexported fields
}
func NewResult ¶
func NewResult(d ...interface{}) *JSONResult
NewResult Optional args: code int, message string, data interface{}
func NewResultCtx ¶
func NewResultCtx(ctx gcore.Ctx, d ...interface{}) *JSONResult
NewResultCtx Optional args: code int, message string, data interface{}
func (*JSONResult) Code ¶
func (s *JSONResult) Code() int
func (*JSONResult) Data ¶
func (s *JSONResult) Data() interface{}
func (*JSONResult) DataMap ¶
func (s *JSONResult) DataMap() interface{}
func (*JSONResult) Fail ¶
func (s *JSONResult) Fail(format string, v ...interface{}) (err error)
Fail only worked with NewResultCtx()
func (*JSONResult) Message ¶
func (s *JSONResult) Message() string
func (*JSONResult) Set ¶
func (s *JSONResult) Set(key string, value interface{}) *JSONResult
func (*JSONResult) SetCode ¶
func (s *JSONResult) SetCode(code int) *JSONResult
func (*JSONResult) SetData ¶
func (s *JSONResult) SetData(d interface{}) *JSONResult
func (*JSONResult) SetMessage ¶
func (s *JSONResult) SetMessage(format string, msg ...interface{}) *JSONResult
func (*JSONResult) Success ¶
func (s *JSONResult) Success(d ...interface{}) (err error)
Success only worked with NewResultCtx()
func (*JSONResult) ToJSON ¶
func (s *JSONResult) ToJSON() []byte
func (*JSONResult) WriteToCtx ¶
func (s *JSONResult) WriteToCtx(ctx gcore.Ctx) (err error)
type Result ¶
func (Result) ToJSONArray ¶
func (Result) ToJSONObject ¶
func (s Result) ToJSONObject() *JSONObject