pub struct JsonPath(/* private fields */);
Expand description
A parsed JSON Path query string
This type represents a valid, parsed JSON Path query string. Please refer to the JSONPath standard (RFC 9535) for the details on what constitutes a valid JSON Path query.
§Usage
A JsonPath
can be parsed directly from an &str
using the parse
method:
let path = JsonPath::parse("$.foo.*").expect("valid JSON Path");
It can then be used to query serde_json::Value
’s with the query
method:
let value = json!({"foo": [1, 2, 3, 4]});
let nodes = path.query(&value);
assert_eq!(nodes.all(), vec![1, 2, 3, 4]);
Implementations§
Source§impl JsonPath
impl JsonPath
Sourcepub fn parse(path_str: &str) -> Result<Self, ParseError>
pub fn parse(path_str: &str) -> Result<Self, ParseError>
Sourcepub fn query<'b>(&self, value: &'b Value) -> NodeList<'b>
pub fn query<'b>(&self, value: &'b Value) -> NodeList<'b>
Query a serde_json::Value
using this JsonPath
§Example
let value = json!({"foo": [1, 2, 3, 4]});
let path = JsonPath::parse("$.foo[::2]")?;
let nodes = path.query(&value);
assert_eq!(nodes.all(), vec![1, 3]);
Sourcepub fn query_located<'b>(&self, value: &'b Value) -> LocatedNodeList<'b>
pub fn query_located<'b>(&self, value: &'b Value) -> LocatedNodeList<'b>
Query a serde_json::Value
using this JsonPath
to produce a LocatedNodeList
§Example
let value = json!({"foo": {"bar": 1, "baz": 2}});
let path = JsonPath::parse("$.foo.*")?;
let query = path.query_located(&value);
let nodes: Vec<&Value> = query.nodes().collect();
assert_eq!(nodes, vec![1, 2]);
let locs: Vec<String> = query
.locations()
.map(|loc| loc.to_string())
.collect();
assert_eq!(locs, ["$['foo']['bar']", "$['foo']['baz']"]);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for JsonPath
impl<'de> Deserialize<'de> for JsonPath
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for JsonPath
impl StructuralPartialEq for JsonPath
Auto Trait Implementations§
impl Freeze for JsonPath
impl !RefUnwindSafe for JsonPath
impl Send for JsonPath
impl Sync for JsonPath
impl Unpin for JsonPath
impl !UnwindSafe for JsonPath
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)