xjson

package
v0.0.41 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package xjson extends Go's json in order to make it easier to handle dynamic JSON building/traversal and some other niceties.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DynGet

func DynGet[T any](o Obj, path string) (T, bool, error)

DynGet traverses the given obj using the given path and returns the value (if any) of type T. If traversal fails, like part of the path is not an object, an error is returned. If traversal succeeds but the value type is different an error is returned.

Path is defined using '.' as delimiter like: "key.nested1.nested2.nested3". For the aforementioned path "key", "nested1" and "nested2" MUST be objects, or else traversal will fail and an error is returned. If any of them are objects but traversal can't go on because a key is absent, no error is returned, just false indicating that the data is not there. Once traversal is finished, then "nested3" must match the given type [T].

func Unmarshal

func Unmarshal[T any](v io.Reader) (T, error)

Unmarshal calls json.Unmarshal after reading the given reader into memory and returns the unmarshalled value. If you need more details, like the data that was read when an unmarshalling error happened, you can:

var errDetails UnmarshalError
if errors.As(err, &errDetails) {
    fmt.Println(errDetails.Data)
}

func UnmarshalFile

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

UnmarshalFile calls Unmarshal with the opened file (closing it afterwards) and returns the unmarshalled value. If you need more details, like the data that was read when an unmarshalling error happened, you can:

var errDetails UnmarshalError
if errors.As(err, &errDetails) {
    fmt.Println(errDetails.Data)
}

Types

type Decoder

type Decoder[T any] struct {
	// contains filtered or unexported fields
}

Decoder specializes the json.Decoder for streams of objects of the same type (although you can create a Decoder with type Obj, then it is dynamic), leveraging parametric types and iterators to make things easier. It won't cover all possible scenarios by design, for that you can use Go's json.Decoder.

func NewDecoder

func NewDecoder[T any](r io.Reader) *Decoder[T]

NewDecoder creates a new decoder for type T.

func (*Decoder[T]) All

func (d *Decoder[T]) All() iter.Seq[T]

All returns a single-use iterator for the stream.

func (*Decoder[T]) Error

func (d *Decoder[T]) Error() error

Error returns the error that interrupted iteration or nil if no error happened.

type Obj

type Obj = map[string]any

Obj represents a dynamic JSON object. Use DynGet to manipulate it more easily.

type UnmarshalError

type UnmarshalError struct {
	// Err is the unmarshalling error (returned by [json.Unmarshal].
	Err error
	// Data is the data that caused the unmarshalling error, useful for debugging.
	Data string
}

UnmarshalError is returned by Unmarshal when an unmarshalling error happens.

func (UnmarshalError) Error

func (e UnmarshalError) Error() string

Jump to

Keyboard shortcuts

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