Skip to content

Commit 82b6d90

Browse files
authored
remove JsonPathInst, instead move find functionality to JsonPath. (#68)
* remove JsonPathInst, instead move `find` functionality to `JsonPath`. 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. * keep export for macros
1 parent 1f4fd59 commit 82b6d90

File tree

12 files changed

+1065
-1010
lines changed

12 files changed

+1065
-1010
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,20 @@ v.slice_or(&some_dafault_value)
256256

257257
### Find
258258

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

262-
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).
262+
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).
263263

264264
```rust
265-
use jsonpath_rust::{JsonPathInst, JsonPathValue};
265+
use jsonpath_rust::{JsonPath, JsonPathValue};
266266
use serde_json::json;
267267
use std::str::FromStr;
268268

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

274274
let expected_value = json!({"active":1});
275275
let expected_path = "$.['first'].['second'][0]".to_string();

benches/equal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use criterion::{criterion_group, criterion_main, Criterion};
2-
use jsonpath_rust::{JsonPathInst, JsonPathQuery};
2+
use jsonpath_rust::{JsonPath, JsonPathQuery};
33
use serde_json::json;
44
use std::str::FromStr;
55

66
struct SearchData {
77
json: serde_json::Value,
8-
path: JsonPathInst,
8+
path: JsonPath,
99
}
1010

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

1313
fn equal_perf_test_with_reuse(cfg: &SearchData) {
14-
let _v = jsonpath_rust::find(&cfg.path, &cfg.json);
14+
let _v = cfg.path.find(&cfg.json);
1515
}
1616

1717
fn equal_perf_test_without_reuse() {
@@ -27,7 +27,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
2727
json: json!({
2828
"author":"abcd(Rees)",
2929
}),
30-
path: JsonPathInst::from_str(PATH).unwrap(),
30+
path: JsonPath::from_str(PATH).unwrap(),
3131
};
3232
c.bench_function("equal bench with reuse", |b| {
3333
b.iter(|| equal_perf_test_with_reuse(&data))

benches/regex.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use criterion::{criterion_group, criterion_main, Criterion};
2-
use jsonpath_rust::{JsonPathInst, JsonPathQuery};
2+
use jsonpath_rust::{JsonPath, JsonPathQuery};
33
use serde_json::json;
44
use std::str::FromStr;
55

66
struct SearchData {
77
json: serde_json::Value,
8-
path: JsonPathInst,
8+
path: JsonPath,
99
}
1010

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

1313
fn regex_perf_test_with_reuse(cfg: &SearchData) {
14-
let _v = jsonpath_rust::find(&cfg.path, &cfg.json);
14+
let _v = cfg.path.find(&cfg.json);
1515
}
1616

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

25-
fn json_path_inst_compiling() {
26-
let _v = JsonPathInst::from_str(PATH).unwrap();
25+
fn json_path_compiling() {
26+
let _v = JsonPath::from_str(PATH).unwrap();
2727
}
2828

2929
pub fn criterion_benchmark(c: &mut Criterion) {
3030
let data = SearchData {
3131
json: json!({
3232
"author":"abcd(Rees)",
3333
}),
34-
path: JsonPathInst::from_str(PATH).unwrap(),
34+
path: JsonPath::from_str(PATH).unwrap(),
3535
};
3636
c.bench_function("regex bench with reuse", |b| {
3737
b.iter(|| regex_perf_test_with_reuse(&data))
3838
});
3939
c.bench_function("regex bench without reuse", |b| {
4040
b.iter(regex_perf_test_without_reuse)
4141
});
42-
c.bench_function("JsonPathInst generation", |b| {
43-
b.iter(json_path_inst_compiling)
44-
});
42+
c.bench_function("JsonPath generation", |b| b.iter(json_path_compiling));
4543
}
4644

4745
criterion_group!(benches, criterion_benchmark);

examples/hello-world.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use jsonpath_rust::JsonPathInst;
1+
use jsonpath_rust::JsonPath;
22
use serde_json::json;
3-
use std::str::FromStr;
43

54
fn main() {
65
let data = json!({
76
"Hello":"World",
87
"Good":"Bye",
98
});
10-
let path = JsonPathInst::from_str("$.Hello").unwrap();
11-
let search_result = jsonpath_rust::find(&path, &data);
9+
let path = JsonPath::try_from("$.Hello").unwrap();
10+
let search_result = path.find(&data);
1211
println!("Hello, {}", search_result);
1312
}

0 commit comments

Comments
 (0)