1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
use serde_json::{json, Value};
use serde_json_path::JsonPath;
#[cfg(feature = "trace")]
use test_log::test;
fn spec_example_json() -> Value {
json!({
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 399
}
}
})
}
#[test]
fn spec_example_1() {
let value = spec_example_json();
let path = JsonPath::parse("$.store.book[*].author").unwrap();
let nodes = path.query(&value).all();
assert_eq!(
nodes,
vec![
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
);
}
#[test]
fn spec_example_2() {
let value = spec_example_json();
let path = JsonPath::parse("$..author").unwrap();
let nodes = path.query(&value).all();
assert_eq!(
nodes,
vec![
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
);
}
#[test]
fn spec_example_3() {
let value = spec_example_json();
let path = JsonPath::parse("$.store.*").unwrap();
let nodes = path.query(&value).all();
assert_eq!(nodes.len(), 2);
assert!(nodes
.iter()
.any(|&node| node == value.pointer("/store/book").unwrap()));
}
#[test]
fn spec_example_4() {
let value = spec_example_json();
let path = JsonPath::parse("$.store..price").unwrap();
let nodes = path.query(&value).all();
assert_eq!(nodes, vec![399., 8.95, 12.99, 8.99, 22.99]);
}
#[test]
fn spec_example_5() {
let value = spec_example_json();
let path = JsonPath::parse("$..book[2]").unwrap();
let node = path.query(&value).at_most_one().unwrap();
assert!(node.is_some());
assert_eq!(node, value.pointer("/store/book/2"));
}
#[test]
fn spec_example_6() {
let value = spec_example_json();
let path = JsonPath::parse("$..book[-1]").unwrap();
let node = path.query(&value).at_most_one().unwrap();
assert!(node.is_some());
assert_eq!(node, value.pointer("/store/book/3"));
}
#[test]
fn spec_example_7() {
let value = spec_example_json();
{
let path = JsonPath::parse("$..book[0,1]").unwrap();
let nodes = path.query(&value).all();
assert_eq!(nodes.len(), 2);
}
{
let path = JsonPath::parse("$..book[:2]").unwrap();
let nodes = path.query(&value).all();
assert_eq!(nodes.len(), 2);
}
}
#[test]
fn spec_example_8() {
let value = spec_example_json();
let path = JsonPath::parse("$..book[?(@.isbn)]").unwrap();
let nodes = path.query(&value);
assert_eq!(nodes.len(), 2);
}
#[test]
fn spec_example_9() {
let value = spec_example_json();
let path = JsonPath::parse("$..book[?(@.price<10)]").unwrap();
let nodes = path.query(&value);
assert_eq!(nodes.len(), 2);
}
#[test]
fn spec_example_10() {
let value = spec_example_json();
let path = JsonPath::parse("$..*").unwrap();
let nodes = path.query(&value);
assert_eq!(nodes.len(), 27);
}
|