Documentation
¶
Overview ¶
Package zjson provides fast and flexible JSON manipulation functions. It offers path-based operations for getting, setting, and modifying JSON data without the need for intermediate unmarshaling and marshaling.
Index ¶
- Variables
- func AddModifier(name string, fn func(json, arg string) string)
- func Delete(json, path string) (string, error)
- func DeleteBytes(json []byte, path string) ([]byte, error)
- func Discard(json string) (string, error)
- func ForEachLine(json string, fn func(line *Res) bool)
- func Format(json []byte) []byte
- func FormatOptions(json []byte, opts *StFormatOptions) []byte
- func Marshal(json interface{}) ([]byte, error)
- func ModifierExists(name string) bool
- func ModifiersState() bool
- func Repair(src string, opt ...func(*RepairOptions)) (dst string, err error)
- func Set(json, path string, value interface{}) (string, error)
- func SetBytes(json []byte, path string, value interface{}) ([]byte, error)
- func SetBytesOptions(json []byte, path string, value interface{}, opts *Options) ([]byte, error)
- func SetModifiersState(b bool)
- func SetOptions(json, path string, value interface{}, opts *Options) (string, error)
- func SetRaw(json, path, value string) (string, error)
- func SetRawBytes(json []byte, path string, value []byte) ([]byte, error)
- func SetRawBytesOptions(json []byte, path string, value []byte, opts *Options) ([]byte, error)
- func SetRawOptions(json, path, value string, opts *Options) (string, error)
- func Stringify(value interface{}) (json string)
- func Ugly(json []byte) []byte
- func Unmarshal(json, v interface{}) error
- func Valid(json string) (ok bool)
- func ValidBytes(json []byte) bool
- type JSONSyntaxError
- type Map
- type Options
- type RepairOptions
- type Res
- func (r *Res) Array() []*Res
- func (r *Res) Bool(def ...bool) bool
- func (r *Res) Bytes() []byte
- func (r *Res) Delete(path string) (err error)
- func (r *Res) Exists() bool
- func (r *Res) Filter(fn func(key, value *Res) bool) *Res
- func (r *Res) Float(def ...float64) float64
- func (r *Res) Float32(def ...float32) float32
- func (r *Res) Float64(def ...float64) float64
- func (r *Res) ForEach(fn func(key, value *Res) bool)
- func (r *Res) Get(path string) *Res
- func (r *Res) Int(def ...int) int
- func (r *Res) Int8(def ...int8) int8
- func (r *Res) Int16(def ...int16) int16
- func (r *Res) Int32(def ...int32) int32
- func (r *Res) Int64(def ...int64) int64
- func (r *Res) IsArray() bool
- func (r *Res) IsObject() bool
- func (r *Res) Map() ztype.Map
- func (r *Res) MapKeys(exclude ...string) (keys []string)
- func (r *Res) MapRes() map[string]*Res
- func (r *Res) Maps() ztype.Maps
- func (r *Res) MatchKeys(keys []string) *Res
- func (r *Res) Raw() string
- func (r *Res) Set(path string, value interface{}) (err error)
- func (r *Res) Slice() ztype.SliceType
- func (r *Res) SliceInt() []int
- func (r *Res) SliceString() []string
- func (r *Res) String(def ...string) string
- func (r *Res) Time(format ...string) (t time.Time)
- func (r *Res) Uint(def ...uint) uint
- func (r *Res) Uint8(def ...uint8) uint8
- func (r *Res) Uint16(def ...uint16) uint16
- func (r *Res) Uint32(def ...uint32) uint32
- func (r *Res) Uint64(def ...uint64) uint64
- func (r *Res) Unmarshal(v interface{}) error
- func (r *Res) Value() interface{}
- type StFormatOptions
- type Type
Constants ¶
This section is empty.
Variables ¶
var ( // DefOptions defines the default formatting options for JSON output. DefOptions = &StFormatOptions{Width: 80, Prefix: "", Indent: " ", SortKeys: false} // Matches defines patterns for comments to be discarded during JSON processing. Matches = []Map{ {"start": "//", "end": "\n"}, {"start": "/*", "end": "*/"}, } )
var ( // ErrNoChange is returned when an operation doesn't modify the JSON ErrNoChange = errors.New("no change") // ErrPathEmpty is returned when an empty path is provided ErrPathEmpty = errors.New("path cannot be empty") // ErrInvalidJSON is returned when the input is not valid JSON ErrInvalidJSON = errors.New("invalid json") // ErrNotAllowedWildcard is returned when a wildcard is used in a path where not allowed ErrNotAllowedWildcard = errors.New("wildcard characters not allowed in path") // ErrNotAllowedArrayAccess is returned when array access is used in a path where not allowed ErrNotAllowedArrayAccess = errors.New("array access character not allowed in path") // ErrTypeError is returned when the JSON value is not of the expected type ErrTypeError = errors.New("json must be an object or array") )
Error definitions for common JSON operations
Functions ¶
func AddModifier ¶
AddModifier registers a new modifier function with the given name. Modifiers can be used in paths to transform JSON values.
func DeleteBytes ¶
DeleteBytes removes a value at the specified path from a JSON byte slice.
func ForEachLine ¶
func FormatOptions ¶
func FormatOptions(json []byte, opts *StFormatOptions) []byte
FormatOptions pretty-prints JSON data with custom formatting options.
func ModifierExists ¶
ModifierExists checks if a modifier with the given name exists.
func ModifiersState ¶
func ModifiersState() bool
func Repair ¶ added in v1.7.19
func Repair(src string, opt ...func(*RepairOptions)) (dst string, err error)
func SetBytesOptions ¶
SetBytesOptions sets a JSON value at the specified path with custom options. It works directly with byte slices for better performance.
func SetModifiersState ¶
func SetModifiersState(b bool)
func SetOptions ¶
SetOptions sets a JSON value at the specified path with custom options. It returns the modified JSON string and any error encountered.
func SetRawBytes ¶
SetRawBytes sets a raw JSON value at the specified path in a JSON byte slice.
func SetRawBytesOptions ¶
SetRawBytesOptions sets a raw JSON value at the specified path in a JSON byte slice with custom options. It accepts raw JSON bytes for both the target JSON and the value to be set.
func SetRawOptions ¶
SetRawOptions sets a raw JSON value at the specified path with custom options.
func Stringify ¶
func Stringify(value interface{}) (json string)
Stringify converts any Go value to its JSON string representation.
func Ugly ¶
Ugly removes all whitespace and formatting from JSON data, producing a compact representation.
func ValidBytes ¶
Types ¶
type JSONSyntaxError ¶ added in v1.7.19
func (*JSONSyntaxError) Error ¶ added in v1.7.19
func (e *JSONSyntaxError) Error() string
type Options ¶ added in v1.1.13
type Options struct {
// Optimistic enables optimistic path processing.
Optimistic bool
// ReplaceInPlace modifies the JSON in place without reallocation when possible.
ReplaceInPlace bool
}
Options provides configuration for JSON operations.
type RepairOptions ¶ added in v1.7.19
type Res ¶
type Res struct {
// contains filtered or unexported fields
}
func GetMultiple ¶
func GetMultipleBytes ¶
func ParseBytes ¶
func (*Res) Filter ¶ added in v1.4.0
Filter returns a new Res containing only the key-value pairs that satisfy the provided filter function.
func (*Res) MatchKeys ¶ added in v1.4.0
MatchKeys returns a new Res containing only the key-value pairs where the key matches one of the provided keys.