zjson

package
v1.7.20 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: MIT Imports: 19 Imported by: 19

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

Constants

This section is empty.

Variables

View Source
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": "*/"},
	}
)
View Source
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

func AddModifier(name string, fn func(json, arg string) string)

AddModifier registers a new modifier function with the given name. Modifiers can be used in paths to transform JSON values.

func Delete

func Delete(json, path string) (string, error)

Delete removes a value at the specified path from a JSON string.

func DeleteBytes

func DeleteBytes(json []byte, path string) ([]byte, error)

DeleteBytes removes a value at the specified path from a JSON byte slice.

func Discard

func Discard(json string) (string, error)

Discard removes comments from JSON data.

func ForEachLine

func ForEachLine(json string, fn func(line *Res) bool)

func Format

func Format(json []byte) []byte

Format pretty-prints JSON data with default formatting options.

func FormatOptions

func FormatOptions(json []byte, opts *StFormatOptions) []byte

FormatOptions pretty-prints JSON data with custom formatting options.

func Marshal

func Marshal(json interface{}) ([]byte, error)

Marshal converts a Go value to a JSON byte slice.

func ModifierExists

func ModifierExists(name string) bool

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 Set

func Set(json, path string, value interface{}) (string, error)

Set sets a value at the specified path in a JSON string.

func SetBytes

func SetBytes(json []byte, path string, value interface{}) ([]byte, error)

SetBytes sets a value at the specified path in a JSON byte slice.

func SetBytesOptions

func SetBytesOptions(json []byte, path string, value interface{},
	opts *Options,
) ([]byte, error)

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

func SetOptions(json, path string, value interface{},
	opts *Options,
) (string, error)

SetOptions sets a JSON value at the specified path with custom options. It returns the modified JSON string and any error encountered.

func SetRaw

func SetRaw(json, path, value string) (string, error)

SetRaw sets a raw JSON value at the specified path in a JSON string.

func SetRawBytes

func SetRawBytes(json []byte, path string, value []byte) ([]byte, error)

SetRawBytes sets a raw JSON value at the specified path in a JSON byte slice.

func SetRawBytesOptions

func SetRawBytesOptions(json []byte, path string, value []byte,
	opts *Options,
) ([]byte, error)

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

func SetRawOptions(json, path, value string, opts *Options) (string, error)

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

func Ugly(json []byte) []byte

Ugly removes all whitespace and formatting from JSON data, producing a compact representation.

func Unmarshal

func Unmarshal(json, v interface{}) error

func Valid

func Valid(json string) (ok bool)

func ValidBytes

func ValidBytes(json []byte) bool

Types

type JSONSyntaxError added in v1.7.19

type JSONSyntaxError struct {
	Message  string
	Position int
}

func (*JSONSyntaxError) Error added in v1.7.19

func (e *JSONSyntaxError) Error() string

type Map

type Map map[string]string

Map is a simple string-to-string map type for JSON operations.

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 RepairOptions struct {
	AllowComments       bool
	AllowTrailingCommas bool
	AllowSingleQuotes   bool
	AllowUnquotedKeys   bool
}

type Res

type Res struct {
	// contains filtered or unexported fields
}

func Get

func Get(json, path string) *Res

func GetBytes

func GetBytes(json []byte, path string) *Res

func GetMultiple

func GetMultiple(json string, path ...string) []*Res

func GetMultipleBytes

func GetMultipleBytes(json []byte, path ...string) []*Res

func Parse

func Parse(json string) *Res

func ParseBytes

func ParseBytes(json []byte) *Res

func (*Res) Array

func (r *Res) Array() []*Res

func (*Res) Bool

func (r *Res) Bool(def ...bool) bool

func (*Res) Bytes added in v1.4.0

func (r *Res) Bytes() []byte

func (*Res) Delete added in v1.4.0

func (r *Res) Delete(path string) (err error)

func (*Res) Exists

func (r *Res) Exists() bool

func (*Res) Filter added in v1.4.0

func (r *Res) Filter(fn func(key, value *Res) bool) *Res

Filter returns a new Res containing only the key-value pairs that satisfy the provided filter function.

func (*Res) Float

func (r *Res) Float(def ...float64) float64

func (*Res) Float32 added in v1.7.19

func (r *Res) Float32(def ...float32) float32

func (*Res) Float64 added in v1.7.19

func (r *Res) Float64(def ...float64) float64

func (*Res) ForEach

func (r *Res) ForEach(fn func(key, value *Res) bool)

func (*Res) Get

func (r *Res) Get(path string) *Res

func (*Res) Int

func (r *Res) Int(def ...int) int

func (*Res) Int8 added in v1.7.19

func (r *Res) Int8(def ...int8) int8

func (*Res) Int16 added in v1.7.19

func (r *Res) Int16(def ...int16) int16

func (*Res) Int32 added in v1.7.19

func (r *Res) Int32(def ...int32) int32

func (*Res) Int64 added in v1.7.19

func (r *Res) Int64(def ...int64) int64

func (*Res) IsArray

func (r *Res) IsArray() bool

func (*Res) IsObject

func (r *Res) IsObject() bool

func (*Res) Map

func (r *Res) Map() ztype.Map

func (*Res) MapKeys added in v1.2.0

func (r *Res) MapKeys(exclude ...string) (keys []string)

func (*Res) MapRes added in v1.6.4

func (r *Res) MapRes() map[string]*Res

func (*Res) Maps added in v1.6.4

func (r *Res) Maps() ztype.Maps

func (*Res) MatchKeys added in v1.4.0

func (r *Res) MatchKeys(keys []string) *Res

MatchKeys returns a new Res containing only the key-value pairs where the key matches one of the provided keys.

func (*Res) Raw

func (r *Res) Raw() string

func (*Res) Set added in v1.4.0

func (r *Res) Set(path string, value interface{}) (err error)

func (*Res) Slice added in v1.4.4

func (r *Res) Slice() ztype.SliceType

func (*Res) SliceInt added in v1.7.0

func (r *Res) SliceInt() []int

func (*Res) SliceString added in v1.7.0

func (r *Res) SliceString() []string

func (*Res) String

func (r *Res) String(def ...string) string

func (*Res) Time

func (r *Res) Time(format ...string) (t time.Time)

func (*Res) Uint

func (r *Res) Uint(def ...uint) uint

func (*Res) Uint8 added in v1.7.19

func (r *Res) Uint8(def ...uint8) uint8

func (*Res) Uint16 added in v1.7.19

func (r *Res) Uint16(def ...uint16) uint16

func (*Res) Uint32 added in v1.7.19

func (r *Res) Uint32(def ...uint32) uint32

func (*Res) Uint64 added in v1.7.19

func (r *Res) Uint64(def ...uint64) uint64

func (*Res) Unmarshal

func (r *Res) Unmarshal(v interface{}) error

func (*Res) Value

func (r *Res) Value() interface{}

type StFormatOptions

type StFormatOptions struct {
	Prefix   string // Text to prepend to each line
	Indent   string // Indentation text
	Width    int    // Maximum width of output
	SortKeys bool   // Whether to sort object keys
}

StFormatOptions defines formatting options for JSON output.

type Type

type Type int
const (
	Null Type = iota
	False
	Number
	String
	True
	JSON
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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