jsonpath/src/lib.rs

379 lines
13 KiB
Rust
Raw Normal View History

2019-06-02 22:03:35 +09:00
//! JsonPath implementation written in Rust.
2019-03-05 23:48:27 +09:00
//!
//! # Example
//! ```
2019-06-02 22:03:35 +09:00
//! 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
//! });
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! let mut selector = jsonpath::selector(&json_obj);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$.store.book[*].author").unwrap(),
//! vec![
//! "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..author").unwrap(),
//! vec![
//! "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$.store.*").unwrap(),
//! vec![
//! &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 }
//! ]),
//! &json!({ "color": "red", "price": 19.95 })
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$.store..price").unwrap(),
//! vec![
//! 8.95, 12.99, 8.99, 22.99, 19.95
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..book[2]").unwrap(),
//! vec![
//! &json!({
//! "category" : "fiction",
//! "author" : "Herman Melville",
//! "title" : "Moby Dick",
//! "isbn" : "0-553-21311-3",
//! "price" : 8.99
//! })
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..book[-2]").unwrap(),
//! vec![
//! &json!({
//! "category" : "fiction",
//! "author" : "Herman Melville",
//! "title" : "Moby Dick",
//! "isbn" : "0-553-21311-3",
//! "price" : 8.99
//! })
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..book[0,1]").unwrap(),
//! vec![
//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
//! &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..book[:2]").unwrap(),
//! vec![
//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
//! &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..book[:2]").unwrap(),
//! vec![
//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
//! &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$..book[?(@.isbn)]").unwrap(),
//! vec![
//! &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}),
//! &json!({"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99})
//! ]);
2019-03-05 23:48:27 +09:00
//!
2019-06-02 22:03:35 +09:00
//! assert_eq!(selector("$.store.book[?(@.price < 10)]").unwrap(),
//! vec![
//! &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
//! &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99})
//! ]);
2019-03-05 23:48:27 +09:00
//! ```
2019-06-02 22:03:35 +09:00
extern crate array_tool;
2019-04-07 23:34:16 +09:00
extern crate core;
2019-04-04 09:37:44 +09:00
extern crate env_logger;
2018-12-26 14:45:31 +09:00
#[macro_use]
extern crate log;
extern crate serde;
2019-03-05 18:43:58 +09:00
extern crate serde_json;
2019-06-09 22:55:35 +09:00
extern crate indexmap;
use serde_json::Value;
2019-06-09 22:55:35 +09:00
pub use parser::parser::{Node, Parser};
pub use select::{Selector, SelectorMut};
pub use select::JsonPathError;
2019-03-11 17:35:15 +09:00
#[doc(hidden)]
2019-06-02 22:03:35 +09:00
mod parser;
2019-04-07 23:34:16 +09:00
#[doc(hidden)]
2019-06-02 22:03:35 +09:00
mod select;
2019-04-09 13:18:00 +09:00
/// It is a high-order function. it compile a JsonPath and then returns a function. this return-function can be reused for different JsonObjects.
2019-03-05 23:48:27 +09:00
///
/// ```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!({
2019-06-02 22:03:35 +09:00
/// "school": {
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
/// },
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
2019-04-04 09:37:44 +09:00
/// ]});
2019-03-05 23:48:27 +09:00
///
2019-03-14 22:30:42 +09:00
/// let json = template(&json_obj).unwrap();
2019-06-02 22:03:35 +09:00
///
/// assert_eq!(json, vec![
/// &json!({"name": "친구3", "age": 30}),
/// &json!({"name": "친구1", "age": 20})
2019-04-04 09:37:44 +09:00
/// ]);
2019-03-05 23:48:27 +09:00
/// ```
2019-06-02 22:03:35 +09:00
pub fn compile(path: &str) -> impl FnMut(&Value) -> Result<Vec<&Value>, JsonPathError> {
2019-06-03 18:45:26 +09:00
let node = Parser::compile(path);
2019-02-26 23:04:04 +09:00
move |json| {
2019-06-02 22:03:35 +09:00
let mut selector = Selector::new();
match &node {
Ok(node) => selector.compiled_path(node.clone()),
Err(e) => return Err(JsonPathError::Path(e.clone()))
};
selector.value(json).select()
2019-02-26 23:04:04 +09:00
}
}
2019-04-09 13:18:00 +09:00
/// It is a high-order function that return a function. this return-function has a jsonpath as argument and return a serde_json::value::Value. so you can use different JsonPath for one JsonObject.
2019-03-05 23:48:27 +09:00
///
/// ```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!({
2019-06-02 22:03:35 +09:00
/// "school": {
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
/// },
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
2019-04-04 09:37:44 +09:00
/// ]});
2019-03-05 23:48:27 +09:00
///
2019-03-14 22:30:42 +09:00
/// let mut selector = jsonpath::selector(&json_obj);
2019-03-05 23:48:27 +09:00
///
2019-03-11 17:35:15 +09:00
/// let json = selector("$..friends[0]").unwrap();
2019-06-02 22:03:35 +09:00
///
/// assert_eq!(json, vec![
/// &json!({"name": "친구3", "age": 30}),
/// &json!({"name": "친구1", "age": 20})
2019-04-04 09:37:44 +09:00
/// ]);
2019-03-05 23:48:27 +09:00
///
2019-03-11 17:35:15 +09:00
/// let json = selector("$..friends[1]").unwrap();
2019-06-02 22:03:35 +09:00
///
/// assert_eq!(json, vec![
/// &json!({"name": "친구4"}),
/// &json!({"name": "친구2", "age": 20})
2019-04-04 09:37:44 +09:00
/// ]);
2019-03-05 23:48:27 +09:00
/// ```
2019-06-02 22:03:35 +09:00
pub fn selector<'a>(json: &'a Value) -> impl FnMut(&'a str) -> Result<Vec<&Value>, JsonPathError> {
2019-05-14 14:19:01 +03:00
let mut selector = Selector::new();
2019-06-02 22:03:35 +09:00
let _ = selector.value(json);
move |path: &str| {
2019-06-09 22:55:35 +09:00
selector.str_path(path)?.reset_value().select()
2019-04-04 09:37:44 +09:00
}
}
2019-04-09 13:18:00 +09:00
/// It is a high-order function that returns a function. this return-function has a jsonpath as argument and return a serde::Deserialize. so you can use different JsonPath for one JsonObject.
2019-04-04 09:37:44 +09:00
///
/// ```rust
/// extern crate jsonpath_lib as jsonpath;
/// extern crate serde;
/// #[macro_use] extern crate serde_json;
///
/// use serde::{Deserialize, Serialize};
///
/// let json_obj = json!({
2019-06-02 22:03:35 +09:00
/// "school": {
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
/// },
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
2019-04-04 09:37:44 +09:00
/// ]});
///
2019-06-02 22:03:35 +09:00
/// #[derive(Deserialize, PartialEq, Debug)]
2019-04-04 09:37:44 +09:00
/// struct Friend {
/// name: String,
/// age: Option<u8>,
/// }
///
2019-06-02 22:03:35 +09:00
/// let mut selector = jsonpath::selector_as::<Friend>(&json_obj);
2019-04-04 09:37:44 +09:00
///
/// let json = selector("$..friends[0]").unwrap();
2019-06-02 22:03:35 +09:00
///
2019-04-04 09:37:44 +09:00
/// let ret = vec!(
/// Friend { name: "친구3".to_string(), age: Some(30) },
/// Friend { name: "친구1".to_string(), age: Some(20) }
/// );
/// assert_eq!(json, ret);
///
/// let json = selector("$..friends[1]").unwrap();
2019-06-02 22:03:35 +09:00
///
2019-04-04 09:37:44 +09:00
/// let ret = vec!(
/// Friend { name: "친구4".to_string(), age: None },
/// Friend { name: "친구2".to_string(), age: Some(20) }
/// );
2019-06-02 22:03:35 +09:00
///
2019-04-04 09:37:44 +09:00
/// assert_eq!(json, ret);
/// ```
2019-06-02 22:03:35 +09:00
pub fn selector_as<T: serde::de::DeserializeOwned>(json: &Value) -> impl FnMut(&str) -> Result<Vec<T>, JsonPathError> + '_ {
2019-05-14 14:19:01 +03:00
let mut selector = Selector::new();
2019-06-02 22:03:35 +09:00
let _ = selector.value(json);
2019-04-04 09:37:44 +09:00
move |path: &str| {
2019-06-09 22:55:35 +09:00
selector.str_path(path)?.reset_value().select_as()
2019-03-03 00:33:27 +09:00
}
}
2019-04-09 13:18:00 +09:00
/// This function compile a jsonpath everytime and it convert `serde_json's Value` to `jsonpath's RefValue` everytime and then it return a `serde_json::value::Value`.
2019-03-05 23:48:27 +09:00
///
/// ```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!({
2019-06-02 22:03:35 +09:00
/// "school": {
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
/// },
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
2019-04-04 09:37:44 +09:00
/// ]});
///
2019-03-14 22:30:42 +09:00
/// let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap();
2019-04-04 09:37:44 +09:00
///
2019-06-02 22:03:35 +09:00
/// assert_eq!(json, vec![
/// &json!({"name": "친구3", "age": 30}),
/// &json!({"name": "친구1", "age": 20})
2019-04-04 09:37:44 +09:00
/// ]);
2019-03-05 23:48:27 +09:00
/// ```
2019-06-02 22:03:35 +09:00
pub fn select<'a>(json: &'a Value, path: &'a str) -> Result<Vec<&'a Value>, JsonPathError> {
2019-06-09 22:55:35 +09:00
Selector::new().str_path(path)?.value(json).select()
2019-03-24 21:18:58 +09:00
}
2019-04-09 13:18:00 +09:00
/// This function compile a jsonpath everytime and it convert `&str` to `jsonpath's RefValue` everytime and then it return a json string.
///
/// ```rust
/// extern crate jsonpath_lib as jsonpath;
/// #[macro_use] extern crate serde_json;
///
2019-04-04 09:37:44 +09:00
/// let ret = jsonpath::select_as_str(r#"
/// {
/// "school": {
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
/// },
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
/// ]
/// }
/// "#, "$..friends[0]").unwrap();
///
/// assert_eq!(ret, r#"[{"name":"친구3","age":30},{"name":"친구1","age":20}]"#);
/// ```
2019-06-02 22:03:35 +09:00
pub fn select_as_str(json_str: &str, path: &str) -> Result<String, JsonPathError> {
let json = serde_json::from_str(json_str).map_err(|e| JsonPathError::Serde(e.to_string()))?;
2019-06-09 22:55:35 +09:00
let ret = Selector::new().str_path(path)?.value(&json).select()?;
2019-06-02 22:03:35 +09:00
serde_json::to_string(&ret).map_err(|e| JsonPathError::Serde(e.to_string()))
2019-03-24 21:18:58 +09:00
}
2019-04-09 13:18:00 +09:00
/// This function compile a jsonpath everytime and it convert `&str` to `jsonpath's RefValue` everytime and then it return a deserialized-instance of type `T`.
2019-04-04 09:37:44 +09:00
///
2019-03-24 21:18:58 +09:00
/// ```rust
/// extern crate jsonpath_lib as jsonpath;
/// extern crate serde;
/// #[macro_use] extern crate serde_json;
///
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize, PartialEq, Debug)]
2019-03-24 21:18:58 +09:00
/// struct Person {
/// name: String,
/// age: u8,
/// phones: Vec<String>,
/// }
///
2019-06-02 22:03:35 +09:00
/// let ret: Vec<Person> = jsonpath::select_as(r#"
2019-03-24 21:18:58 +09:00
/// {
/// "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()],
/// };
///
2019-06-02 22:03:35 +09:00
/// assert_eq!(ret[0], person);
2019-03-24 21:18:58 +09:00
/// ```
2019-06-02 22:03:35 +09:00
pub fn select_as<T: serde::de::DeserializeOwned>(json_str: &str, path: &str) -> Result<Vec<T>, JsonPathError> {
let json = serde_json::from_str(json_str).map_err(|e| JsonPathError::Serde(e.to_string()))?;
2019-06-09 22:55:35 +09:00
Selector::new().str_path(path)?.value(&json).select_as()
}