jsonx

package
v1.0.75 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultIndent = "  "
	Extension     = ".json"
)

Variables

This section is empty.

Functions

func ExtractJsonFieldNames

func ExtractJsonFieldNames(v any) []string

ExtractJsonFieldNames extracts the JSON field names from the given value

func FromBytes

func FromBytes[T any](b []byte) (T, error)

FromBytes decodes JSON from a byte slice into type [T]

func FromFile

func FromFile[T any](path string) (T, error)

FromFile reads and decodes JSON from a file at the path into type [T]

func FromJSON

func FromJSON[T any](r io.Reader) (T, error)

FromJSON decodes JSON from the input reader into type [T]

func HandleEntity

func HandleEntity(data []byte, entity Entity) error

HandleEntity parses the given JSON raw bytes using the handlers, according to its detected value type

func HandleEntityValue

func HandleEntityValue(value any, entity Entity)

HandleEntityValue parses the given JSON marshaled value using the handlers, according to its detected value type

func HandlePrimitive

func HandlePrimitive(data []byte, primitive Primitive) error

HandlePrimitive parses the given JSON raw bytes using the handlers, according to its detected value type

func HandlePrimitiveValue

func HandlePrimitiveValue(value any, primitive Primitive)

HandlePrimitiveValue parses the given JSON marshaled value using the handlers, according to its detected value type

func String

func String(value any) string

String returns the JSON marshaled value as a string

func StructToMap

func StructToMap(v any) (map[string]any, error)

StructToMap efficiently converts a struct to map[string]any using sonic

func ToBytes

func ToBytes[T any](item T, opts ...Options) ([]byte, error)

ToBytes encodes the item to JSON and returns it as a byte slice with optional formatting options

func ToFile

func ToFile[T any](item T, path string, opts ...Options) error

ToFile encodes the item to JSON and writes it to a file at the path with optional formatting options

func ToJSON

func ToJSON[T any](item T, w io.Writer, opts ...Options) error

ToJSON encodes the item to JSON and writes it to w with optional formatting options

Types

type Entity

type Entity interface {
	// OnFloat Hook called when a float value is detected
	OnFloat(floatValue float64)
	// OnString Hook called when a string value is detected
	OnString(stringValue string)
	// OnBool Hook called when a boolean value is detected
	OnBool(boolValue bool)
	// OnNull Hook called when a null value is detected
	OnNull()
	// OnArray Hook called when an array value is detected
	OnArray(arrayValue []any)
	// OnObject Hook called when an object value is detected
	OnObject(objectValue map[string]any)
}

Entity interface of an unknown JSON value

type Options

type Options struct {
	Pretty bool
	Prefix string
	Indent string
}

Options for JSON encoding and decoding

type Primitive

type Primitive interface {
	// OnValue Hook called when a primitive value is detected
	OnValue(value any)
	// OnNull Hook called when a null value is detected
	OnNull()
	// OnComplex Hook called when a object or array value is detected
	OnComplex(complex any)
}

Primitive interface of a JSON primitive value

Jump to

Keyboard shortcuts

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