...

Source file src/github.com/exponent-io/jsonpath/example_test.go

Documentation: github.com/exponent-io/jsonpath

     1  package jsonpath
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  )
     8  
     9  func ExampleDecoder_SeekTo() {
    10  
    11  	var j = []byte(`[
    12  		{"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}},
    13  		{"Space": "RGB",   "Point": {"R": 98, "G": 218, "B": 255}}
    14  	]`)
    15  
    16  	w := NewDecoder(bytes.NewReader(j))
    17  	var v interface{}
    18  
    19  	w.SeekTo(0, "Space")
    20  	w.Decode(&v)
    21  	fmt.Printf("%v => %v\n", w.Path(), v)
    22  
    23  	w.SeekTo(0, "Point", "Cr")
    24  	w.Decode(&v)
    25  	fmt.Printf("%v => %v\n", w.Path(), v)
    26  
    27  	w.SeekTo(1, "Point", "G")
    28  	w.Decode(&v)
    29  	fmt.Printf("%v => %v\n", w.Path(), v)
    30  
    31  	// seek to the end of the object
    32  	w.SeekTo()
    33  	fmt.Printf("%v\n", w.Path())
    34  
    35  	// Output:
    36  	// [0 Space] => YCbCr
    37  	// [0 Point Cr] => -10
    38  	// [1 Point G] => 218
    39  	// []
    40  }
    41  
    42  func ExampleDecoder_Scan() {
    43  
    44  	var j = []byte(`{"colors":[
    45  		{"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10, "A": 58}},
    46  		{"Space": "RGB",   "Point": {"R": 98, "G": 218, "B": 255, "A": 231}}
    47  	]}`)
    48  
    49  	var actions PathActions
    50  
    51  	// Extract the value at Point.A
    52  	actions.Add(func(d *Decoder) error {
    53  		var alpha int
    54  		err := d.Decode(&alpha)
    55  		fmt.Printf("Alpha: %v\n", alpha)
    56  		return err
    57  	}, "Point", "A")
    58  
    59  	w := NewDecoder(bytes.NewReader(j))
    60  	w.SeekTo("colors", 0)
    61  
    62  	var ok = true
    63  	var err error
    64  	for ok {
    65  		ok, err = w.Scan(&actions)
    66  		if err != nil && err != io.EOF {
    67  			panic(err)
    68  		}
    69  	}
    70  
    71  	// Output:
    72  	// Alpha: 58
    73  	// Alpha: 231
    74  }
    75  
    76  func ExampleDecoder_Scan_anyIndex() {
    77  
    78  	var j = []byte(`{"colors":[
    79  		{"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10, "A": 58}},
    80  		{"Space": "RGB",   "Point": {"R": 98, "G": 218, "B": 255, "A": 231}}
    81  	]}`)
    82  
    83  	var actions PathActions
    84  
    85  	// Extract the value at Point.A
    86  	actions.Add(func(d *Decoder) error {
    87  		var cr int
    88  		err := d.Decode(&cr)
    89  		fmt.Printf("Chrominance (Cr): %v\n", cr)
    90  		return err
    91  	}, "colors", AnyIndex, "Point", "Cr")
    92  
    93  	actions.Add(func(d *Decoder) error {
    94  		var r int
    95  		err := d.Decode(&r)
    96  		fmt.Printf("Red: %v\n", r)
    97  		return err
    98  	}, "colors", AnyIndex, "Point", "R")
    99  
   100  	w := NewDecoder(bytes.NewReader(j))
   101  
   102  	var ok = true
   103  	var err error
   104  	for ok {
   105  		ok, err = w.Scan(&actions)
   106  		if err != nil && err != io.EOF {
   107  			panic(err)
   108  		}
   109  	}
   110  
   111  	// Output:
   112  	// Chrominance (Cr): -10
   113  	// Red: 98
   114  }
   115  

View as plain text