pub struct NodeList<'a>(/* private fields */);
Expand description
A list of nodes resulting from a JSONPath query
Each node within the list is a borrowed reference to the node in the original
serde_json::Value
that was queried.
Implementations§
Source§impl<'a> NodeList<'a>
impl<'a> NodeList<'a>
Sourcepub fn at_most_one(&self) -> Result<Option<&'a Value>, AtMostOneError>
pub fn at_most_one(&self) -> Result<Option<&'a Value>, AtMostOneError>
Extract at most one node from a NodeList
This is intended for queries that are expected to optionally yield a single node.
§Usage
let value = json!({"foo": ["bar", "baz"]});
let path = JsonPath::parse("$.foo[0]")?;
let node = path.query(&value).at_most_one().unwrap();
assert_eq!(node, Some(&json!("bar")));
let path = JsonPath::parse("$.foo.*")?;
let error = path.query(&value).at_most_one().unwrap_err();
assert!(matches!(error, AtMostOneError(2)));
Sourcepub fn exactly_one(&self) -> Result<&'a Value, ExactlyOneError>
pub fn exactly_one(&self) -> Result<&'a Value, ExactlyOneError>
Extract exactly one node from a NodeList
This is intended for queries that are expected to yield exactly one node.
§Usage
let value = json!({"foo": ["bar", "baz"]});
let path = JsonPath::parse("$.foo[0]")?;
let node = path.query(&value).exactly_one().unwrap();
assert_eq!(node, "bar");
let path = JsonPath::parse("$.foo.*")?;
let error = path.query(&value).exactly_one().unwrap_err();
assert!(matches!(error, ExactlyOneError::MoreThanOne(2)));
Sourcepub fn all(self) -> Vec<&'a Value>
pub fn all(self) -> Vec<&'a Value>
Extract all nodes yielded by the query
This is intended for queries that are expected to yield zero or more nodes.
§Usage
let value = json!({"foo": ["bar", "baz"]});
let path = JsonPath::parse("$.foo.*")?;
let nodes = path.query(&value).all();
assert_eq!(nodes, vec!["bar", "baz"]);
Sourcepub fn iter(&self) -> Iter<'_, &Value>
pub fn iter(&self) -> Iter<'_, &Value>
Get an iterator over a NodeList
Note that NodeList
also implements IntoIterator
.
Sourcepub fn get(&self, index: usize) -> Option<&'a Value>
pub fn get(&self, index: usize) -> Option<&'a Value>
Returns the node at the given index in the NodeList
, or None
if the given index is
out of bounds.
§Usage
let value = json!({"foo": ["bar", "biz", "bop"]});
let path = JsonPath::parse("$.foo.*")?;
let nodes = path.query(&value);
assert_eq!(nodes.get(1), Some(&json!("biz")));
assert!(nodes.get(4).is_none());
Sourcepub fn one(self) -> Option<&'a Value>
👎Deprecated since 0.5.1: it is recommended to use at_most_one
, exactly_one
, first
, last
, or get
instead
pub fn one(self) -> Option<&'a Value>
at_most_one
, exactly_one
, first
, last
, or get
insteadExtract at most one node from a NodeList
This is intended for queries that are expected to optionally yield a single node.
§Usage
let value = json!({"foo": ["bar", "baz"]});
let path = JsonPath::parse("$.foo[0]")?;
let node = path.query(&value).one();
assert_eq!(node, Some(&json!("bar")));
let path = JsonPath::parse("$.foo.*")?;
let node = path.query(&value).one();
assert!(node.is_none());
Trait Implementations§
Source§impl<'a> IntoIterator for NodeList<'a>
impl<'a> IntoIterator for NodeList<'a>
Source§impl<'a> Serialize for NodeList<'a>
impl<'a> Serialize for NodeList<'a>
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl<'a> Eq for NodeList<'a>
impl<'a> StructuralPartialEq for NodeList<'a>
Auto Trait Implementations§
impl<'a> Freeze for NodeList<'a>
impl<'a> RefUnwindSafe for NodeList<'a>
impl<'a> Send for NodeList<'a>
impl<'a> Sync for NodeList<'a>
impl<'a> Unpin for NodeList<'a>
impl<'a> UnwindSafe for NodeList<'a>
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
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)
clone_to_uninit
)