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 ¶
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 ¶
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 ¶
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 ¶
NewDecoder creates a new decoder for type T.
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.