Skip to content

Commit

Permalink
remove JsonPathInst, instead move find functionality to JsonPath.
Browse files Browse the repository at this point in the history
no longer export all macros, add comments to the remaining ones.
Implement `FromStr` and `TryFrom<&str` for JsonPath (as the internet is not sure which is better)
fix various clippy things.
  • Loading branch information
xMAC94x committed Jul 8, 2024
1 parent 1f4fd59 commit 124a667
Show file tree
Hide file tree
Showing 12 changed files with 1,069 additions and 1,014 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,20 @@ v.slice_or(&some_dafault_value)

### Find

there are 3 different functions to find data inside a `value`.
there are 4 different functions to find data inside a `value`.
All take references, to increase reusability. Especially json parsing and jsonpath parsing can take significant time, compared to a simple find.

The methods `find`, `find_as_path` and `find_slice` take the same inputs, but handle them differently depending on your usecase. They are further described in the [docs](https://docs.rs/jsonpath-rust/latest/jsonpath_rust/index.html#functions).
The methods `find`, `find_as_path`, `find_slice` and `find_slice_ptr` take the same inputs, but handle them differently depending on your usecase. They are further described in the [docs](https://docs.rs/jsonpath-rust/latest/jsonpath_rust/enum.JsonPath.html#implementations).

```rust
use jsonpath_rust::{JsonPathInst, JsonPathValue};
use jsonpath_rust::{JsonPath, JsonPathValue};
use serde_json::json;
use std::str::FromStr;

fn main() {
let data = json!({"first":{"second":[{"active":1},{"passive":1}]}});
let path = JsonPathInst::from_str("$.first.second[?(@.active)]").unwrap();
let slice_of_data = jsonpath_rust::find_slice(&path, &data);
let path = JsonPath::from_str("$.first.second[?(@.active)]").unwrap();
let slice_of_data = path.find_slice(&data);

let expected_value = json!({"active":1});
let expected_path = "$.['first'].['second'][0]".to_string();
Expand Down
8 changes: 4 additions & 4 deletions benches/equal.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use criterion::{criterion_group, criterion_main, Criterion};
use jsonpath_rust::{JsonPathInst, JsonPathQuery};
use jsonpath_rust::{JsonPath, JsonPathQuery};
use serde_json::json;
use std::str::FromStr;

struct SearchData {
json: serde_json::Value,
path: JsonPathInst,
path: JsonPath,
}

const PATH: &str = "$.[?(@.author == 'abcd(Rees)')]";

fn equal_perf_test_with_reuse(cfg: &SearchData) {
let _v = jsonpath_rust::find(&cfg.path, &cfg.json);
let _v = cfg.path.find(&cfg.json);
}

fn equal_perf_test_without_reuse() {
Expand All @@ -27,7 +27,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
json: json!({
"author":"abcd(Rees)",
}),
path: JsonPathInst::from_str(PATH).unwrap(),
path: JsonPath::from_str(PATH).unwrap(),
};
c.bench_function("equal bench with reuse", |b| {
b.iter(|| equal_perf_test_with_reuse(&data))
Expand Down
16 changes: 7 additions & 9 deletions benches/regex.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use criterion::{criterion_group, criterion_main, Criterion};
use jsonpath_rust::{JsonPathInst, JsonPathQuery};
use jsonpath_rust::{JsonPath, JsonPathQuery};
use serde_json::json;
use std::str::FromStr;

struct SearchData {
json: serde_json::Value,
path: JsonPathInst,
path: JsonPath,
}

const PATH: &str = "$.[?(@.author ~= '.*(?i)d\\(Rees\\)')]";

fn regex_perf_test_with_reuse(cfg: &SearchData) {
let _v = jsonpath_rust::find(&cfg.path, &cfg.json);
let _v = cfg.path.find(&cfg.json);
}

fn regex_perf_test_without_reuse() {
Expand All @@ -22,26 +22,24 @@ fn regex_perf_test_without_reuse() {
let _v = json.path(PATH).expect("the path is correct");
}

fn json_path_inst_compiling() {
let _v = JsonPathInst::from_str(PATH).unwrap();
fn json_path_compiling() {
let _v = JsonPath::from_str(PATH).unwrap();
}

pub fn criterion_benchmark(c: &mut Criterion) {
let data = SearchData {
json: json!({
"author":"abcd(Rees)",
}),
path: JsonPathInst::from_str(PATH).unwrap(),
path: JsonPath::from_str(PATH).unwrap(),
};
c.bench_function("regex bench with reuse", |b| {
b.iter(|| regex_perf_test_with_reuse(&data))
});
c.bench_function("regex bench without reuse", |b| {
b.iter(regex_perf_test_without_reuse)
});
c.bench_function("JsonPathInst generation", |b| {
b.iter(json_path_inst_compiling)
});
c.bench_function("JsonPath generation", |b| b.iter(json_path_compiling));
}

criterion_group!(benches, criterion_benchmark);
Expand Down
7 changes: 3 additions & 4 deletions examples/hello-world.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use jsonpath_rust::JsonPathInst;
use jsonpath_rust::JsonPath;
use serde_json::json;
use std::str::FromStr;

fn main() {
let data = json!({
"Hello":"World",
"Good":"Bye",
});
let path = JsonPathInst::from_str("$.Hello").unwrap();
let search_result = jsonpath_rust::find(&path, &data);
let path = JsonPath::try_from("$.Hello").unwrap();
let search_result = path.find(&data);
println!("Hello, {}", search_result);
}
Loading

0 comments on commit 124a667

Please sign in to comment.