AnyIndex can be used in a pattern to match any array index.
const AnyIndex = -2
DecodeAction handlers are called by the Decoder when scanning objects. See PathActions.Add for more detail.
type DecodeAction func(d *Decoder) error
Decoder extends the Go runtime's encoding/json.Decoder to support navigating in a stream of JSON tokens.
type Decoder struct { json.Decoder // contains filtered or unexported fields }
func NewDecoder(r io.Reader) *Decoder
NewDecoder creates a new instance of the extended JSON Decoder.
func (d *Decoder) Decode(v interface{}) error
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v. This is equivalent to encoding/json.Decode().
func (d *Decoder) Path() JsonPath
Path returns a slice of string and/or int values representing the path from the root of the JSON object to the position of the most-recently parsed token.
func (d *Decoder) Scan(ext *PathActions) (bool, error)
Scan moves forward over the JSON stream consuming all the tokens at the current level (current object, current array) invoking each matching PathAction along the way.
Scan returns true if there are more contiguous values to scan (for example in an array).
▹ Example
▹ Example (AnyIndex)
func (d *Decoder) SeekTo(path ...interface{}) (bool, error)
SeekTo causes the Decoder to move forward to a given path in the JSON structure.
The path argument must consist of strings or integers. Each string specifies an JSON object key, and each integer specifies an index into a JSON array.
Consider the JSON structure
{ "a": [0,"s",12e4,{"b":0,"v":35} ] }
SeekTo("a",3,"v") will move to the value referenced by the "a" key in the current object, followed by a move to the 4th value (index 3) in the array, followed by a move to the value at key "v". In this example, a subsequent call to the decoder's Decode() would unmarshal the value 35.
SeekTo returns a boolean value indicating whether a match was found.
Decoder is intended to be used with a stream of tokens. As a result it navigates forward only.
▹ Example
func (d *Decoder) Token() (json.Token, error)
Token is equivalent to the Token() method on json.Decoder. The primary difference is that it distinguishes between strings that are keys and and strings that are values. String tokens that are object keys are returned as a KeyString rather than as a native string.
JsonPath is a slice of strings and/or integers. Each string specifies an JSON object key, and each integer specifies an index into a JSON array.
type JsonPath []interface{}
func (p *JsonPath) Equal(o JsonPath) bool
Equal tests for equality between two JsonPath types.
func (p *JsonPath) HasPrefix(o JsonPath) bool
KeyString is returned from Decoder.Token to represent each key in a JSON object value.
type KeyString string
PathActions represents a collection of DecodeAction functions that should be called at certain path positions when scanning the JSON stream. PathActions can be created once and used many times in one or more JSON streams.
type PathActions struct {
// contains filtered or unexported fields
}
func (je *PathActions) Add(action DecodeAction, path ...interface{})
Add specifies an action to call on the Decoder when the specified path is encountered.