mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-03-16 15:30:50 +00:00
README.md - Rust
This commit is contained in:
parent
728345d6a6
commit
c3e5f61642
433
README.md
433
README.md
@ -13,52 +13,87 @@ To enjoy Rust
|
||||
|
||||
*(not yet published `jsonpath-wasm`)*
|
||||
```javascript
|
||||
// browser
|
||||
import * as jsonpath from "jsonpath-wasm";
|
||||
// nodejs
|
||||
let jsonpath = require('jsonpath-wasm');
|
||||
```
|
||||
|
||||
#### `read` 함수
|
||||
|
||||
```
|
||||
jsonpath.read(JSON.parse("{\"a\" : 1}"), "$.a");
|
||||
jsonpath.read("{\"a\" : 1}", "$.a");
|
||||
```
|
||||
```javascript
|
||||
let jsonObj = {
|
||||
"school": {
|
||||
"friends": [{"id": 0}, {"id": 1}]
|
||||
},
|
||||
"friends": [{"id": 0}, {"id": 1}]
|
||||
};
|
||||
let ret = [{"id": 0}, {"id": 0}];
|
||||
|
||||
let a = jsonpath.read(JSON.stringify(jsonObj), "$..friends[0]");
|
||||
let b = jsonpath.read(jsonObj, "$..friends[0]");
|
||||
console.log(
|
||||
JSON.stringify(ret) == JSON.stringify(a),
|
||||
JSON.stringify(a) == JSON.stringify(b)
|
||||
);
|
||||
```
|
||||
|
||||
#### JsonPath 재사용
|
||||
|
||||
```
|
||||
let template = jsonpath.compile("$.a");
|
||||
```javascript
|
||||
let template = jsonpath.compile("$..friends[0]");
|
||||
|
||||
//
|
||||
// 1. read json string
|
||||
//
|
||||
template("{\"a\" : 1}")
|
||||
let jsonObj = {
|
||||
"school": {
|
||||
"friends": [ {"id": 0}, {"id": 1} ]
|
||||
},
|
||||
"friends": [ {"id": 0}, {"id": 1} ]
|
||||
};
|
||||
|
||||
//
|
||||
// 2. read as json object
|
||||
//
|
||||
template(JSON.parse("{\"a\" : 1}"));
|
||||
let ret = [ {"id": 0}, {"id": 0} ];
|
||||
|
||||
// 1. read as json object
|
||||
console.log(JSON.stringify(template(jsonObj)) == JSON.stringify(ret));
|
||||
// 2. read as json string
|
||||
console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == JSON.stringify(ret));
|
||||
|
||||
let jsonObj2 = {
|
||||
"school": {
|
||||
"friends": [ {"name": "Millicent Norman"}, {"name": "Vincent Cannon"} ]
|
||||
},
|
||||
"friends": [ {"id": 0}, {"id": 1} ]
|
||||
};
|
||||
|
||||
let ret2 = [ {"id": 0}, {"name": "Millicent Norman"} ];
|
||||
|
||||
// 1. read as json object
|
||||
console.log(JSON.stringify(template(jsonObj2)) == JSON.stringify(ret2));
|
||||
// 2. read as json string
|
||||
console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == JSON.stringify(ret2));
|
||||
```
|
||||
|
||||
#### Json 재사용
|
||||
|
||||
```
|
||||
//
|
||||
// 1. read json string
|
||||
//
|
||||
let reader1 = jsonpath.reader("{\"a\" : 1}");
|
||||
reader1("$.a");
|
||||
reader1("$.b");
|
||||
```javascript
|
||||
let jsonObj = {
|
||||
"school": {
|
||||
"friends": [{"id": 0}, {"id": 1}]
|
||||
},
|
||||
"friends": [{"id": 0},{"id": 1}]
|
||||
};
|
||||
|
||||
//
|
||||
// 2. read as json object
|
||||
//
|
||||
let reader2 = jsonpath.reader(JSON.parse("{\"a\" : 1}"));
|
||||
reader2("$.a");
|
||||
reader2("$.b");
|
||||
// 1. read as json object
|
||||
let reader = jsonpath.reader(jsonObj);
|
||||
console.log(JSON.stringify(reader("$..friends[0]")) == JSON.stringify([ {"id": 0}, {"id": 0} ]));
|
||||
console.log(JSON.stringify(reader("$..friends[1]")) == JSON.stringify([ {"id": 1}, {"id": 1} ]));
|
||||
|
||||
// 2. read as json string
|
||||
let reader2 = jsonpath.reader(JSON.stringify(jsonObj));
|
||||
console.log(JSON.stringify(reader2("$..friends[0]")) == JSON.stringify([ {"id": 0}, {"id": 0} ]));
|
||||
console.log(JSON.stringify(reader2("$..friends[1]")) == JSON.stringify([ {"id": 1}, {"id": 1} ]));
|
||||
```
|
||||
|
||||
### 데모
|
||||
### 예제
|
||||
|
||||
**Demo**: https://freestrings.github.io/jsonpath/
|
||||
|
||||
@ -126,7 +161,347 @@ json 데이터 *(참고 사이트: https://github.com/json-path/JsonPath)*
|
||||
|
||||
## With Rust (as library)
|
||||
|
||||
-
|
||||
```rust
|
||||
extern crate jsonpath_lib as jsonpath;
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
```
|
||||
|
||||
#### `read` 함수
|
||||
|
||||
```rust
|
||||
let json_obj = json!({
|
||||
"school": {
|
||||
"friends": [{"id": 0}, {"id": 1}]
|
||||
},
|
||||
"friends": [{"id": 0}, {"id": 1}]
|
||||
});
|
||||
let json = jsonpath::read(json_obj, "$..friends[0]").unwrap();
|
||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
||||
assert_eq!(json, ret)
|
||||
```
|
||||
|
||||
#### JsonPath 재사용
|
||||
|
||||
```rust
|
||||
let mut template = jsonpath::compile("$..friends[0]");
|
||||
|
||||
let json_obj = json!({
|
||||
"school": {
|
||||
"friends": [ {"id": 0}, {"id": 1} ]
|
||||
},
|
||||
"friends": [ {"id": 0}, {"id": 1} ]
|
||||
});
|
||||
|
||||
let json = template(json_obj).unwrap();
|
||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
||||
assert_eq!(json, ret);
|
||||
|
||||
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);
|
||||
```
|
||||
|
||||
#### Json 재사용
|
||||
|
||||
```rust
|
||||
let json_obj = json!({
|
||||
"school": {
|
||||
"friends": [{"id": 0}, {"id": 1}]
|
||||
},
|
||||
"friends": [{"id": 0},{"id": 1}]
|
||||
});
|
||||
|
||||
let mut reader = jsonpath::reader(json_obj);
|
||||
|
||||
let json = reader("$..friends[0]").unwrap();
|
||||
let ret = json!([ {"id": 0}, {"id": 0} ]);
|
||||
assert_eq!(json, ret);
|
||||
|
||||
let json = reader("$..friends[1]").unwrap();
|
||||
let ret = json!([ {"id": 1}, {"id": 1} ]);
|
||||
assert_eq!(json, ret);
|
||||
```
|
||||
|
||||
#### 예제
|
||||
|
||||
```rust
|
||||
let json_obj = json!({
|
||||
"store": {
|
||||
"book": [
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Evelyn Waugh",
|
||||
"title": "Sword of Honour",
|
||||
"price": 12.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Herman Melville",
|
||||
"title": "Moby Dick",
|
||||
"isbn": "0-553-21311-3",
|
||||
"price": 8.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
}
|
||||
],
|
||||
"bicycle": {
|
||||
"color": "red",
|
||||
"price": 19.95
|
||||
}
|
||||
},
|
||||
"expensive": 10
|
||||
});
|
||||
|
||||
let mut reader = jsonpath::reader(json_obj);
|
||||
|
||||
//
|
||||
// $.store.book[*].author
|
||||
//
|
||||
let json = reader("$.store.book[*].author").unwrap();
|
||||
let ret = json!([
|
||||
"Nigel Rees",
|
||||
"Evelyn Waugh",
|
||||
"Herman Melville",
|
||||
"J. R. R. Tolkien"
|
||||
]);
|
||||
assert_eq!(json, ret);
|
||||
|
||||
//
|
||||
// $..author
|
||||
//
|
||||
let json = reader("$..author").unwrap();
|
||||
let ret = json!([
|
||||
"Nigel Rees",
|
||||
"Evelyn Waugh",
|
||||
"Herman Melville",
|
||||
"J. R. R. Tolkien"
|
||||
]);
|
||||
assert_eq!(json, ret);
|
||||
|
||||
//
|
||||
// $.store.*
|
||||
//
|
||||
let json = reader("$.store.*").unwrap();
|
||||
let ret = json!([
|
||||
[
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Evelyn Waugh",
|
||||
"title": "Sword of Honour",
|
||||
"price": 12.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Herman Melville",
|
||||
"title": "Moby Dick",
|
||||
"isbn": "0-553-21311-3",
|
||||
"price": 8.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
}
|
||||
],
|
||||
{
|
||||
"color": "red",
|
||||
"price": 19.95
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $.store..price
|
||||
//
|
||||
let json = reader("$.store..price").unwrap();
|
||||
let ret = json!([8.95, 12.99, 8.99, 22.99, 19.95]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[2]
|
||||
//
|
||||
let json = reader("$..book[2]").unwrap();
|
||||
let ret = json!([{
|
||||
"category" : "fiction",
|
||||
"author" : "Herman Melville",
|
||||
"title" : "Moby Dick",
|
||||
"isbn" : "0-553-21311-3",
|
||||
"price" : 8.99
|
||||
}]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[-2]
|
||||
//
|
||||
let json = reader("$..book[-2]").unwrap();
|
||||
let ret = json!([{
|
||||
"category" : "fiction",
|
||||
"author" : "Herman Melville",
|
||||
"title" : "Moby Dick",
|
||||
"isbn" : "0-553-21311-3",
|
||||
"price" : 8.99
|
||||
}]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[0,1]
|
||||
//
|
||||
let json = reader("$..book[0,1]").unwrap();
|
||||
let ret = json!([
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Evelyn Waugh",
|
||||
"title": "Sword of Honour",
|
||||
"price": 12.99
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[:2]
|
||||
//
|
||||
let json = reader("$..book[:2]").unwrap();
|
||||
let ret = json!([
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Evelyn Waugh",
|
||||
"title": "Sword of Honour",
|
||||
"price": 12.99
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[2:]
|
||||
//
|
||||
let json = reader("$..book[2:]").unwrap();
|
||||
let ret = json!([
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Herman Melville",
|
||||
"title": "Moby Dick",
|
||||
"isbn": "0-553-21311-3",
|
||||
"price": 8.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[?(@.isbn)]
|
||||
//
|
||||
let json = reader("$..book[?(@.isbn)]").unwrap();
|
||||
let ret = json!([
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Herman Melville",
|
||||
"title": "Moby Dick",
|
||||
"isbn": "0-553-21311-3",
|
||||
"price": 8.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $.store.book[?(@.price < 10)]
|
||||
//
|
||||
let json = reader("$.store.book[?(@.price < 10)]").unwrap();
|
||||
let ret = json!([
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Herman Melville",
|
||||
"title": "Moby Dick",
|
||||
"isbn": "0-553-21311-3",
|
||||
"price": 8.99
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
|
||||
//
|
||||
// $..book[?((@.price == 12.99 || $.store.bicycle.price < @.price) || @.category == "reference")]
|
||||
//
|
||||
let json = reader("$..book[?((@.price == 12.99 || $.store.bicycle.price < @.price) || @.category == "reference")]").unwrap();
|
||||
let ret = json!([
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Evelyn Waugh",
|
||||
"title": "Sword of Honour",
|
||||
"price": 12.99
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
},
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
}
|
||||
]);
|
||||
assert_eq!(ret, json);
|
||||
```
|
||||
|
||||
## With AWS API Gateway
|
||||
|
||||
|
@ -257,12 +257,12 @@ impl JsonValueFilter {
|
||||
pub fn new(json: &str) -> result::Result<Self, String> {
|
||||
let json: Value = serde_json::from_str(json)
|
||||
.map_err(|e| e.description().to_string())?;
|
||||
Ok(JsonValueFilter::new_from_value(json))
|
||||
Ok(JsonValueFilter::new_from_value(Rc::new(Box::new(json))))
|
||||
}
|
||||
|
||||
pub fn new_from_value(json: Value) -> Self{
|
||||
pub fn new_from_value(json: Rc<Box<Value>>) -> Self {
|
||||
JsonValueFilter {
|
||||
json: Rc::new(Box::new(json)),
|
||||
json: json,
|
||||
filter_stack: Vec::new(),
|
||||
token_stack: Vec::new(),
|
||||
term_stack: Vec::new(),
|
||||
|
282
src/lib.rs
282
src/lib.rs
@ -2,11 +2,11 @@
|
||||
//!
|
||||
//! # Example
|
||||
//! ```
|
||||
//! extern crate jsonpath_lib as jsonpath;
|
||||
//! #[macro_use]
|
||||
//! extern crate serde_json;
|
||||
//! extern crate jsonpath_lib as jsonpath;
|
||||
//! #[macro_use]
|
||||
//! extern crate serde_json;
|
||||
//!
|
||||
//! let json_obj = json!({
|
||||
//! let json_obj = json!({
|
||||
//! "store": {
|
||||
//! "book": [
|
||||
//! {
|
||||
@ -42,180 +42,122 @@
|
||||
//! }
|
||||
//! },
|
||||
//! "expensive": 10
|
||||
//! });
|
||||
//! });
|
||||
//!
|
||||
//! let mut reader = jsonpath::reader(json_obj);
|
||||
//! let mut reader = jsonpath::reader(json_obj);
|
||||
//!
|
||||
//! //
|
||||
//! // $.store.book[*].author
|
||||
//! //
|
||||
//! let json = reader("$.store.book[*].author");
|
||||
//! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
|
||||
//! assert_eq!(json, ret);
|
||||
//! //
|
||||
//! // $.store.book[*].author
|
||||
//! //
|
||||
//! let json = reader("$.store.book[*].author").unwrap();
|
||||
//! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
|
||||
//! assert_eq!(json, ret);
|
||||
//!
|
||||
//! //
|
||||
//! // $..author
|
||||
//! //
|
||||
//! let json = reader("$..author");
|
||||
//! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
|
||||
//! assert_eq!(json, ret);
|
||||
//! //
|
||||
//! // $..author
|
||||
//! //
|
||||
//! let json = reader("$..author").unwrap();
|
||||
//! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
|
||||
//! assert_eq!(json, ret);
|
||||
//!
|
||||
//! //
|
||||
//! // $.store.*
|
||||
//! //
|
||||
//! let json = reader("$.store.*");
|
||||
//! let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
|
||||
//! let ret = json!([
|
||||
//! [
|
||||
//! {"category" : "reference", "author" : "Nigel Rees","title" : "Sayings of the Century", "price" : 8.95},
|
||||
//! {"category" : "fiction", "author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99},
|
||||
//! {"category" : "fiction", "author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99},
|
||||
//! {"category" : "fiction", "author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99}
|
||||
//! ],
|
||||
//! {"color" : "red","price" : 19.95},
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $.store.*
|
||||
//! //
|
||||
//! let json = reader("$.store.*").unwrap();
|
||||
//! let ret = json!([
|
||||
//! [
|
||||
//! {"category" : "reference", "author" : "Nigel Rees","title" : "Sayings of the Century", "price" : 8.95},
|
||||
//! {"category" : "fiction", "author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99},
|
||||
//! {"category" : "fiction", "author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99},
|
||||
//! {"category" : "fiction", "author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99}
|
||||
//! ],
|
||||
//! {"color" : "red","price" : 19.95},
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $.store..price
|
||||
//! //
|
||||
//! let json = reader("$.store..price");
|
||||
//! let ret = json!([8.95, 12.99, 8.99, 22.99, 19.95]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $.store..price
|
||||
//! //
|
||||
//! let json = reader("$.store..price").unwrap();
|
||||
//! let ret = json!([8.95, 12.99, 8.99, 22.99, 19.95]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $..book[2]
|
||||
//! //
|
||||
//! let json = reader("$..book[2]");
|
||||
//! let ret = json!([{
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! }]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $..book[2]
|
||||
//! //
|
||||
//! let json = reader("$..book[2]").unwrap();
|
||||
//! let ret = json!([{
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! }]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $..book[-2]
|
||||
//! //
|
||||
//! let json = reader("$..book[-2]");
|
||||
//! let ret = json!([{
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! }]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $..book[-2]
|
||||
//! //
|
||||
//! let json = reader("$..book[-2]").unwrap();
|
||||
//! let ret = json!([{
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! }]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $..book[0,1]
|
||||
//! //
|
||||
//! let json = reader("$..book[0,1]");
|
||||
//! let ret = json!([
|
||||
//! {
|
||||
//! "category" : "reference",
|
||||
//! "author" : "Nigel Rees",
|
||||
//! "title" : "Sayings of the Century",
|
||||
//! "price" : 8.95
|
||||
//! },
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Evelyn Waugh",
|
||||
//! "title" : "Sword of Honour",
|
||||
//! "price" : 12.99
|
||||
//! }
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $..book[0,1]
|
||||
//! //
|
||||
//! let json = reader("$..book[0,1]").unwrap();
|
||||
//! let ret = json!([
|
||||
//! {"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95},
|
||||
//! {"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $..book[:2]
|
||||
//! //
|
||||
//! let json = reader("$..book[:2]");
|
||||
//! let ret = json!([
|
||||
//! {
|
||||
//! "category" : "reference",
|
||||
//! "author" : "Nigel Rees",
|
||||
//! "title" : "Sayings of the Century",
|
||||
//! "price" : 8.95
|
||||
//! },
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Evelyn Waugh",
|
||||
//! "title" : "Sword of Honour",
|
||||
//! "price" : 12.99
|
||||
//! }
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $..book[:2]
|
||||
//! //
|
||||
//! let json = reader("$..book[:2]").unwrap();
|
||||
//! let ret = json!([
|
||||
//! {"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95},
|
||||
//! {"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99}
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $..book[2:]
|
||||
//! //
|
||||
//! let json = reader("$..book[2:]");
|
||||
//! let ret = json!([
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! },
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "J. R. R. Tolkien",
|
||||
//! "title" : "The Lord of the Rings",
|
||||
//! "isbn" : "0-395-19395-8",
|
||||
//! "price" : 22.99
|
||||
//! }
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $..book[2:]
|
||||
//! //
|
||||
//! let json = reader("$..book[2:]").unwrap();
|
||||
//! let ret = json!([
|
||||
//! {"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99},
|
||||
//! {"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99}
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $..book[?(@.isbn)]
|
||||
//! //
|
||||
//! let json = reader("$..book[?(@.isbn)]");
|
||||
//! let ret = json!([
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! },
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "J. R. R. Tolkien",
|
||||
//! "title" : "The Lord of the Rings",
|
||||
//! "isbn" : "0-395-19395-8",
|
||||
//! "price" : 22.99
|
||||
//! }
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $.store.book[?(@.price < 10)]
|
||||
//! //
|
||||
//! let json = reader("$.store.book[?(@.price < 10)]");
|
||||
//! let ret = json!([
|
||||
//! {
|
||||
//! "category" : "reference",
|
||||
//! "author" : "Nigel Rees",
|
||||
//! "title" : "Sayings of the Century",
|
||||
//! "price" : 8.95
|
||||
//! },
|
||||
//! {
|
||||
//! "category" : "fiction",
|
||||
//! "author" : "Herman Melville",
|
||||
//! "title" : "Moby Dick",
|
||||
//! "isbn" : "0-553-21311-3",
|
||||
//! "price" : 8.99
|
||||
//! }
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! //
|
||||
//! // $..book[?(@.isbn)]
|
||||
//! //
|
||||
//! let json = reader("$..book[?(@.isbn)]").unwrap();
|
||||
//! let ret = json!([
|
||||
//! {"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99},
|
||||
//! {"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99}
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//!
|
||||
//! //
|
||||
//! // $.store.book[?(@.price < 10)]
|
||||
//! //
|
||||
//! let json = reader("$.store.book[?(@.price < 10)]").unwrap();
|
||||
//! let ret = json!([
|
||||
//! {"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95},
|
||||
//! {"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}
|
||||
//! ]);
|
||||
//! assert_eq!(ret, json);
|
||||
//! ```
|
||||
//!
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
@ -239,6 +181,7 @@ use parser::parser::*;
|
||||
use filter::value_filter::*;
|
||||
|
||||
use std::result;
|
||||
use std::rc::Rc;
|
||||
use serde_json::Value;
|
||||
|
||||
type Result = result::Result<Value, String>;
|
||||
@ -278,7 +221,7 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(Value) -> Result + 'a {
|
||||
move |json| {
|
||||
match &node {
|
||||
Ok(n) => {
|
||||
let mut jf = JsonValueFilter::new_from_value(json);
|
||||
let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json)));
|
||||
jf.visit(n.clone());
|
||||
Ok(jf.take_value())
|
||||
}
|
||||
@ -309,8 +252,9 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(Value) -> Result + 'a {
|
||||
/// assert_eq!(json, ret);
|
||||
/// ```
|
||||
pub fn reader(json: Value) -> impl FnMut(&str) -> Result {
|
||||
let mut jf = JsonValueFilter::new_from_value(json);
|
||||
let n = Rc::new(Box::new(json));
|
||||
move |path: &str| {
|
||||
let mut jf = JsonValueFilter::new_from_value(n.clone());
|
||||
let mut parser = Parser::new(path);
|
||||
parser.parse(&mut jf)?;
|
||||
Ok(jf.take_value())
|
||||
@ -326,12 +270,12 @@ pub fn reader(json: Value) -> impl FnMut(&str) -> Result {
|
||||
/// },
|
||||
/// "friends": [{"id": 0}, {"id": 1}]
|
||||
/// });
|
||||
/// let mut reader = jsonpath::read(json_obj, "$..friends[0]");
|
||||
/// let json = jsonpath::read(json_obj, "$..friends[0]").unwrap();
|
||||
/// let ret = json!([ {"id": 0}, {"id": 0} ]);
|
||||
/// assert_eq!(json, ret);
|
||||
/// ```
|
||||
pub fn read(json: Value, path: &str) -> Result {
|
||||
let mut jf = JsonValueFilter::new_from_value(json);
|
||||
let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json)));
|
||||
let mut parser = Parser::new(path);
|
||||
parser.parse(&mut jf)?;
|
||||
Ok(jf.take_value())
|
||||
|
@ -9,6 +9,7 @@ mod utils;
|
||||
use cfg_if::cfg_if;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use std::result::Result;
|
||||
use std::rc::Rc;
|
||||
use serde_json::Value;
|
||||
|
||||
use jsonpath::parser::parser::*;
|
||||
@ -23,7 +24,7 @@ cfg_if! {
|
||||
}
|
||||
|
||||
fn filter_value(json: Value, node: Node) -> JsValue {
|
||||
let mut jf = JsonValueFilter::new_from_value(json);
|
||||
let mut jf = JsonValueFilter::new_from_value(Rc::new(Box::new(json)));
|
||||
jf.visit(node);
|
||||
let taken = jf.take_value();
|
||||
match JsValue::from_serde(&taken) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user