...

Text file src/github.com/exponent-io/jsonpath/README.md

Documentation: github.com/exponent-io/jsonpath

     1[![GoDoc](https://godoc.org/github.com/exponent-io/jsonpath?status.svg)](https://godoc.org/github.com/exponent-io/jsonpath)
     2[![Build Status](https://travis-ci.org/exponent-io/jsonpath.svg?branch=master)](https://travis-ci.org/exponent-io/jsonpath)
     3
     4# jsonpath
     5
     6This package extends the [json.Decoder](https://golang.org/pkg/encoding/json/#Decoder) to support navigating a stream of JSON tokens. You should be able to use this extended Decoder places where a json.Decoder would have been used.
     7
     8This Decoder has the following enhancements...
     9 * The [Scan](https://godoc.org/github.com/exponent-io/jsonpath/#Decoder.Scan) method supports scanning a JSON stream while extracting particular values along the way using [PathActions](https://godoc.org/github.com/exponent-io/jsonpath#PathActions).
    10 * The [SeekTo](https://godoc.org/github.com/exponent-io/jsonpath#Decoder.SeekTo) method supports seeking forward in a JSON token stream to a particular path.
    11 * The [Path](https://godoc.org/github.com/exponent-io/jsonpath#Decoder.Path) method returns the path of the most recently parsed token.
    12 * The [Token](https://godoc.org/github.com/exponent-io/jsonpath#Decoder.Token) method has been modified to distinguish between strings that are object keys and strings that are values. Object key strings are returned as the [KeyString](https://godoc.org/github.com/exponent-io/jsonpath#KeyString) type rather than a native string.
    13
    14## Installation
    15
    16    go get -u github.com/exponent-io/jsonpath
    17
    18## Example Usage
    19
    20#### SeekTo
    21
    22```go
    23import "github.com/exponent-io/jsonpath"
    24
    25var j = []byte(`[
    26  {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}},
    27  {"Space": "RGB",   "Point": {"R": 98, "G": 218, "B": 255}}
    28]`)
    29
    30w := json.NewDecoder(bytes.NewReader(j))
    31var v interface{}
    32
    33w.SeekTo(1, "Point", "G")
    34w.Decode(&v) // v is 218
    35```
    36
    37#### Scan with PathActions
    38
    39```go
    40var j = []byte(`{"colors":[
    41  {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10, "A": 58}},
    42  {"Space": "RGB",   "Point": {"R": 98, "G": 218, "B": 255, "A": 231}}
    43]}`)
    44
    45var actions PathActions
    46
    47// Extract the value at Point.A
    48actions.Add(func(d *Decoder) error {
    49  var alpha int
    50  err := d.Decode(&alpha)
    51  fmt.Printf("Alpha: %v\n", alpha)
    52  return err
    53}, "Point", "A")
    54
    55w := NewDecoder(bytes.NewReader(j))
    56w.SeekTo("colors", 0)
    57
    58var ok = true
    59var err error
    60for ok {
    61  ok, err = w.Scan(&actions)
    62  if err != nil && err != io.EOF {
    63    panic(err)
    64  }
    65}
    66```

View as plain text