mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-25 09:22:19 +00:00
Bump up 0.1.9
This commit is contained in:
parent
30aa38379a
commit
0729a2a47f
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "jsonpath_lib"
|
||||
version = "0.1.8"
|
||||
version = "0.1.9"
|
||||
authors = ["Changseok Han <freestrings@gmail.com>"]
|
||||
|
||||
description = "JsonPath in Rust and Webassembly - Webassembly Demo: https://freestrings.github.io/jsonpath"
|
||||
|
40
README.md
40
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\<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;
|
||||
```
|
||||
|
||||
### 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
|
||||
|
@ -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<String>,
|
||||
/// phone: String,
|
||||
/// }
|
||||
///
|
||||
/// let ret: Person = jsonpath::select_as(r#"
|
||||
/// fn input_str() -> &'static str {
|
||||
/// r#"[
|
||||
/// {
|
||||
/// "person":
|
||||
/// "name": "이름1",
|
||||
/// "age": 40,
|
||||
/// "phone": "+33 12341234"
|
||||
/// },
|
||||
/// {
|
||||
/// "name": "Doe John",
|
||||
/// "age": 44,
|
||||
/// "phones": [
|
||||
/// "+44 1234567",
|
||||
/// "+44 2345678"
|
||||
/// ]
|
||||
/// "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<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(crate) node: Option<Node>,
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "jsonpath-wasm"
|
||||
version = "0.1.6"
|
||||
version = "0.1.7"
|
||||
authors = ["Changseok Han <freestrings@gmail.com>"]
|
||||
description = "JsonPath Webassembly version compiled by Rust - Demo: https://freestrings.github.io/jsonpath"
|
||||
keywords = ["library", "jsonpath", "json", "webassembly"]
|
||||
|
@ -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);
|
||||
|
@ -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() {}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user