Documentation
¶
Overview ¶
Extends the Go runtime's json.Decoder enabling navigation of a stream of json tokens.
Index ¶
Examples ¶
Constants ¶
const AnyIndex = -2
AnyIndex can be used in a pattern to match any array index.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DecodeAction ¶
DecodeAction handlers are called by the Decoder when scanning objects. See PathActions.Add for more detail.
type Decoder ¶
Decoder extends the Go runtime's encoding/json.Decoder to support navigating in a stream of JSON tokens.
func NewDecoder ¶
NewDecoder creates a new instance of the extended JSON Decoder.
func (*Decoder) Decode ¶
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 (*Decoder) Path ¶
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 (*Decoder) Scan ¶
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 ¶
var j = []byte(`{"colors":[ {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10, "A": 58}}, {"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255, "A": 231}} ]}`) var actions PathActions // Extract the value at Point.A actions.Add(func(d *Decoder) error { var alpha int err := d.Decode(&alpha) fmt.Printf("Alpha: %v\n", alpha) return err }, "Point", "A") w := NewDecoder(bytes.NewReader(j)) w.SeekTo("colors", 0) var ok = true var err error for ok { ok, err = w.Scan(&actions) if err != nil && err != io.EOF { panic(err) } }
Output: Alpha: 58 Alpha: 231
Example (AnyIndex) ¶
var j = []byte(`{"colors":[ {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10, "A": 58}}, {"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255, "A": 231}} ]}`) var actions PathActions // Extract the value at Point.A actions.Add(func(d *Decoder) error { var cr int err := d.Decode(&cr) fmt.Printf("Chrominance (Cr): %v\n", cr) return err }, "colors", AnyIndex, "Point", "Cr") actions.Add(func(d *Decoder) error { var r int err := d.Decode(&r) fmt.Printf("Red: %v\n", r) return err }, "colors", AnyIndex, "Point", "R") w := NewDecoder(bytes.NewReader(j)) var ok = true var err error for ok { ok, err = w.Scan(&actions) if err != nil && err != io.EOF { panic(err) } }
Output: Chrominance (Cr): -10 Red: 98
func (*Decoder) SeekTo ¶
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 ¶
var j = []byte(`[ {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}}, {"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}} ]`) w := NewDecoder(bytes.NewReader(j)) var v interface{} w.SeekTo(0, "Space") w.Decode(&v) fmt.Printf("%v => %v\n", w.Path(), v) w.SeekTo(0, "Point", "Cr") w.Decode(&v) fmt.Printf("%v => %v\n", w.Path(), v) w.SeekTo(1, "Point", "G") w.Decode(&v) fmt.Printf("%v => %v\n", w.Path(), v) // seek to the end of the object w.SeekTo() fmt.Printf("%v\n", w.Path())
Output: [0 Space] => YCbCr [0 Point Cr] => -10 [1 Point G] => 218 []
type JsonPath ¶
type JsonPath []interface{}
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 KeyString ¶
type KeyString string
KeyString is returned from Decoder.Token to represent each key in a JSON object value.
type PathActions ¶
type PathActions struct {
// contains filtered or unexported fields
}
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.
func (*PathActions) Add ¶
func (je *PathActions) Add(action DecodeAction, path ...interface{})
Add specifies an action to call on the Decoder when the specified path is encountered.