Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove JsonPathInst, instead move find functionality to JsonPath. #68

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading