diff --git a/Cargo.toml b/Cargo.toml index a65561f..01b3f7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonpath_lib" -version = "0.1.8" +version = "0.1.9" authors = ["Changseok Han "] description = "JsonPath in Rust and Webassembly - Webassembly Demo: https://freestrings.github.io/jsonpath" diff --git a/README.md b/README.md index e25dea9..ec198c6 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ To enjoy Rust! [With Rust (as library)](#with-rust-as-library) - [jsonpath_lib library](#jsonpath_lib-library) +- [rust - jsonpath::Selector struct](#rust---jsonpathselector-struct) - [rust - jsonpath::select(json: &serde_json::value::Value, jsonpath: &str)](#rust---jsonpathselectjson-serde_jsonvaluevalue-jsonpath-str) - [rust - jsonpath::select_as_str(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_as_strjson-str-jsonpath-str) - [rust - jsonpath::select_as\(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_ast-serdededeserializeownedjson-str-jsonpath-str) @@ -265,6 +266,45 @@ extern crate jsonpath_lib as jsonpath; extern crate serde_json; ``` +### rust - jsonpath::Selector struct + +```rust +#[derive(Serialize, Deserialize, PartialEq, Debug)] +struct Friend { + name: String, + age: Option, +} + +let json_obj = json!({ + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 30}, + {"name": "친구4"} +]}); + +let mut selector = Selector::new(); + +let result = selector + .path("$..[?(@.age >= 30)]").unwrap() +// .value_from_str(&serde_json::to_string(&json_obj).unwrap() /*&str*/).unwrap() +// .value_from(&json_obj /*&impl serde::ser::Serialize*/).unwrap() + .value((&json_obj /*serde_json::value::Value*/ ).into()).unwrap() + .select_to_value().unwrap(); + +assert_eq!(json!([{"name": "친구3", "age": 30}]), result); + +let result = selector.select_to_str().unwrap(); +assert_eq!(r#"[{"name":"친구3","age":30}]"#, result); + +let result = selector.select_to::>().unwrap(); +assert_eq!(vec![Friend { name: "친구3".to_string(), age: Some(30) }], result); +``` + ### rust - jsonpath::select(json: &serde_json::value::Value, jsonpath: &str) ```rust diff --git a/src/select/mod.rs b/src/select/mod.rs index e65416b..1872be4 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -7,43 +7,73 @@ use super::filter::value_filter::*; use super::parser::parser::*; use super::ref_value::model::*; -/// Utility structure. Functions like jsonpath :: selector or jsonpath :: compile are also implemented using this structure. +/// Utility. Functions like jsonpath::selector or jsonpath::compile are also implemented using this structure. /// /// ```rust /// extern crate jsonpath_lib as jsonpath; /// extern crate serde; -/// #[macro_use] extern crate serde_json; +/// extern crate serde_json; /// /// use serde::{Deserialize, Serialize}; +/// use serde_json::Value; /// -/// #[derive(Deserialize, PartialEq, Debug)] +/// use jsonpath::Selector; +/// +/// #[derive(Serialize, Deserialize, PartialEq, Debug)] /// struct Person { /// name: String, /// age: u8, -/// phones: Vec, +/// phone: String, /// } /// -/// let ret: Person = jsonpath::select_as(r#" -/// { -/// "person": +/// fn input_str() -> &'static str { +/// r#"[ /// { -/// "name": "Doe John", -/// "age": 44, -/// "phones": [ -/// "+44 1234567", -/// "+44 2345678" -/// ] +/// "name": "이름1", +/// "age": 40, +/// "phone": "+33 12341234" +/// }, +/// { +/// "name": "이름2", +/// "age": 42, +/// "phone": "++44 12341234" /// } +/// ]"# /// } -/// "#, "$.person").unwrap(); /// -/// let person = Person { -/// name: "Doe John".to_owned(), -/// age: 44, -/// phones: vec!["+44 1234567".to_owned(), "+44 2345678".to_owned()], -/// }; +/// fn input_json() -> Value { +/// serde_json::from_str(input_str()).unwrap() +/// } /// -/// assert_eq!(person, ret); +/// fn input_person() -> Vec { +/// serde_json::from_str(input_str()).unwrap() +/// } +/// +/// +/// let mut selector = Selector::new(); +/// +/// let result = selector +/// .path("$..[?(@.age > 40)]").unwrap() +/// .value_from_str(input_str()).unwrap() +/// .select_to_value().unwrap(); +/// assert_eq!(input_json()[1], result[0]); +/// +/// let result = selector.select_to_str().unwrap(); +/// assert_eq!(serde_json::to_string(&vec![&input_json()[1].clone()]).unwrap(), result); +/// +/// let result = selector.select_to::>().unwrap(); +/// assert_eq!(input_person()[1], result[0]); +/// +/// let _ = selector.path("$..[?(@.age == 40)]"); +/// +/// let result = selector.select_to_value().unwrap(); +/// assert_eq!(input_json()[0], result[0]); +/// +/// let result = selector.select_to_str().unwrap(); +/// assert_eq!(serde_json::to_string(&vec![&input_json()[0].clone()]).unwrap(), result); +/// +/// let result = selector.select_to::>().unwrap(); +/// assert_eq!(input_person()[0], result[0]); /// ``` pub struct Selector { pub(crate) node: Option, diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 9b4a7a8..b83380a 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsonpath-wasm" -version = "0.1.6" +version = "0.1.7" authors = ["Changseok Han "] description = "JsonPath Webassembly version compiled by Rust - Demo: https://freestrings.github.io/jsonpath" keywords = ["library", "jsonpath", "json", "webassembly"] diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 5447ee3..699f7e0 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -19,8 +19,6 @@ use jsonpath::filter::value_filter::JsonValueFilter; use jsonpath::parser::parser::{Node, NodeVisitor, Parser}; use jsonpath::ref_value::model::{RefValue, RefValueWrapper}; -mod utils; - cfg_if! { if #[cfg(feature = "wee_alloc")] { extern crate wee_alloc; @@ -29,6 +27,16 @@ cfg_if! { } } +cfg_if! { + if #[cfg(feature = "console_error_panic_hook")] { + extern crate console_error_panic_hook; + pub use self::console_error_panic_hook::set_once as set_panic_hook; + } else { + #[inline] + pub fn set_panic_hook() {} + } +} + fn filter_ref_value(json: RefValueWrapper, node: Node) -> JsValue { let mut jf = JsonValueFilter::new_from_value(json); jf.visit(node); diff --git a/wasm/src/utils.rs b/wasm/src/utils.rs deleted file mode 100644 index 8c6a61d..0000000 --- a/wasm/src/utils.rs +++ /dev/null @@ -1,11 +0,0 @@ -use cfg_if::cfg_if; - -cfg_if! { - if #[cfg(feature = "console_error_panic_hook")] { - extern crate console_error_panic_hook; - pub use self::console_error_panic_hook::set_once as set_panic_hook; - } else { - #[inline] - pub fn set_panic_hook() {} - } -}