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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
extern crate jsonpath_lib as jsonpath;
extern crate serde;
#[macro_use]
extern crate serde_json;
use serde::Deserialize;
use serde_json::Value;
use common::{compare_result, read_contents, read_json, setup};
use jsonpath::JsonPathError;
mod common;
#[test]
fn compile() {
let compile_object = |path| {
let template = jsonpath::Compiled::compile(path).unwrap();
let json_obj = read_json("./benchmark/data_obj.json");
let json = template.select(&json_obj).unwrap();
let ret = json!([
{"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Gray Berry"}
]);
compare_result(json, ret);
};
let compile_array = |path| {
let template = jsonpath::Compiled::compile(path).unwrap();
let json_obj = read_json("./benchmark/data_array.json");
let json = template.select(&json_obj).unwrap();
let ret = json!([
{"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Rosetta Erickson"}
]);
compare_result(json, ret);
};
fn compile_error() {
let template = jsonpath::Compiled::compile("$[");
assert!(template.is_err());
}
setup();
compile_object("$..friends[2]");
compile_array("$..friends[2]");
compile_error();
}
#[test]
fn selector() {
setup();
fn select<'a, F>(selector: &mut F, path: &'a str, target: Value)
where
F: FnMut(&'a str) -> Result<Vec<&Value>, JsonPathError>,
{
let json = selector(path).unwrap();
compare_result(json, target);
}
let json_obj = read_json("./benchmark/data_obj.json");
let mut selector = jsonpath::selector(&json_obj);
select(
&mut selector,
"$..friends[2]",
json!([
{"id": 2,"name": "Gray Berry"},
{"id": 2,"name": "Gray Berry"}
]),
);
select(
&mut selector,
"$..friends[0]",
json!([
{"id": 0},
{"id": 0,"name": "Millicent Norman"}
]),
);
}
#[test]
fn selector_as() {
#[derive(Deserialize, PartialEq, Debug)]
struct Friend {
id: u8,
name: Option<String>,
}
fn select<'a, F>(selector: &mut F, path: &'a str, target: Vec<Friend>)
where
F: FnMut(&'a str) -> Result<Vec<Friend>, JsonPathError>,
{
let json = selector(path).unwrap();
assert_eq!(json, target);
}
let json_obj = read_json("./benchmark/data_obj.json");
let mut selector = jsonpath::selector_as::<Friend>(&json_obj);
select(
&mut selector,
"$..friends[2]",
vec![
Friend {
id: 2,
name: Some("Gray Berry".to_string()),
},
Friend {
id: 2,
name: Some("Gray Berry".to_string()),
},
],
);
select(
&mut selector,
"$..friends[0]",
vec![
Friend { id: 0, name: None },
Friend {
id: 0,
name: Some("Millicent Norman".to_string()),
},
],
);
}
#[test]
fn select() {
let json_obj = read_json("./benchmark/example.json");
let json = jsonpath::select(&json_obj, "$..book[2]").unwrap();
let ret = json!([{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"price" : 8.99
}]);
compare_result(json, ret);
}
#[test]
fn select_str() {
let json_str = read_contents("./benchmark/example.json");
let result_str = jsonpath::select_as_str(&json_str, "$..book[2]").unwrap();
let ret = json!([{
"category" : "fiction",
"author" : "Herman Melville",
"title" : "Moby Dick",
"isbn" : "0-553-21311-3",
"price" : 8.99
}]);
let json: Value = serde_json::from_str(&result_str).unwrap();
assert_eq!(json, ret);
}
#[test]
fn test_to_struct() {
#[derive(Deserialize, PartialEq, Debug)]
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
let ret: Vec<Person> = jsonpath::select_as(
r#"
{
"person":
{
"name": "Doe John",
"age": 44,
"phones": [
"+44 1234567",
"+44 2345678"
]
}
}
"#,
"$.person",
)
.unwrap();
let person = Person {
name: "Doe John".to_string(),
age: 44,
phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
};
assert_eq!(vec![person], ret);
}
|