Bump up 0.1.9

This commit is contained in:
freestrings 2019-04-08 14:44:18 +09:00
parent 30aa38379a
commit 0729a2a47f
6 changed files with 102 additions and 35 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "jsonpath_lib" name = "jsonpath_lib"
version = "0.1.8" version = "0.1.9"
authors = ["Changseok Han <freestrings@gmail.com>"] authors = ["Changseok Han <freestrings@gmail.com>"]
description = "JsonPath in Rust and Webassembly - Webassembly Demo: https://freestrings.github.io/jsonpath" description = "JsonPath in Rust and Webassembly - Webassembly Demo: https://freestrings.github.io/jsonpath"

View File

@ -31,6 +31,7 @@ To enjoy Rust!
[With Rust (as library)](#with-rust-as-library) [With Rust (as library)](#with-rust-as-library)
- [jsonpath_lib library](#jsonpath_lib-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(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_str(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_as_strjson-str-jsonpath-str)
- [rust - jsonpath::select_as\<T: `serde::de::DeserializeOwned`\>(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_ast-serdededeserializeownedjson-str-jsonpath-str) - [rust - jsonpath::select_as\<T: `serde::de::DeserializeOwned`\>(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; extern crate serde_json;
``` ```
### rust - jsonpath::Selector struct
```rust
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Friend {
name: String,
age: Option<u8>,
}
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::<Vec<Friend>>().unwrap();
assert_eq!(vec![Friend { name: "친구3".to_string(), age: Some(30) }], result);
```
### rust - jsonpath::select(json: &serde_json::value::Value, jsonpath: &str) ### rust - jsonpath::select(json: &serde_json::value::Value, jsonpath: &str)
```rust ```rust

View File

@ -7,43 +7,73 @@ use super::filter::value_filter::*;
use super::parser::parser::*; use super::parser::parser::*;
use super::ref_value::model::*; 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 /// ```rust
/// extern crate jsonpath_lib as jsonpath; /// extern crate jsonpath_lib as jsonpath;
/// extern crate serde; /// extern crate serde;
/// #[macro_use] extern crate serde_json; /// extern crate serde_json;
/// ///
/// use serde::{Deserialize, Serialize}; /// use serde::{Deserialize, Serialize};
/// use serde_json::Value;
/// ///
/// #[derive(Deserialize, PartialEq, Debug)] /// use jsonpath::Selector;
///
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct Person { /// struct Person {
/// name: String, /// name: String,
/// age: u8, /// age: u8,
/// phones: Vec<String>, /// phone: String,
/// } /// }
/// ///
/// let ret: Person = jsonpath::select_as(r#" /// fn input_str() -> &'static str {
/// { /// r#"[
/// "person":
/// { /// {
/// "name": "Doe John", /// "name": "이름1",
/// "age": 44, /// "age": 40,
/// "phones": [ /// "phone": "+33 12341234"
/// "+44 1234567", /// },
/// "+44 2345678" /// {
/// ] /// "name": "이름2",
/// "age": 42,
/// "phone": "++44 12341234"
/// } /// }
/// ]"#
/// } /// }
/// "#, "$.person").unwrap();
/// ///
/// let person = Person { /// fn input_json() -> Value {
/// name: "Doe John".to_owned(), /// serde_json::from_str(input_str()).unwrap()
/// age: 44, /// }
/// phones: vec!["+44 1234567".to_owned(), "+44 2345678".to_owned()],
/// };
/// ///
/// assert_eq!(person, ret); /// fn input_person() -> Vec<Person> {
/// 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::<Vec<Person>>().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::<Vec<Person>>().unwrap();
/// assert_eq!(input_person()[0], result[0]);
/// ``` /// ```
pub struct Selector { pub struct Selector {
pub(crate) node: Option<Node>, pub(crate) node: Option<Node>,

View File

@ -1,6 +1,6 @@
[package] [package]
name = "jsonpath-wasm" name = "jsonpath-wasm"
version = "0.1.6" version = "0.1.7"
authors = ["Changseok Han <freestrings@gmail.com>"] authors = ["Changseok Han <freestrings@gmail.com>"]
description = "JsonPath Webassembly version compiled by Rust - Demo: https://freestrings.github.io/jsonpath" description = "JsonPath Webassembly version compiled by Rust - Demo: https://freestrings.github.io/jsonpath"
keywords = ["library", "jsonpath", "json", "webassembly"] keywords = ["library", "jsonpath", "json", "webassembly"]

View File

@ -19,8 +19,6 @@ use jsonpath::filter::value_filter::JsonValueFilter;
use jsonpath::parser::parser::{Node, NodeVisitor, Parser}; use jsonpath::parser::parser::{Node, NodeVisitor, Parser};
use jsonpath::ref_value::model::{RefValue, RefValueWrapper}; use jsonpath::ref_value::model::{RefValue, RefValueWrapper};
mod utils;
cfg_if! { cfg_if! {
if #[cfg(feature = "wee_alloc")] { if #[cfg(feature = "wee_alloc")] {
extern crate 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 { fn filter_ref_value(json: RefValueWrapper, node: Node) -> JsValue {
let mut jf = JsonValueFilter::new_from_value(json); let mut jf = JsonValueFilter::new_from_value(json);
jf.visit(node); jf.visit(node);

View File

@ -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() {}
}
}