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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
//! JSONPath implementation for Rust //! //! # Example //! ``` //! extern crate jsonpath_lib as jsonpath; //! #[macro_use] //! extern crate serde_json; //! //! let json_obj = 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": 19.95 //! } //! }, //! "expensive": 10 //! }); //! //! let mut reader = jsonpath::reader(json_obj); //! //! // //! // $.store.book[*].author //! // //! let json = reader("$.store.book[*].author").unwrap(); //! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]); //! assert_eq!(json, ret); //! //! // //! // $..author //! // //! let json = reader("$..author").unwrap(); //! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]); //! assert_eq!(json, ret); //! //! // //! // $.store.* //! // //! let json = reader("$.store.*").unwrap(); //! let ret = json!([ //! [ //! {"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} //! ], //! {"color" : "red","price" : 19.95}, //! ]); //! assert_eq!(ret, json); //! //! // //! // $.store..price //! // //! let json = reader("$.store..price").unwrap(); //! let ret = json!([8.95, 12.99, 8.99, 22.99, 19.95]); //! assert_eq!(ret, json); //! //! // //! // $..book[2] //! // //! let json = reader("$..book[2]").unwrap(); //! let ret = json!([{ //! "category" : "fiction", //! "author" : "Herman Melville", //! "title" : "Moby Dick", //! "isbn" : "0-553-21311-3", //! "price" : 8.99 //! }]); //! assert_eq!(ret, json); //! //! // //! // $..book[-2] //! // //! let json = reader("$..book[-2]").unwrap(); //! let ret = json!([{ //! "category" : "fiction", //! "author" : "Herman Melville", //! "title" : "Moby Dick", //! "isbn" : "0-553-21311-3", //! "price" : 8.99 //! }]); //! assert_eq!(ret, json); //! //! // //! // $..book[0,1] //! // //! let json = reader("$..book[0,1]").unwrap(); //! let ret = json!([ //! {"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} //! ]); //! assert_eq!(ret, json); //! //! // //! // $..book[:2] //! // //! let json = reader("$..book[:2]").unwrap(); //! let ret = json!([ //! {"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} //! ]); //! assert_eq!(ret, json); //! //! // //! // $..book[2:] //! // //! let json = reader("$..book[2:]").unwrap(); //! let ret = json!([ //! {"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} //! ]); //! assert_eq!(ret, json); //! //! // //! // $..book[?(@.isbn)] //! // //! let json = reader("$..book[?(@.isbn)]").unwrap(); //! let ret = json!([ //! {"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} //! ]); //! assert_eq!(ret, json); //! //! // //! // $.store.book[?(@.price < 10)] //! // //! let json = reader("$.store.book[?(@.price < 10)]").unwrap(); //! let ret = json!([ //! {"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}, //! {"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99} //! ]); //! assert_eq!(ret, json); //! ``` #[macro_use] extern crate log; extern crate env_logger; extern crate serde; #[cfg(test)] #[macro_use] extern crate serde_json; #[cfg(not(test))] extern crate serde_json; extern crate core; extern crate indexmap; #[doc(hidden)] pub mod parser; #[doc(hidden)] pub mod filter; use parser::parser::*; use filter::value_filter::*; use std::result; use std::rc::Rc; use serde_json::Value; type Result = result::Result<Value, String>; /// # Read multiple Json multiple times with the same JsonPath /// /// ```rust /// let mut template = jsonpath::compile("$..friends[0]"); /// /// /// let json_obj = json!({ /// "school": { /// "friends": [ {"id": 0}, {"id": 1} ] /// }, /// "friends": [ {"id": 0}, {"id": 1} ] /// }); /// /// let json = template(json_obj).unwrap(); /// let ret = json!([ {"id": 0}, {"id": 0} ]); /// assert_eq!(json, ret); /// /// /// let json_obj = json!({ /// "school": { /// "friends": [ {"name": "Millicent Norman"}, {"name": "Vincent Cannon"} ] /// }, /// "friends": [ {"id": 0}, {"id": 1} ] /// }); /// /// let json = template(json_obj).unwrap(); /// let ret = json!([ {"id": 0}, {"name": "Millicent Norman"} ]); /// assert_eq!(json, ret); /// ``` pub fn compile<'a>(path: &'a str) -> impl FnMut(Value) -> Result + 'a { let mut parser = Parser::new(path); let node = parser.compile(); move |json| { match &node { Ok(n) => { let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json))); jf.visit(n.clone()); Ok(jf.take_value()) } Err(e) => Err(e.clone()) } } } /// # Read the same Json multiple times using different JsonPath /// /// ```rust /// let json_obj = json!({ /// "school": { /// "friends": [{"id": 0}, {"id": 1}] /// }, /// "friends": [{"id": 0},{"id": 1}] /// }); /// /// let mut reader = jsonpath::reader(json_obj); /// /// let json = reader("$..friends[0]").unwrap(); /// let ret = json!([ {"id": 0}, {"id": 0} ]); /// assert_eq!(json, ret); /// /// let json = reader("$..friends[1]").unwrap(); /// let ret = json!([ {"id": 1}, {"id": 1} ]); /// assert_eq!(json, ret); /// ``` pub fn reader(json: Value) -> impl FnMut(&str) -> Result { let n = Rc::new(Box::new(json)); move |path: &str| { let mut jf = JsonValueFilter::new_from_value(n.clone()); let mut parser = Parser::new(path); parser.parse(&mut jf)?; Ok(jf.take_value()) } } /// # Read Json using JsonPath /// /// ```rust /// let json_obj = json!({ /// "school": { /// "friends": [{"id": 0}, {"id": 1}] /// }, /// "friends": [{"id": 0}, {"id": 1}] /// }); /// let json = jsonpath::read(json_obj, "$..friends[0]").unwrap(); /// let ret = json!([ {"id": 0}, {"id": 0} ]); /// assert_eq!(json, ret); /// ``` pub fn read(json: Value, path: &str) -> Result { let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json))); let mut parser = Parser::new(path); parser.parse(&mut jf)?; Ok(jf.take_value()) } #[cfg(test)] mod tests { use super::*; use std::io::Read; fn read_json(path: &str) -> Value { let mut f = std::fs::File::open(path).unwrap(); let mut contents = String::new(); f.read_to_string(&mut contents).unwrap(); serde_json::from_str(contents.as_str()).unwrap() } #[test] fn compile() { let mut template = super::compile("$..friends[2]"); let json_obj = read_json("./benches/data_obj.json"); let json = template(json_obj).unwrap(); let ret = json!([ {"id": 2,"name": "Gray Berry"}, {"id": 2,"name": "Gray Berry"} ]); assert_eq!(json, ret); let json_obj = read_json("./benches/data_array.json"); let json = template(json_obj).unwrap(); let ret = json!([ {"id": 2,"name": "Gray Berry"}, {"id": 2,"name": "Rosetta Erickson"} ]); assert_eq!(json, ret); } #[test] fn reader() { let json_obj = read_json("./benches/data_obj.json"); let mut reader = super::reader(json_obj); let json = reader("$..friends[2]").unwrap(); let ret = json!([ {"id": 2,"name": "Gray Berry"}, {"id": 2,"name": "Gray Berry"} ]); assert_eq!(json, ret); let json = reader("$..friends[0]").unwrap(); let ret = json!([ {"id": 0}, {"id": 0,"name": "Millicent Norman"} ]); assert_eq!(json, ret); } #[test] fn read() { let json_obj = read_json("./benches/example.json"); let json = super::read(json_obj, "$..book[2]").unwrap(); let ret = json!([{ "category" : "fiction", "author" : "Herman Melville", "title" : "Moby Dick", "isbn" : "0-553-21311-3", "price" : 8.99 }]); assert_eq!(json, ret); } }