jsonpath/src/lib.rs

357 lines
10 KiB
Rust
Raw Normal View History

2019-03-06 23:50:10 +09:00
//! JsonPath implementation for Rust
2019-03-05 23:48:27 +09:00
//!
//! # Example
//! ```
2019-03-06 18:55:21 +09:00
//! extern crate jsonpath_lib as jsonpath;
2019-03-06 23:50:10 +09:00
//! #[macro_use] extern crate serde_json;
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! let json_obj = json!({
2019-03-05 23:48:27 +09:00
//! "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
2019-03-06 18:55:21 +09:00
//! });
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! let mut reader = jsonpath::reader(json_obj);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $.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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..author
//! //
//! let json = reader("$..author").unwrap();
//! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
//! assert_eq!(json, ret);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $.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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $.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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $..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);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $.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);
2019-03-05 23:48:27 +09:00
//! ```
2019-03-06 23:50:10 +09:00
2018-12-26 14:45:31 +09:00
#[macro_use]
extern crate log;
extern crate env_logger;
2019-03-05 18:43:58 +09:00
#[cfg(test)]
2019-03-06 23:50:10 +09:00
#[macro_use] extern crate serde_json;
2019-03-05 18:43:58 +09:00
#[cfg(not(test))]
extern crate serde_json;
2019-02-19 08:20:59 +09:00
extern crate core;
2019-02-25 16:43:46 +09:00
extern crate indexmap;
2019-02-26 23:04:04 +09:00
2019-03-05 23:48:27 +09:00
#[doc(hidden)]
2019-03-03 00:33:27 +09:00
pub mod parser;
2019-03-05 23:48:27 +09:00
#[doc(hidden)]
2019-03-03 00:33:27 +09:00
pub mod filter;
2019-02-26 23:04:04 +09:00
2019-03-03 00:33:27 +09:00
use parser::parser::*;
use filter::value_filter::*;
2019-02-26 23:04:04 +09:00
use std::result;
2019-03-06 18:55:21 +09:00
use std::rc::Rc;
2019-02-26 23:04:04 +09:00
use serde_json::Value;
type Result = result::Result<Value, String>;
2019-03-05 23:48:27 +09:00
/// # Read multiple Json multiple times with the same JsonPath
///
/// ```rust
2019-03-06 23:50:10 +09:00
/// extern crate jsonpath_lib as jsonpath;
/// #[macro_use] extern crate serde_json;
///
2019-03-05 23:48:27 +09:00
/// 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);
/// ```
2019-02-26 23:04:04 +09:00
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) => {
2019-03-06 18:55:21 +09:00
let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json)));
2019-02-26 23:04:04 +09:00
jf.visit(n.clone());
Ok(jf.take_value())
2019-03-05 23:48:27 +09:00
}
2019-02-26 23:04:04 +09:00
Err(e) => Err(e.clone())
}
}
}
2019-03-05 23:48:27 +09:00
/// # Read the same Json multiple times using different JsonPath
///
/// ```rust
2019-03-06 23:50:10 +09:00
/// extern crate jsonpath_lib as jsonpath;
/// #[macro_use] extern crate serde_json;
///
2019-03-05 23:48:27 +09:00
/// 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);
/// ```
2019-03-03 00:33:27 +09:00
pub fn reader(json: Value) -> impl FnMut(&str) -> Result {
2019-03-06 18:55:21 +09:00
let n = Rc::new(Box::new(json));
2019-03-03 00:33:27 +09:00
move |path: &str| {
2019-03-06 18:55:21 +09:00
let mut jf = JsonValueFilter::new_from_value(n.clone());
2019-03-03 00:33:27 +09:00
let mut parser = Parser::new(path);
parser.parse(&mut jf)?;
Ok(jf.take_value())
}
}
2019-03-05 23:48:27 +09:00
/// # Read Json using JsonPath
///
/// ```rust
2019-03-06 23:50:10 +09:00
/// extern crate jsonpath_lib as jsonpath;
/// #[macro_use] extern crate serde_json;
///
2019-03-05 23:48:27 +09:00
/// let json_obj = json!({
/// "school": {
/// "friends": [{"id": 0}, {"id": 1}]
/// },
/// "friends": [{"id": 0}, {"id": 1}]
/// });
2019-03-06 18:55:21 +09:00
/// let json = jsonpath::read(json_obj, "$..friends[0]").unwrap();
2019-03-05 23:48:27 +09:00
/// let ret = json!([ {"id": 0}, {"id": 0} ]);
/// assert_eq!(json, ret);
/// ```
2019-03-03 00:33:27 +09:00
pub fn read(json: Value, path: &str) -> Result {
2019-03-06 18:55:21 +09:00
let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json)));
2019-02-26 23:04:04 +09:00
let mut parser = Parser::new(path);
parser.parse(&mut jf)?;
Ok(jf.take_value())
2019-03-03 00:33:27 +09:00
}
2019-03-05 23:48:27 +09:00
#[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);
}
}