Documentation
¶
Index ¶
- func MGet(text string, paths ...string) []gjson.Result
- func MGetBytes(bytes []byte, paths ...string) []gjson.Result
- func MarshalToString(v any) (string, error)
- func Set(text, path string, value any, opts ...Option) (string, error)
- func SetBytes(bytes []byte, path string, value any, opts ...Option) ([]byte, error)
- func SetRaw(text, path, value string, opts ...Option) (string, error)
- func SetRawBytes(bytes []byte, path string, value []byte, opts ...Option) ([]byte, error)
- func UnmarshalFromString(data string, v any) error
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MGet ¶
MGet searches JSON text 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 MGetBytes ¶
MGetBytes searches JSON bytes 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. If working with bytes, this method preferred over MGet(string(bytes), paths)
func MarshalToString ¶
MarshalToString returns the JSON-encoded string of v.
The implementation streams the encoder output directly into a strings.Builder, avoiding the intermediate []byte allocation and extra copy that a naive json.Marshal + string(buf) would perform.
Note: encoding/json.Encoder.Encode appends a trailing newline to its output. We strip that so the returned string matches what json.Marshal would have produced.
func Set ¶
Set sets a JSON value for the specified path with options. 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 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"
func SetBytes ¶
SetBytes sets a JSON value for the specified path with options. If working with bytes, this method preferred over Set(string(bytes), path, value, opts...)
func SetRaw ¶
SetRaw sets a raw JSON value for the specified path with options. This function works the same as Set except that the value is set as a raw block of JSON. This allows for setting pre-marshalled JSON objects.
func SetRawBytes ¶
SetRawBytes sets a raw JSON value for the specified path with options. If working with bytes, this method preferred over SetRaw(string(bytes), path, string(value), opts...)
func UnmarshalFromString ¶
UnmarshalFromString parses the JSON-encoded string and stores the result in the value pointed to by v.
v must be a non-nil pointer; see encoding/json.Unmarshal for the full contract. Passing a non-pointer or a nil pointer returns an *json.InvalidUnmarshalError.
Types ¶
type Option ¶
Option represents additional options for the Set functions.
func BeOptimistic ¶
func BeOptimistic() Option
BeOptimistic sets a hint that the value likely exists which allows for the JSON to perform a fast-track search and replace.
func DoReplaceInPlace ¶
func DoReplaceInPlace() Option
DoReplaceInPlace sets a hint to replace the input JSON rather than allocate a new JSON byte slice. When this field is specified the input JSON will no longer be valid, and it should not be used In the case when the destination slice doesn't have enough free bytes to replace the data in place, a new bytes slice will be created under the hood. This option automatically enables the BeOptimistic option. The input must be a byte slice in order to use this field.