hiltontj / serde_json_path

Query serde_json Values with JSONPath
https://serdejsonpath.live/
MIT License
50 stars 3 forks source link

[9] Implement `JsonPath` for holding parsed JSON Path queries #10

Closed hiltontj closed 1 year ago

hiltontj commented 1 year ago

The JsonPath struct was introduced, which holds a parsed, valid JSON Path query. This can be used to query serde_json::Values. For example:

let value = json!({ "foo": { "bar": ["baz", "bop"] } });
let path = JsonPath::parse("$.foo.bar.*")?; // <- Note, this is fallible
let nodes = path.query(&value).all();       // <- while this is not
assert_eq!(nodes, vec!["baz", "bop"]);

The JsonPath type can be used to parse JSON Path query strings only once, and then re-use the parsed query. This separates the fallible step (the parsing) from the infallible step (the querying), which will be a quality of life improvement for the reasons discussed in #9.

In addition, FromStr and Deserialize implementions were included for the JsonPath type. This will allow the type to be used directly where Deserialize is used.

The docs and tests were updated to reflect the new API.

This would be a breaking change, so will require a bump to 0.5.x.

This aims to close #9 .