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

Keep track of traversed path and implement JsonPath::query_path_and_value #77

Closed
wants to merge 1 commit into from

Conversation

FruitieX
Copy link

@FruitieX FruitieX commented Jan 17, 2024

This PR makes it possible to include paths in query results.

A new JsonPath::query_path_and_value function is exposed to avoid breaking backwards compatibility with existing users of JsonPath::query. The return value is a Vec<(Vec<String>, &Value)> where the Vec<String> represents a list of traversed keys and indices to arrive at the serde_json::Value.

For example:

use serde_json::json;
use serde_json_path::JsonPath;

fn main() -> Result<(), serde_json_path::ParseError> {
  let path = JsonPath::parse("$.foo[::2]")?;
  let value = json!({"foo": [1, 2, 3, 4]});
  let result = path.query_path_and_value(&value);
  assert_eq!(result[1].0, vec!["foo", "2"]);
  assert_eq!(result[1].1, 3);
  Ok(())
}

This implementation surely has room for optimization - I was lazy and in a hurry and just passed owned Vec<String> types everywhere.

I am not very familiar with the more advanced features of JSON Paths, but I did verify that the implementation works correctly in basic cases (accessing object members by keys, array indexing, wildcards and such)

@FruitieX
Copy link
Author

FruitieX commented Feb 2, 2024

Closing in favor of #78

@FruitieX FruitieX closed this Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant