Documentation ¶
Overview ¶
Package tyson provides a wrapper for navigating hierarchies of unknown or dynamic JSON objects parsed into map[string]any. It aims to hide the messy type and existence checks that usually occur when dealing with such structures.
Example (Default_values) ¶
This example explains how to return a default value for target values that either don't exist or have a wrong type.
package main import ( "encoding/json" "fmt" "github.com/deep-rent/tyson" ) func main() { var o tyson.Object _ = json.Unmarshal([]byte(`{ "num": 12.34 }`), &o) xyz := o.Get("xyz") str := o.GetString("num") fmt.Printf(`key "xyz" does not exist: %t`+"\n", xyz.Empty()) fmt.Printf(`val "num" isn't a string: %t`+"\n", str.Empty()) fmt.Printf("xyz: %v\n", xyz.Or("def")) fmt.Printf("str: %s\n", str.OrGet(func() string { return "def" })) }
Output: key "xyz" does not exist: true val "num" isn't a string: true xyz: def str: def
Example (Type_getters) ¶
This example showcases the retrieval of various types from a parsed JSON object.
package main import ( "encoding/json" "fmt" "github.com/deep-rent/tyson" ) func main() { var o tyson.Object _ = json.Unmarshal([]byte(` { "obj": { "num": 12.34, "int": 12345, "str": "abc" }, "arr": [true, false] } `), &o) fmt.Printf("num: %.2f\n", o.GetFloat("obj", "num").Value()) fmt.Printf("int: %d\n", o.GetInt("obj", "int").Value()) fmt.Printf("str: %q\n", o.GetString("obj", "str").Value()) fmt.Printf("arr: %v\n", o.GetBools("arr").Value()) }
Output: num: 12.34 int: 12345 str: "abc" arr: [true false]
Index ¶
- func AsArray(v any) (w []any, ok bool)
- func AsBool(v any) (w bool, ok bool)
- func AsFloat(v any) (w float64, ok bool)
- func AsInt(v float64) (w int64, ok bool)
- func AsString(v any) (w string, ok bool)
- type Mapper
- type Node
- type Object
- func (o Object) Get(keys ...string) Node[any]
- func (o Object) GetArray(keys ...string) Node[[]any]
- func (o Object) GetArrays(keys ...string) Node[[][]any]
- func (o Object) GetBool(keys ...string) Node[bool]
- func (o Object) GetBools(keys ...string) Node[[]bool]
- func (o Object) GetFloat(keys ...string) Node[float64]
- func (o Object) GetFloats(keys ...string) Node[[]float64]
- func (o Object) GetInt(keys ...string) Node[int64]
- func (o Object) GetInts(keys ...string) Node[[]int64]
- func (o Object) GetObject(keys ...string) Node[Object]
- func (o Object) GetObjects(keys ...string) Node[[]Object]
- func (o Object) GetString(keys ...string) Node[string]
- func (o Object) GetStrings(keys ...string) Node[[]string]
- func (o Object) Has(k string) bool
- func (o Object) Remove(k string)
- func (o Object) Set(k string, v any)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Mapper ¶
A Mapper converts type S into type T. If the conversion succeeded, ok will be true; if not, ok will be false. This is essentially a generic abstraction of the "comma ok" idiom used in Go type casts.
func All ¶
All "lifts" m to convert a slice of item type S into a slice of item type T. The resulting Mapper indicates ok if and only if m was successfully applied to each element of the input slice.
type Node ¶
type Node[T any] interface { // Value returns the contained value if present, or else the // zero value of T. Value() T // Empty returns true if no value is present, otherwise false. Empty() bool // Or returns the contained value if present, otherwise v. Or(v T) T // OrGet returns the contained value if present, or else the // result produced by f. OrGet(f func() T) T }
A Node is a container which may or may not contain a value of type T. If a value is present, Empty returns false and Value will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as Or which returns a default value if this Node is empty.
Use EmptyNode or ValueNode to create empty or nonempty Nodes respectively.
type Object ¶
An Object represents the result of parsing an unknown or dynamic JSON object. Provided getters can be used to navigate the object hierarchy in a type-safe manner.
func (Object) Get ¶
Get follows the given hierarchy of keys to locate a target value within the underlying JSON structure. The returned Node is empty if some key does not exist, or else contains the target value. If no key is passed, the returned Node contains this Object.
The following example fetches the nested value of "c" from the parsed JSON object:
var o tyson.Object _ = json.Unmarshal([]byte(`{"a":{"b":{"c":"d"}}}`), &o) fmt.Print(o.Get("a", "b", "c").Value()) // prints "d"
func (Object) GetArray ¶
GetArray follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON array. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetArrays ¶
GetArrays follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a two-dimensional JSON array. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetBool ¶
GetBool follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON boolean. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetBools ¶
GetBools follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON array of only booleans. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetFloat ¶
GetFloat follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON number. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetFloats ¶
GetFloats follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON array of only numbers. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetInt ¶
GetInt follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not an integral JSON number. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetInts ¶
GetInts follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON array of only integral numbers. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetObject ¶
GetObject follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON object. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetObjects ¶
GetObjects follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON array of only objects. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetString ¶
GetString follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON string. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.
func (Object) GetStrings ¶
GetStrings follows the given hierarchy of keys to fetch a target value from the underlying JSON structure. The returned Node is empty if some key does not exist, or if the target value is not a JSON array of only strings. Otherwise, the returned Node contains the target value.
See Object.Get for an example on how to fetch nested JSON values.