Skip to content

Commit 0fbf796

Browse files
author
meir
committed
added a small readme
1 parent 77e3f96 commit 0fbf796

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

README.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
# JsonPathCalculator
2-
Json path calculator library for rust
2+
Json path calculator library for rust. The idea behind this library is that it can operate on any json representation as long as it implements the [`SelectValue`](src/select_value.rs) triat. The library has an implementation for [serde_json value](https://docs.serde.rs/serde_json/value/enum.Value.html) and [ivalue](https://docs.rs/tch/0.1.1/tch/enum.IValue.html).
3+
4+
### Getting Started
5+
Add the following to your cargo.toml
6+
7+
```rust
8+
[dependencies]
9+
jsonpath_calculator = { git = "https://github.com/RedisJSON/JsonPathCalculator.git", branch = "master" }
10+
```
11+
12+
Usage example:
13+
14+
```rust
15+
extern crate jsonpath_calculator
16+
#[macro_use] extern crate serde_json;
17+
18+
fn main() {
19+
let mut query = jsonpath_calculator::compile("$..friends[0]");
20+
let calculator = jsonpath_calculator::create(&query)
21+
22+
let json_obj = json!({
23+
"school": {
24+
"friends": [
25+
{"name": "친구1", "age": 20},
26+
{"name": "친구2", "age": 20}
27+
]
28+
},
29+
"friends": [
30+
{"name": "친구3", "age": 30},
31+
{"name": "친구4"}
32+
]
33+
});
34+
35+
let json = calculator.calc(&json_obj);
36+
37+
assert_eq!(json, vec![
38+
&json!({"name": "친구3", "age": 30}),
39+
&json!({"name": "친구1", "age": 20})
40+
]);
41+
}
42+
```
43+
44+
### Tests
45+
`jsonpath_calculator` pass **Almost** all the tests on https://github.com/freestrings/jsonpath, to run the tests:
46+
47+
```
48+
cargo test
49+
```

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ use json_path::{
2222

2323
/// Create a PathCalculator object. The path calculator can be re-used
2424
/// to calculate json paths on different jsons.
25+
/// /// ```rust
26+
/// extern crate jsonpath_calculator
27+
/// #[macro_use] extern crate serde_json;
28+
///
29+
/// let mut query = jsonpath_calculator::compile("$..friends[0]");
30+
/// let calculator = jsonpath_calculator::create(&query)
31+
///
32+
/// let json_obj = json!({
33+
/// "school": {
34+
/// "friends": [
35+
/// {"name": "친구1", "age": 20},
36+
/// {"name": "친구2", "age": 20}
37+
/// ]
38+
/// },
39+
/// "friends": [
40+
/// {"name": "친구3", "age": 30},
41+
/// {"name": "친구4"}
42+
/// ]});
43+
///
44+
/// let json = calculator.calc(&json_obj);
45+
///
46+
/// assert_eq!(json, vec![
47+
/// &json!({"name": "친구3", "age": 30}),
48+
/// &json!({"name": "친구1", "age": 20})
49+
/// ]);
50+
/// ```
2551
pub fn create<'i>(query: &'i Query<'i>) -> PathCalculator<'i, DummyTrackerGenerator> {
2652
PathCalculator::create(query)
2753
}

0 commit comments

Comments
 (0)