mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-25 09:22:19 +00:00
select_str 설명 수정
This commit is contained in:
parent
8e1a0f84e1
commit
a9f8f0bf4b
16
README.md
16
README.md
@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
It is an implementation of the `Rust` version [JsonPath] (https://goessner.net/articles/JsonPath/). `Webassembly` and` Javascript` also provide the same API interface.
|
It is an implementation of the `Rust` version [JsonPath] (https://goessner.net/articles/JsonPath/). `Webassembly` and` Javascript` also provide the same API interface.
|
||||||
|
|
||||||
## [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)
|
||||||
|
|
||||||
## 왜?
|
## 왜?
|
||||||
|
|
||||||
@ -211,16 +212,11 @@ assert_eq!(json, ret)
|
|||||||
### rust - jsonpath::select_str(json: &str, jsonpath: &str)
|
### rust - jsonpath::select_str(json: &str, jsonpath: &str)
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let json_obj = json!({
|
let ret = jsonpath::select_str(r#"{
|
||||||
"school": {
|
"school": { "friends": [{"id": 0}, {"id": 1}] },
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
|
||||||
},
|
|
||||||
"friends": [{"id": 0}, {"id": 1}]
|
"friends": [{"id": 0}, {"id": 1}]
|
||||||
});
|
}"#, "$..friends[0]").unwrap();
|
||||||
let json_str = jsonpath::select_str(&serde_json::to_string(&json_obj).unwrap(), "$..friends[0]").unwrap();
|
assert_eq!(ret, r#"[{"id":0},{"id":0}]"#);
|
||||||
let json: Value = serde_json::from_str(&json_str).unwrap();
|
|
||||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
|
||||||
assert_eq!(json, ret)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### rust - jsonpath::compile(jsonpath: &str)
|
### rust - jsonpath::compile(jsonpath: &str)
|
||||||
|
17
src/lib.rs
17
src/lib.rs
@ -301,18 +301,11 @@ pub fn read(json: &Value, path: &str) -> Result {
|
|||||||
/// extern crate jsonpath_lib as jsonpath;
|
/// extern crate jsonpath_lib as jsonpath;
|
||||||
/// #[macro_use] extern crate serde_json;
|
/// #[macro_use] extern crate serde_json;
|
||||||
///
|
///
|
||||||
/// use serde_json::Value;
|
/// let ret = jsonpath::select_str(r#"{
|
||||||
///
|
/// "school": { "friends": [{"id": 0}, {"id": 1}] },
|
||||||
/// let json_obj = json!({
|
/// "friends": [{"id": 0}, {"id": 1}]
|
||||||
/// "school": {
|
/// }"#, "$..friends[0]").unwrap();
|
||||||
/// "friends": [{"id": 0}, {"id": 1}]
|
/// assert_eq!(ret, r#"[{"id":0},{"id":0}]"#);
|
||||||
/// },
|
|
||||||
/// "friends": [{"id": 0}, {"id": 1}]
|
|
||||||
/// });
|
|
||||||
/// let json_str = jsonpath::select_str(&serde_json::to_string(&json_obj).unwrap(), "$..friends[0]").unwrap();
|
|
||||||
/// let json: Value = serde_json::from_str(&json_str).unwrap();
|
|
||||||
/// let ret = json!([ {"id": 0}, {"id": 0} ]);
|
|
||||||
/// assert_eq!(json, ret);
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn select_str(json: &str, path: &str) -> result::Result<String, String> {
|
pub fn select_str(json: &str, path: &str) -> result::Result<String, String> {
|
||||||
let ref_value: RefValue = serde_json::from_str(json).map_err(|e| format!("{:?}", e))?;
|
let ref_value: RefValue = serde_json::from_str(json).map_err(|e| format!("{:?}", e))?;
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
pub mod model;
|
pub mod model;
|
||||||
mod convert;
|
mod convert;
|
||||||
pub mod de;
|
pub mod de;
|
||||||
pub mod ser;
|
pub mod ser;
|
||||||
//mod utf8;
|
|
Loading…
x
Reference in New Issue
Block a user