gjson

package
v0.0.0-...-8506bae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 NewBuilderE(v interface{}) (*Builder, error)

func (*Builder) Delete

func (s *Builder) Delete(path string) error

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

func (s *Builder) Get(path string) Result

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

func (s *Builder) GetMany(path ...string) []Result

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

func (s *Builder) JSONArray() *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

func (s *Builder) Set(path string, value interface{}) error

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

func (*Builder) SetRaw

func (s *Builder) SetRaw(path, value string) error

SetRaw sets a raw json value for the specified path. This function works the same as Set except that the value is set as a raw block of json. This allows for setting premarshalled json objects.

func (*Builder) String

func (s *Builder) String() string

String convert the *Builder to JSON string,

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

func NewJSONArrayE(v interface{}) (*JSONArray, error)

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.

func (*JSONArray) Append

func (s *JSONArray) Append(values ...interface{}) (err error)

func (*JSONArray) First

func (s *JSONArray) First() Result

func (*JSONArray) Last

func (s *JSONArray) Last() Result

func (*JSONArray) Len

func (s *JSONArray) Len() int64

func (*JSONArray) Merge

func (s *JSONArray) Merge(arr interface{}) (err error)

Merge *JSONArray, JSONArray or any valid slice to s

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) WriteTo

func (s *JSONResult) WriteTo(dst io.Writer) (err error)

func (*JSONResult) WriteToCtx

func (s *JSONResult) WriteToCtx(ctx gcore.Ctx) (err error)

type Options

type Options = sjson.Options

type Result

type Result struct {
	gjson.Result
	// contains filtered or unexported fields
}

func (Result) ForEach

func (s Result) ForEach(f func(k, v Result) bool)

func (Result) Path

func (s Result) Path() string

func (Result) Paths

func (s Result) Paths() []string

func (Result) ToJSONArray

func (s Result) ToJSONArray() *JSONArray

func (Result) ToJSONObject

func (s Result) ToJSONObject() *JSONObject

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL