jsonpath/src/lib.rs

438 lines
13 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-14 22:30:42 +09:00
//! let mut selector = jsonpath::selector(&json_obj);
2019-03-05 23:48:27 +09:00
//!
2019-03-06 18:55:21 +09:00
//! //
//! // $.store.book[*].author
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$.store.book[*].author").unwrap();
2019-03-06 18:55:21 +09:00
//! 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
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..author").unwrap();
2019-03-06 18:55:21 +09:00
//! 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.*
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$.store.*").unwrap();
2019-03-06 18:55:21 +09:00
//! 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
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$.store..price").unwrap();
2019-03-06 18:55:21 +09:00
//! 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]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..book[2]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..book[-2]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..book[0,1]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..book[:2]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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:]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..book[2:]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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)]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$..book[?(@.isbn)]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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)]
//! //
2019-03-11 17:35:15 +09:00
//! let json = selector("$.store.book[?(@.price < 10)]").unwrap();
2019-03-06 18:55:21 +09:00
//! 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
2019-04-07 23:34:16 +09:00
extern crate core;
2019-04-04 09:37:44 +09:00
extern crate env_logger;
extern crate indexmap;
2018-12-26 14:45:31 +09:00
#[macro_use]
extern crate log;
2019-03-24 21:18:58 +09:00
#[macro_use]
extern crate serde;
2019-03-05 18:43:58 +09:00
extern crate serde_json;
2019-04-07 23:34:16 +09:00
use core::borrow::BorrowMut;
use std::result;
use serde_json::Value;
2019-03-05 23:48:27 +09:00
#[doc(hidden)]
2019-03-24 21:18:58 +09:00
pub mod parser;
2019-03-05 23:48:27 +09:00
#[doc(hidden)]
2019-03-24 21:18:58 +09:00
pub mod filter;
2019-03-11 17:35:15 +09:00
#[doc(hidden)]
2019-03-24 21:18:58 +09:00
pub mod ref_value;
2019-04-07 23:34:16 +09:00
#[doc(hidden)]
pub mod select;
2019-02-26 23:04:04 +09:00
2019-04-07 23:34:16 +09:00
pub use select::Selector;
2019-04-04 09:37:44 +09:00
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!({
/// "school": {
2019-04-04 09:37:44 +09:00
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
2019-03-05 23:48:27 +09:00
/// },
2019-04-04 09:37:44 +09:00
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
/// ]});
2019-03-05 23:48:27 +09:00
///
2019-03-14 22:30:42 +09:00
/// let json = template(&json_obj).unwrap();
2019-04-04 09:37:44 +09:00
/// let ret = json!([
/// {"name": "친구3", "age": 30},
/// {"name": "친구1", "age": 20}
/// ]);
2019-03-05 23:48:27 +09:00
/// assert_eq!(json, ret);
/// ```
2019-03-24 21:18:58 +09:00
pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value, String> + 'a {
2019-05-14 14:19:01 +03:00
let mut selector = Selector::new();
2019-04-07 23:34:16 +09:00
let _ = selector.path(path);
let mut selector = Box::new(selector);
2019-02-26 23:04:04 +09:00
move |json| {
2019-05-14 14:19:01 +03:00
let s: &mut Selector = selector.borrow_mut();
let _ = s.value(&json);
2019-04-07 23:34:16 +09:00
s.select_to_value()
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!({
/// "school": {
2019-04-04 09:37:44 +09:00
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
2019-03-05 23:48:27 +09:00
/// },
2019-04-04 09:37:44 +09:00
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
/// ]});
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-04-04 09:37:44 +09:00
/// let ret = json!([
/// {"name": "친구3", "age": 30},
/// {"name": "친구1", "age": 20}
/// ]);
2019-03-05 23:48:27 +09:00
/// assert_eq!(json, ret);
///
2019-03-11 17:35:15 +09:00
/// let json = selector("$..friends[1]").unwrap();
2019-04-04 09:37:44 +09:00
/// let ret = json!([
/// {"name": "친구4"},
/// {"name": "친구2", "age": 20}
/// ]);
2019-03-05 23:48:27 +09:00
/// assert_eq!(json, ret);
/// ```
2019-04-07 23:34:16 +09:00
pub fn selector<'a>(json: &Value) -> impl FnMut(&'a str) -> result::Result<Value, String> {
2019-05-14 14:19:01 +03:00
let mut selector = Selector::new();
2019-04-07 23:34:16 +09:00
let _ = selector.value(json.into());
let mut selector = Box::new(selector);
move |path: &'a str| {
2019-05-14 14:19:01 +03:00
let s: &mut Selector = selector.borrow_mut();
2019-04-07 23:34:16 +09:00
s.path(path)?.select_to_value()
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!({
/// "school": {
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
/// },
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
/// ]});
///
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct Friend {
/// name: String,
/// age: Option<u8>,
/// }
///
/// let mut selector = jsonpath::selector_as::<Vec<Friend>>(&json_obj);
///
/// let json = selector("$..friends[0]").unwrap();
/// 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();
/// let ret = vec!(
/// Friend { name: "친구4".to_string(), age: None },
/// Friend { name: "친구2".to_string(), age: Some(20) }
/// );
/// assert_eq!(json, ret);
/// ```
pub fn selector_as<T: serde::de::DeserializeOwned>(json: &Value) -> impl FnMut(&str) -> result::Result<T, String> {
2019-05-14 14:19:01 +03:00
let mut selector = Selector::new();
2019-04-07 23:34:16 +09:00
let _ = selector.value(json.into());
2019-04-04 09:37:44 +09:00
move |path: &str| {
2019-04-07 23:34:16 +09:00
selector.path(path)?.select_to()
2019-03-03 00:33:27 +09:00
}
}
2019-04-01 14:32:20 +09:00
#[deprecated(since = "0.1.4", note = "Please use the selector function instead")]
2019-04-07 23:34:16 +09:00
pub fn reader<'a>(json: &Value) -> impl FnMut(&'a str) -> result::Result<Value, String> {
2019-03-11 17:35:15 +09:00
selector(json)
}
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!({
/// "school": {
2019-04-04 09:37:44 +09:00
/// "friends": [
/// {"name": "친구1", "age": 20},
/// {"name": "친구2", "age": 20}
/// ]
2019-03-05 23:48:27 +09:00
/// },
2019-04-04 09:37:44 +09:00
/// "friends": [
/// {"name": "친구3", "age": 30},
/// {"name": "친구4"}
/// ]});
///
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
///
/// let ret = json!([
/// {"name": "친구3", "age": 30},
/// {"name": "친구1", "age": 20}
/// ]);
2019-03-05 23:48:27 +09:00
/// assert_eq!(json, ret);
/// ```
2019-03-24 21:18:58 +09:00
pub fn select(json: &Value, path: &str) -> result::Result<Value, String> {
2019-05-14 14:19:01 +03:00
let mut selector = Selector::new();
2019-04-07 23:34:16 +09:00
selector.path(path)?.value(json.into())?.select_to_value()
2019-03-11 17:35:15 +09:00
}
2019-04-01 14:32:20 +09:00
#[deprecated(since = "0.1.4", note = "Please use the select function instead")]
2019-03-24 21:18:58 +09:00
pub fn read(json: &Value, path: &str) -> result::Result<Value, String> {
2019-03-11 17:35:15 +09:00
select(json, path)
2019-03-03 00:33:27 +09:00
}
2019-03-05 23:48:27 +09:00
2019-04-01 14:32:20 +09:00
#[deprecated(since = "0.1.7", note = "Please use the select_as_str function instead")]
2019-03-24 21:18:58 +09:00
pub fn select_str(json: &str, path: &str) -> result::Result<String, String> {
select_as_str(json, path)
}
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-03-24 21:18:58 +09:00
pub fn select_as_str(json: &str, path: &str) -> result::Result<String, String> {
2019-05-14 14:19:01 +03:00
Selector::new()
2019-04-07 23:34:16 +09:00
.path(path)?
.value_from_str(json)?
.select_to_str()
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>,
/// }
///
/// let ret: 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!(person, ret);
/// ```
2019-04-04 09:37:44 +09:00
pub fn select_as<T: serde::de::DeserializeOwned>(json: &str, path: &str) -> result::Result<T, String> {
2019-05-14 14:19:01 +03:00
Selector::new()
2019-04-07 23:34:16 +09:00
.path(path)?
.value_from_str(json)?
.select_to()
}