2018-12-26 14:45:31 +09:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate serde;
|
2019-02-19 08:20:59 +09:00
|
|
|
#[macro_use]
|
2018-12-26 14:45:31 +09:00
|
|
|
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
|
|
|
|
|
|
|
mod jsonpath;
|
|
|
|
|
|
|
|
use jsonpath::parser::*;
|
|
|
|
use jsonpath::json_filter::value_filter::*;
|
|
|
|
|
|
|
|
use std::result;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
type Result = result::Result<Value, String>;
|
|
|
|
|
|
|
|
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(json)?;
|
|
|
|
jf.visit(n.clone());
|
|
|
|
Ok(jf.take_value())
|
|
|
|
},
|
|
|
|
Err(e) => Err(e.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filter(json: Value, path: &str) -> Result {
|
|
|
|
let mut jf = JsonValueFilter::new_from_value(json)?;
|
|
|
|
let mut parser = Parser::new(path);
|
|
|
|
parser.parse(&mut jf)?;
|
|
|
|
Ok(jf.take_value())
|
|
|
|
}
|