Skip to content

Commit

Permalink
Issue 31 not stale (#49)
Browse files Browse the repository at this point in the history
add a new api to handle the return of the path with value as well
  • Loading branch information
besok authored Nov 3, 2023
1 parent 4e96013 commit fc17949
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 254 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@
* make jsonpath inst cloneable.
* **`0.3.3`**
* fix a bug with the logical operators
* **`0.3.4`**
* add a result as a path
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "jsonpath-rust"
description = "The library provides the basic functionality to find the set of the data according to the filtering query."
version = "0.3.3"
version = "0.3.4"
authors = ["BorisZhguchev <[email protected]>"]
edition = "2018"
license-file = "LICENSE"
Expand Down
52 changes: 45 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ Given the json
| `$..book[?(@.author ~= /.*REES/i)]` | All books matching regex (ignore case) |
| `$..*` | Give me every thing |

## The library
### The library

The library intends to provide the basic functionality for ability to find the slices of data using the syntax, saying
above. The dependency can be found as following:
``` jsonpath-rust = 0.3.0```
``` jsonpath-rust = *```

The basic example is the following one:

Expand Down Expand Up @@ -255,7 +255,7 @@ fn main() {
let finder = JsonPathFinder::from_str(r#"{"first":{"second":[{"active":1},{"passive":1}]}}"#, "$.first.second[?(@.active)]").unwrap();
let slice_of_data: Vec<&Value> = finder.find_slice();
let js = json!({"active":1});
assert_eq!(slice_of_data, vec![JsonPathValue::Slice(&js)]);
assert_eq!(slice_of_data, vec![JsonPathValue::Slice(&js,"$.first.second[0]".to_string())]);
}
```

Expand All @@ -282,7 +282,7 @@ fn test() {

let v = finder.find_slice();
let js = json!("Sayings of the Century");
assert_eq!(v, vec![JsonPathValue::Slice(&js)]);
assert_eq!(v, vec![JsonPathValue::Slice(&js,"$.book[0].title".to_string())]);
}

```
Expand Down Expand Up @@ -345,7 +345,45 @@ fn test() {
}
```

### The structure
The library can return a path describing the value instead of the value itself.
To do that, the method `find_as_path` can be used:

```rust
use jsonpath_rust::JsonPathFinder;
use serde_json::{json, Value, JsonPathValue};

fn main() {
let finder = JsonPathFinder::from_str(r#"{"first":{"second":[{"active":1},{"passive":1}]}}"#, "$.first.second[?(@.active)]").unwrap();
let slice_of_data: Value = finder.find_as_path();
assert_eq!(slice_of_data, Value::Array(vec!["$.first.second[0]".to_string()]));
}
```

or it can be taken from the `JsonPathValue` instance:
```rust
use serde_json::{json, Value};
use crate::jsonpath_rust::{JsonPathFinder, JsonPathQuery, JsonPathInst, JsonPathValue};
use std::str::FromStr;

fn test() {
let json: Box<Value> = serde_json::from_str("{}").unwrap();
let path: Box<JsonPathInst> = Box::from(JsonPathInst::from_str("$..book[?(@.author size 10)].title").unwrap());
let finder = JsonPathFinder::new(json, path);

let v = finder.find_slice();
let js = json!("Sayings of the Century");

// Slice has a path of its value as well
assert_eq!(v, vec![JsonPathValue::Slice(&js,"$.book[0].title".to_string())]);
}
```

** If the value has been modified during the search, there is no way to find a path of a new value.
It can happen if we try to find a length() of array, for in stance.**



## The structure

```rust
pub enum JsonPath {
Expand Down Expand Up @@ -380,11 +418,11 @@ pub enum JsonPathIndex {

```

### How to contribute
## How to contribute

TBD

### How to update version
## How to update version
- update files
- add tag `git tag -a v<Version> -m "message"`
- git push origin <tag_name>
Loading

0 comments on commit fc17949

Please sign in to comment.