mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-02 23:11:08 +00:00
selector_as API 추가
This commit is contained in:
parent
583c4f4252
commit
ec80919f7f
156
README.md
156
README.md
@ -11,11 +11,11 @@ It is an implementation for [JsonPath](https://goessner.net/articles/JsonPath/)
|
|||||||
- [Webassembly Demo](https://freestrings.github.io/jsonpath/)
|
- [Webassembly Demo](https://freestrings.github.io/jsonpath/)
|
||||||
- [Rust documentation](https://docs.rs/jsonpath_lib/0.1.6/jsonpath_lib)
|
- [Rust documentation](https://docs.rs/jsonpath_lib/0.1.6/jsonpath_lib)
|
||||||
|
|
||||||
## 왜?
|
## Why?
|
||||||
|
|
||||||
To enjoy Rust!
|
To enjoy Rust!
|
||||||
|
|
||||||
## 목차
|
## API
|
||||||
|
|
||||||
[With Javascript](#with-javascript)
|
[With Javascript](#with-javascript)
|
||||||
|
|
||||||
@ -25,16 +25,17 @@ To enjoy Rust!
|
|||||||
- [javascript - jsonpath.compile(jsonpath: string)](#javascript---jsonpathcompilejsonpath-string)
|
- [javascript - jsonpath.compile(jsonpath: string)](#javascript---jsonpathcompilejsonpath-string)
|
||||||
- [javascript - jsonpath.selector(json: string|object)](#javascript---jsonpathselectorjson-stringobject)
|
- [javascript - jsonpath.selector(json: string|object)](#javascript---jsonpathselectorjson-stringobject)
|
||||||
- [javascript - alloc_json, dealloc_json](#javascript---alloc_json-dealloc_json)
|
- [javascript - alloc_json, dealloc_json](#javascript---alloc_json-dealloc_json)
|
||||||
- [javascript-wasm - examples](https://github.com/freestrings/jsonpath/wiki/javascript-wasm-examples)
|
- [javascript-wasm - examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples)
|
||||||
|
|
||||||
[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::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\>(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_astjson-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::compile(jsonpath: &str)](#rust---jsonpathcompilejsonpath-str)
|
- [rust - jsonpath::compile(jsonpath: &str)](#rust---jsonpathcompilejsonpath-str)
|
||||||
- [rust - jsonpath::selector(json: &serde_json::value::Value)](#rust---jsonpathselectorjson-serde_jsonvaluevalue)
|
- [rust - jsonpath::selector(json: &serde_json::value::Value)](#rust---jsonpathselectorjson-serde_jsonvaluevalue)
|
||||||
|
- [rust - jsonpath::selector_as\<T: `serde::de::DeserializeOwned`\>(json: &serde_json::value::Value)](#rust---jsonpathselector_ast-serdededeserializeownedjson-serde_jsonvaluevalue)
|
||||||
- [rust - examples](https://github.com/freestrings/jsonpath/wiki/rust-examples)
|
- [rust - examples](https://github.com/freestrings/jsonpath/wiki/rust-examples)
|
||||||
|
|
||||||
[With AWS API Gateway](#)
|
[With AWS API Gateway](#)
|
||||||
@ -145,14 +146,13 @@ console.log(JSON.stringify(selector("$..friends[1]")) == ret2);
|
|||||||
|
|
||||||
### javascript - alloc_json, dealloc_json
|
### javascript - alloc_json, dealloc_json
|
||||||
|
|
||||||
*(in `jsonpath-rs` not yet supported)*
|
*(not supported in `jsonpath-rs`)*
|
||||||
|
|
||||||
wasm-bindgen은 Javascript와 Webassembly 간 값을 주고받을 때 JSON 객체는 String으로 변환되기 때문에, 반복해서 사용되는 JSON 객체를 Webassembly 영역에 생성해 두면 성능에 도움이 된다.
|
wasm-bindgen은 Javascript와 Webassembly 간 값을 주고받을 때 JSON 객체는 String으로 변환되기 때문에, 반복해서 사용되는 JSON 객체를 Webassembly 영역에 생성해 두면 성능에 도움이 된다.
|
||||||
|
|
||||||
Since wasm-bindgen converts JSON objects to String when exchanging values between Javascript and Webassembly, it is helpful to create repeated Json objects in Webassembly area.
|
Since wasm-bindgen converts JSON objects to String when exchanging values between Javascript and Webassembly, it is helpful to create repeated Json objects in Webassembly area.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
let jsonObj = {
|
let jsonObj = {
|
||||||
"school": {
|
"school": {
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
"friends": [{"id": 0}, {"id": 1}]
|
||||||
@ -183,7 +183,6 @@ console.log(
|
|||||||
JSON.stringify(ret1) == JSON.stringify(ret6));// true
|
JSON.stringify(ret1) == JSON.stringify(ret6));// true
|
||||||
|
|
||||||
jsonpath.dealloc_json(ptr);
|
jsonpath.dealloc_json(ptr);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## With Rust (as library)
|
## With Rust (as library)
|
||||||
@ -201,34 +200,56 @@ extern crate serde_json;
|
|||||||
```rust
|
```rust
|
||||||
let json_obj = json!({
|
let json_obj = json!({
|
||||||
"school": {
|
"school": {
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
"friends": [
|
||||||
|
{"name": "친구1", "age": 20},
|
||||||
|
{"name": "친구2", "age": 20}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
"friends": [
|
||||||
});
|
{"name": "친구3", "age": 30},
|
||||||
let json = jsonpath::select(json_obj, "$..friends[0]").unwrap();
|
{"name": "친구4"}
|
||||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
]});
|
||||||
assert_eq!(json, ret)
|
|
||||||
|
let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap();
|
||||||
|
|
||||||
|
let ret = json!([
|
||||||
|
{"name": "친구3", "age": 30},
|
||||||
|
{"name": "친구1", "age": 20}
|
||||||
|
]);
|
||||||
|
assert_eq!(json, ret);
|
||||||
```
|
```
|
||||||
|
|
||||||
### rust - jsonpath::select_as_str(json: &str, jsonpath: &str)
|
### rust - jsonpath::select_as_str(json: &str, jsonpath: &str)
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let ret = jsonpath::select_as_str(r#"{
|
let ret = jsonpath::select_as_str(r#"
|
||||||
"school": { "friends": [{"id": 0}, {"id": 1}] },
|
{
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
"school": {
|
||||||
}"#, "$..friends[0]").unwrap();
|
"friends": [
|
||||||
assert_eq!(ret, r#"[{"id":0},{"id":0}]"#);
|
{"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}]"#);
|
||||||
```
|
```
|
||||||
|
|
||||||
### rust - jsonpath::select_as\<T\>(json: &str, jsonpath: &str)
|
### rust - jsonpath::select_as\<T: `serde::de::DeserializeOwned`\>(json: &str, jsonpath: &str)
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Deserialize, PartialEq, Debug)]
|
||||||
struct Person {
|
struct Person {
|
||||||
name: String,
|
name: String,
|
||||||
age: u8,
|
age: u8,
|
||||||
phones: Vec<String>,
|
phones: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let ret: Person = jsonpath::select_as(r#"
|
let ret: Person = jsonpath::select_as(r#"
|
||||||
{
|
{
|
||||||
"person":
|
"person":
|
||||||
@ -242,11 +263,13 @@ let ret: Person = jsonpath::select_as(r#"
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"#, "$.person").unwrap();
|
"#, "$.person").unwrap();
|
||||||
|
|
||||||
let person = Person {
|
let person = Person {
|
||||||
name: "Doe John".to_string(),
|
name: "Doe John".to_string(),
|
||||||
age: 44,
|
age: 44,
|
||||||
phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
|
phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(person, ret);
|
assert_eq!(person, ret);
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -257,44 +280,99 @@ let mut template = jsonpath::compile("$..friends[0]");
|
|||||||
|
|
||||||
let json_obj = json!({
|
let json_obj = json!({
|
||||||
"school": {
|
"school": {
|
||||||
"friends": [ {"id": 0}, {"id": 1} ]
|
"friends": [
|
||||||
|
{"name": "친구1", "age": 20},
|
||||||
|
{"name": "친구2", "age": 20}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"friends": [ {"id": 0}, {"id": 1} ]
|
"friends": [
|
||||||
});
|
{"name": "친구3", "age": 30},
|
||||||
|
{"name": "친구4"}
|
||||||
|
]});
|
||||||
|
|
||||||
let json = template(&json_obj).unwrap();
|
let json = template(&json_obj).unwrap();
|
||||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
|
||||||
assert_eq!(json, ret);
|
|
||||||
|
|
||||||
let json_obj = json!({
|
let ret = json!([
|
||||||
"school": {
|
{"name": "친구3", "age": 30},
|
||||||
"friends": [ {"name": "Millicent Norman"}, {"name": "Vincent Cannon"} ]
|
{"name": "친구1", "age": 20}
|
||||||
},
|
]);
|
||||||
"friends": [ {"id": 0}, {"id": 1} ]
|
|
||||||
});
|
|
||||||
|
|
||||||
let json = template(json_obj).unwrap();
|
|
||||||
let ret = json!([ {"id": 0}, {"name": "Millicent Norman"} ]);
|
|
||||||
assert_eq!(json, ret);
|
assert_eq!(json, ret);
|
||||||
```
|
```
|
||||||
|
|
||||||
### rust - jsonpath::selector(&json: serde_json::value::Value)
|
### rust - jsonpath::selector(json: &serde_json::value::Value)
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let json_obj = json!({
|
let json_obj = json!({
|
||||||
"school": {
|
"school": {
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
"friends": [
|
||||||
|
{"name": "친구1", "age": 20},
|
||||||
|
{"name": "친구2", "age": 20}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"friends": [{"id": 0},{"id": 1}]
|
"friends": [
|
||||||
});
|
{"name": "친구3", "age": 30},
|
||||||
|
{"name": "친구4"}
|
||||||
|
]});
|
||||||
|
|
||||||
let mut selector = jsonpath::selector(&json_obj);
|
let mut selector = jsonpath::selector(&json_obj);
|
||||||
|
|
||||||
let json = selector("$..friends[0]").unwrap();
|
let json = selector("$..friends[0]").unwrap();
|
||||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
|
||||||
|
let ret = json!([
|
||||||
|
{"name": "친구3", "age": 30},
|
||||||
|
{"name": "친구1", "age": 20}
|
||||||
|
]);
|
||||||
|
|
||||||
assert_eq!(json, ret);
|
assert_eq!(json, ret);
|
||||||
|
|
||||||
let json = selector("$..friends[1]").unwrap();
|
let json = selector("$..friends[1]").unwrap();
|
||||||
let ret = json!([ {"id": 1}, {"id": 1} ]);
|
|
||||||
|
let ret = json!([
|
||||||
|
{"name": "친구4"},
|
||||||
|
{"name": "친구2", "age": 20}
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert_eq!(json, ret);
|
||||||
|
```
|
||||||
|
|
||||||
|
### rust - jsonpath::selector_as\<T: `serde::de::DeserializeOwned`\>(json: &serde_json::value::Value)
|
||||||
|
|
||||||
|
```rust
|
||||||
|
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);
|
assert_eq!(json, ret);
|
||||||
```
|
```
|
@ -42,6 +42,17 @@ fn bench_selector(b: &mut Bencher) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn bench_selector_as(b: &mut Bencher) {
|
||||||
|
let json = get_json();
|
||||||
|
let mut selector = jsonpath::selector_as::<Value>(&json);
|
||||||
|
b.iter(move || {
|
||||||
|
for _ in 1..100 {
|
||||||
|
let _ = selector(get_path()).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_select_val(b: &mut Bencher) {
|
fn bench_select_val(b: &mut Bencher) {
|
||||||
let json = get_json();
|
let json = get_json();
|
||||||
@ -53,7 +64,7 @@ fn bench_select_val(b: &mut Bencher) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_select_str(b: &mut Bencher) {
|
fn bench_select_as_str(b: &mut Bencher) {
|
||||||
let json = get_string();
|
let json = get_string();
|
||||||
b.iter(move || {
|
b.iter(move || {
|
||||||
for _ in 1..100 {
|
for _ in 1..100 {
|
||||||
@ -73,7 +84,6 @@ fn bench_compile(b: &mut Bencher) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_select_as(b: &mut Bencher) {
|
fn bench_select_as(b: &mut Bencher) {
|
||||||
let json = get_string();
|
let json = get_string();
|
||||||
@ -83,7 +93,7 @@ fn bench_select_as(b: &mut Bencher) {
|
|||||||
category: String,
|
category: String,
|
||||||
author: String,
|
author: String,
|
||||||
title: String,
|
title: String,
|
||||||
price: f64
|
price: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
b.iter(move || {
|
b.iter(move || {
|
||||||
|
@ -277,9 +277,9 @@ pub struct JsonValueFilter {
|
|||||||
|
|
||||||
impl JsonValueFilter {
|
impl JsonValueFilter {
|
||||||
pub fn new(json: &str) -> Result<Self, String> {
|
pub fn new(json: &str) -> Result<Self, String> {
|
||||||
let json: Value = serde_json::from_str(json)
|
let json: RefValue = serde_json::from_str(json)
|
||||||
.map_err(|e| e.description().to_string())?;
|
.map_err(|e| e.description().to_string())?;
|
||||||
Ok(JsonValueFilter::new_from_value((&json).into()))
|
Ok(JsonValueFilter::new_from_value(json .into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_value(json: RefValueWrapper) -> Self {
|
pub fn new_from_value(json: RefValueWrapper) -> Self {
|
||||||
|
206
src/lib.rs
206
src/lib.rs
@ -158,6 +158,7 @@
|
|||||||
//! assert_eq!(ret, json);
|
//! assert_eq!(ret, json);
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
extern crate env_logger;
|
||||||
extern crate indexmap;
|
extern crate indexmap;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
@ -165,6 +166,7 @@ extern crate log;
|
|||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::result;
|
use std::result;
|
||||||
|
|
||||||
@ -172,7 +174,7 @@ use serde_json::Value;
|
|||||||
|
|
||||||
use filter::value_filter::JsonValueFilter;
|
use filter::value_filter::JsonValueFilter;
|
||||||
use parser::parser::{NodeVisitor, Parser};
|
use parser::parser::{NodeVisitor, Parser};
|
||||||
use ref_value::model::{RefValue, RefValueWrapper};
|
use ref_value::model::RefValueWrapper;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
@ -181,7 +183,23 @@ pub mod filter;
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod ref_value;
|
pub mod ref_value;
|
||||||
|
|
||||||
/// # Compile a Jsonpath so it select a JsonObject immediately.
|
fn query_from_str(json: &str, path: &str) -> result::Result<JsonValueFilter, String> {
|
||||||
|
let mut jf = JsonValueFilter::new(json)?;
|
||||||
|
let mut parser = Parser::new(path);
|
||||||
|
parser.parse(&mut jf)?;
|
||||||
|
Ok(jf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn query_from_json_wrapper(json_wrapper: RefValueWrapper, path: &str) -> result::Result<JsonValueFilter, String> {
|
||||||
|
let mut jf = JsonValueFilter::new_from_value(json_wrapper);
|
||||||
|
let mut parser = Parser::new(path);
|
||||||
|
parser.parse(&mut jf)?;
|
||||||
|
Ok(jf)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// It is a highorder function that compile a JsonPath then returns a function.
|
||||||
|
///
|
||||||
|
/// this return function can be reused for different JsonObjects.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate jsonpath_lib as jsonpath;
|
/// extern crate jsonpath_lib as jsonpath;
|
||||||
@ -189,28 +207,23 @@ pub mod ref_value;
|
|||||||
///
|
///
|
||||||
/// let mut template = jsonpath::compile("$..friends[0]");
|
/// let mut template = jsonpath::compile("$..friends[0]");
|
||||||
///
|
///
|
||||||
///
|
|
||||||
/// let json_obj = json!({
|
/// let json_obj = json!({
|
||||||
/// "school": {
|
/// "school": {
|
||||||
/// "friends": [ {"id": 0}, {"id": 1} ]
|
/// "friends": [
|
||||||
|
/// {"name": "친구1", "age": 20},
|
||||||
|
/// {"name": "친구2", "age": 20}
|
||||||
|
/// ]
|
||||||
/// },
|
/// },
|
||||||
/// "friends": [ {"id": 0}, {"id": 1} ]
|
/// "friends": [
|
||||||
/// });
|
/// {"name": "친구3", "age": 30},
|
||||||
|
/// {"name": "친구4"}
|
||||||
|
/// ]});
|
||||||
///
|
///
|
||||||
/// let json = template(&json_obj).unwrap();
|
/// let json = template(&json_obj).unwrap();
|
||||||
/// let ret = json!([ {"id": 0}, {"id": 0} ]);
|
/// let ret = json!([
|
||||||
/// assert_eq!(json, ret);
|
/// {"name": "친구3", "age": 30},
|
||||||
///
|
/// {"name": "친구1", "age": 20}
|
||||||
///
|
/// ]);
|
||||||
/// let json_obj = json!({
|
|
||||||
/// "school": {
|
|
||||||
/// "friends": [ {"name": "Millicent Norman"}, {"name": "Vincent Cannon"} ]
|
|
||||||
/// },
|
|
||||||
/// "friends": [ {"id": 0}, {"id": 1} ]
|
|
||||||
/// });
|
|
||||||
///
|
|
||||||
/// let json = template(&json_obj).unwrap();
|
|
||||||
/// let ret = json!([ {"id": 0}, {"name": "Millicent Norman"} ]);
|
|
||||||
/// assert_eq!(json, ret);
|
/// assert_eq!(json, ret);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value, String> + 'a {
|
pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value, String> + 'a {
|
||||||
@ -221,15 +234,16 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value,
|
|||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
let mut jf = JsonValueFilter::new_from_value(json.into());
|
let mut jf = JsonValueFilter::new_from_value(json.into());
|
||||||
jf.visit(n.clone());
|
jf.visit(n.clone());
|
||||||
Ok(jf.take_value().into())
|
Ok((&jf.take_value()).into())
|
||||||
}
|
}
|
||||||
Err(e) => Err(e.clone())
|
Err(e) => Err(e.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// It returns highorder function that return a function.
|
||||||
/// # Use multiple JsonPaths for one JsonObject.
|
///
|
||||||
|
/// this function has a jsonpath as argument and return a serde_json::value::Value. so you can use different JsonPath for one JsonObject.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate jsonpath_lib as jsonpath;
|
/// extern crate jsonpath_lib as jsonpath;
|
||||||
@ -237,28 +251,90 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value,
|
|||||||
///
|
///
|
||||||
/// let json_obj = json!({
|
/// let json_obj = json!({
|
||||||
/// "school": {
|
/// "school": {
|
||||||
/// "friends": [{"id": 0}, {"id": 1}]
|
/// "friends": [
|
||||||
|
/// {"name": "친구1", "age": 20},
|
||||||
|
/// {"name": "친구2", "age": 20}
|
||||||
|
/// ]
|
||||||
/// },
|
/// },
|
||||||
/// "friends": [{"id": 0},{"id": 1}]
|
/// "friends": [
|
||||||
/// });
|
/// {"name": "친구3", "age": 30},
|
||||||
|
/// {"name": "친구4"}
|
||||||
|
/// ]});
|
||||||
///
|
///
|
||||||
/// let mut selector = jsonpath::selector(&json_obj);
|
/// let mut selector = jsonpath::selector(&json_obj);
|
||||||
///
|
///
|
||||||
/// let json = selector("$..friends[0]").unwrap();
|
/// let json = selector("$..friends[0]").unwrap();
|
||||||
/// let ret = json!([ {"id": 0}, {"id": 0} ]);
|
/// let ret = json!([
|
||||||
|
/// {"name": "친구3", "age": 30},
|
||||||
|
/// {"name": "친구1", "age": 20}
|
||||||
|
/// ]);
|
||||||
/// assert_eq!(json, ret);
|
/// assert_eq!(json, ret);
|
||||||
///
|
///
|
||||||
/// let json = selector("$..friends[1]").unwrap();
|
/// let json = selector("$..friends[1]").unwrap();
|
||||||
/// let ret = json!([ {"id": 1}, {"id": 1} ]);
|
/// let ret = json!([
|
||||||
|
/// {"name": "친구4"},
|
||||||
|
/// {"name": "친구2", "age": 20}
|
||||||
|
/// ]);
|
||||||
/// assert_eq!(json, ret);
|
/// assert_eq!(json, ret);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn selector(json: &Value) -> impl FnMut(&str) -> result::Result<Value, String> {
|
pub fn selector(json: &Value) -> impl FnMut(&str) -> result::Result<Value, String> {
|
||||||
let wrapper: RefValueWrapper = json.into();
|
let wrapper: RefValueWrapper = json.into();
|
||||||
move |path: &str| {
|
move |path: &str| {
|
||||||
let mut jf = JsonValueFilter::new_from_value(wrapper.clone());
|
let mut jf = query_from_json_wrapper(wrapper.clone(), path)?;
|
||||||
let mut parser = Parser::new(path);
|
Ok((&jf.take_value()).into())
|
||||||
parser.parse(&mut jf)?;
|
}
|
||||||
Ok(jf.take_value().into())
|
}
|
||||||
|
|
||||||
|
/// It returns highorder function that returns a function.
|
||||||
|
///
|
||||||
|
/// this function has a jsonpath as argument and return a serde::Deserialize. so you can use different JsonPath for one JsonObject.
|
||||||
|
///
|
||||||
|
/// ```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> {
|
||||||
|
let wrapper: RefValueWrapper = json.into();
|
||||||
|
move |path: &str| {
|
||||||
|
let mut jf = query_from_json_wrapper(wrapper.clone(), path)?;
|
||||||
|
T::deserialize(jf.take_value().deref()).map_err(|e| format!("{:?}", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,7 +343,7 @@ pub fn reader(json: &Value) -> impl FnMut(&str) -> result::Result<Value, String>
|
|||||||
selector(json)
|
selector(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Select a JsonObject
|
/// Select a JsonObject. it return a serde_json::value::Value.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate jsonpath_lib as jsonpath;
|
/// extern crate jsonpath_lib as jsonpath;
|
||||||
@ -275,19 +351,27 @@ pub fn reader(json: &Value) -> impl FnMut(&str) -> result::Result<Value, String>
|
|||||||
///
|
///
|
||||||
/// let json_obj = json!({
|
/// let json_obj = json!({
|
||||||
/// "school": {
|
/// "school": {
|
||||||
/// "friends": [{"id": 0}, {"id": 1}]
|
/// "friends": [
|
||||||
|
/// {"name": "친구1", "age": 20},
|
||||||
|
/// {"name": "친구2", "age": 20}
|
||||||
|
/// ]
|
||||||
/// },
|
/// },
|
||||||
/// "friends": [{"id": 0}, {"id": 1}]
|
/// "friends": [
|
||||||
/// });
|
/// {"name": "친구3", "age": 30},
|
||||||
|
/// {"name": "친구4"}
|
||||||
|
/// ]});
|
||||||
|
///
|
||||||
/// let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap();
|
/// let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap();
|
||||||
/// let ret = json!([ {"id": 0}, {"id": 0} ]);
|
///
|
||||||
|
/// let ret = json!([
|
||||||
|
/// {"name": "친구3", "age": 30},
|
||||||
|
/// {"name": "친구1", "age": 20}
|
||||||
|
/// ]);
|
||||||
/// assert_eq!(json, ret);
|
/// assert_eq!(json, ret);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn select(json: &Value, path: &str) -> result::Result<Value, String> {
|
pub fn select(json: &Value, path: &str) -> result::Result<Value, String> {
|
||||||
let mut jf = JsonValueFilter::new_from_value(json.into());
|
let mut jf = query_from_json_wrapper(json.into(), path)?;
|
||||||
let mut parser = Parser::new(path);
|
Ok((&jf.take_value()).into())
|
||||||
parser.parse(&mut jf)?;
|
|
||||||
Ok(jf.take_value().into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deprecated(since = "0.1.4", note = "Please use the select function instead")]
|
#[deprecated(since = "0.1.4", note = "Please use the select function instead")]
|
||||||
@ -300,27 +384,36 @@ pub fn select_str(json: &str, path: &str) -> result::Result<String, String> {
|
|||||||
select_as_str(json, path)
|
select_as_str(json, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Return to json string
|
/// Select a JsonObject. it return a JsonObject as String.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate jsonpath_lib as jsonpath;
|
/// extern crate jsonpath_lib as jsonpath;
|
||||||
/// #[macro_use] extern crate serde_json;
|
/// #[macro_use] extern crate serde_json;
|
||||||
///
|
///
|
||||||
/// let ret = jsonpath::select_as_str(r#"{
|
/// let ret = jsonpath::select_as_str(r#"
|
||||||
/// "school": { "friends": [{"id": 0}, {"id": 1}] },
|
/// {
|
||||||
/// "friends": [{"id": 0}, {"id": 1}]
|
/// "school": {
|
||||||
/// }"#, "$..friends[0]").unwrap();
|
/// "friends": [
|
||||||
/// assert_eq!(ret, r#"[{"id":0},{"id":0}]"#);
|
/// {"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}]"#);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn select_as_str(json: &str, path: &str) -> result::Result<String, String> {
|
pub fn select_as_str(json: &str, path: &str) -> result::Result<String, String> {
|
||||||
let ref_value: RefValue = serde_json::from_str(json).map_err(|e| format!("{:?}", e))?;
|
let mut jf = query_from_str(json, path)?;
|
||||||
let mut jf = JsonValueFilter::new_from_value(ref_value.into());
|
serde_json::to_string(&jf.take_value().deref()).map_err(|e| e.description().to_string())
|
||||||
let mut parser = Parser::new(path);
|
|
||||||
parser.parse(&mut jf)?;
|
|
||||||
serde_json::to_string(&jf.take_value().deref()).map_err(|e| format!("{:?}", e))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Return to deserializeable.
|
/// Select a JsonObject. it return a deserialized instance of type `T`
|
||||||
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// extern crate jsonpath_lib as jsonpath;
|
/// extern crate jsonpath_lib as jsonpath;
|
||||||
/// extern crate serde;
|
/// extern crate serde;
|
||||||
@ -357,10 +450,7 @@ pub fn select_as_str(json: &str, path: &str) -> result::Result<String, String> {
|
|||||||
///
|
///
|
||||||
/// assert_eq!(person, ret);
|
/// assert_eq!(person, ret);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn select_as<'a, T: serde::Deserialize<'a>>(json: &str, path: &str) -> result::Result<T, String> {
|
pub fn select_as<T: serde::de::DeserializeOwned>(json: &str, path: &str) -> result::Result<T, String> {
|
||||||
let ref_value: RefValue = serde_json::from_str(json).map_err(|e| format!("{:?}", e))?;
|
let mut jf = query_from_str(json, path)?;
|
||||||
let mut jf = JsonValueFilter::new_from_value(ref_value.into());
|
T::deserialize(jf.take_value().deref()).map_err(|e| e.description().to_string())
|
||||||
let mut parser = Parser::new(path);
|
|
||||||
parser.parse(&mut jf)?;
|
|
||||||
T::deserialize(jf.take_value().deref()).map_err(|e| format!("{:?}", e))
|
|
||||||
}
|
}
|
@ -254,15 +254,6 @@ impl Into<RefValueWrapper> for &Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<Value> for RefValueWrapper {
|
|
||||||
fn into(self) -> Value {
|
|
||||||
match serde_json::to_value(self.deref()) {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(e) => panic!("Error RefValueWrapper into Value: {:?}", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Into<Value> for &RefValueWrapper {
|
impl Into<Value> for &RefValueWrapper {
|
||||||
fn into(self) -> Value {
|
fn into(self) -> Value {
|
||||||
match serde_json::to_value(self.deref()) {
|
match serde_json::to_value(self.deref()) {
|
||||||
|
27
tests/lib.rs
27
tests/lib.rs
@ -63,6 +63,33 @@ fn selector() {
|
|||||||
assert_eq!(json, ret);
|
assert_eq!(json, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn selector_as() {
|
||||||
|
let json_obj = read_json("./benches/data_obj.json");
|
||||||
|
let mut selector = jsonpath::selector_as::<Vec<Friend>>(&json_obj);
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
struct Friend {
|
||||||
|
id: u8,
|
||||||
|
name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let json = selector("$..friends[2]").unwrap();
|
||||||
|
|
||||||
|
let ret = vec!(
|
||||||
|
Friend { id: 2, name: Some("Gray Berry".to_string()) },
|
||||||
|
Friend { id: 2, name: Some("Gray Berry".to_string()) },
|
||||||
|
);
|
||||||
|
assert_eq!(json, ret);
|
||||||
|
|
||||||
|
let json = selector("$..friends[0]").unwrap();
|
||||||
|
let ret = vec!(
|
||||||
|
Friend { id: 0, name: None },
|
||||||
|
Friend { id: 0, name: Some("Millicent Norman".to_string()) },
|
||||||
|
);
|
||||||
|
assert_eq!(json, ret);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn select() {
|
fn select() {
|
||||||
let json_obj = read_json("./benches/example.json");
|
let json_obj = read_json("./benches/example.json");
|
||||||
|
@ -19,7 +19,7 @@ fn de() {
|
|||||||
let json_str = read_json("./benches/example.json");
|
let json_str = read_json("./benches/example.json");
|
||||||
// RefValue -> Value
|
// RefValue -> Value
|
||||||
let ref_value: RefValue = serde_json::from_str(json_str.as_str()).unwrap();
|
let ref_value: RefValue = serde_json::from_str(json_str.as_str()).unwrap();
|
||||||
let value_wrapper: RefValueWrapper = ref_value.into();
|
let ref value_wrapper: RefValueWrapper = ref_value.into();
|
||||||
let value: Value = value_wrapper.into();
|
let value: Value = value_wrapper.into();
|
||||||
|
|
||||||
// Value
|
// Value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user