Documentation ¶
Overview ¶
Package jp provides pointers for json strings.
Index ¶
- func Valid[T Stringlike](json T) bool
- type Iterator
- type Result
- func (t Result[T]) Array() []Result[T]
- func (t Result[T]) Bool() bool
- func (t Result[T]) Exists() bool
- func (t Result[T]) Float() float64
- func (t Result[T]) ForEach(iterator func(key, value Result[T]) bool)
- func (t Result[T]) Get(path string) Result[T]
- func (t Result[T]) Int() int64
- func (t Result[T]) IsArray() bool
- func (t Result[T]) IsBool() bool
- func (t Result[T]) IsObject() bool
- func (t Result[T]) Len() int
- func (t Result[T]) Less(token Result[T], caseSensitive bool) bool
- func (t Result[T]) Map() map[string]Result[T]
- func (t Result[T]) Range() *Iterator[T]
- func (t Result[T]) String() string
- func (t Result[T]) Time() time.Time
- func (t Result[T]) Uint() uint64
- func (t Result[T]) Value() interface{}
- type Stringlike
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Valid ¶
func Valid[T Stringlike](json T) bool
Valid returns true if the input is valid json.
if !jp.Valid(json) { return errors.New("invalid json") } value := jp.Get(json, "name.last")
Types ¶
type Iterator ¶
type Iterator[T Stringlike] struct { // contains filtered or unexported fields }
type Result ¶
type Result[T Stringlike] struct { // Type is the json type Type Type // Raw is the raw json Raw T // Str is the json string Str string // Num is the json number Num float64 // Index of raw value in original json, zero means index unknown Index int // contains filtered or unexported fields }
Result represents a json value that is returned from Get().
func Get ¶
func Get[T Stringlike](json T, pointer string) Result[T]
Get searches json for the specified RFC 6901 JSON pointer.
This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.
func GetMany ¶
func GetMany[T Stringlike](json T, path ...string) []Result[T]
GetMany searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.
func Parse ¶
func Parse[T Stringlike](json T) Result[T]
Parse parses the json and returns a result.
This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.
func (Result[T]) Array ¶
Array returns back an array of values. If the result represents a null value or is non-existent, then an empty array will be returned. If the result is not a JSON array, the return value will be an array containing one result.
func (Result[T]) Exists ¶
Exists returns true if value exists.
if jp.Get(json, "name.last").Exists(){ println("value exists") }
func (Result[T]) ForEach ¶
ForEach iterates through values. If the result represents a non-existent value, then no values will be iterated. If the result is an Object, the iterator will pass the key and value of each item. If the result is an Array, the iterator will pass the index and value of each item.
func (Result[T]) Get ¶
Get searches result for the specified path. The result should be a JSON array or object.
func (Result[T]) Less ¶
Less return true if a token is less than another token. The caseSensitive paramater is used when the tokens are Strings. The order when comparing two different type is:
Null < False < Number < String < True < JSON
func (Result[T]) Map ¶
Map returns back a map of values. The result should be a JSON object. If the result is not a JSON object, the return value will be an empty map.