serde_json_path

Struct NodeList

Source
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>

Source

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)));
Source

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)));
Source

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"]);
Source

pub fn len(&self) -> usize

Get the length of a NodeList

Source

pub fn is_empty(&self) -> bool

Check if a NodeList is empty

Source

pub fn iter(&self) -> Iter<'_, &Value>

Get an iterator over a NodeList

Note that NodeList also implements IntoIterator.

Source

pub fn first(&self) -> Option<&'a Value>

Returns the first node in the NodeList, or None if it is empty

§Usage
let value = json!({"foo": ["bar", "baz"]});
let path = JsonPath::parse("$.foo.*")?;
let node = path.query(&value).first();
assert_eq!(node, Some(&json!("bar")));
Source

pub fn last(&self) -> Option<&'a Value>

Returns the last node in the NodeList, or None if it is empty

§Usage
let value = json!({"foo": ["bar", "baz"]});
let path = JsonPath::parse("$.foo.*")?;
let node = path.query(&value).last();
assert_eq!(node, Some(&json!("baz")));
Source

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());
Source

pub 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

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).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> Clone for NodeList<'a>

Source§

fn clone(&self) -> NodeList<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for NodeList<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for NodeList<'a>

Source§

fn default() -> NodeList<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> From<NodeList<'a>> for NodesType<'a>

Source§

fn from(value: NodeList<'a>) -> NodesType<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Vec<&'a Value>> for NodeList<'a>

Source§

fn from(nodes: Vec<&'a Value>) -> NodeList<'a>

Converts to this type from the input type.
Source§

impl<'a> IntoIterator for NodeList<'a>

Source§

type Item = &'a Value

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<NodeList<'a> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <NodeList<'a> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> PartialEq for NodeList<'a>

Source§

fn eq(&self, other: &NodeList<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'a> Eq for NodeList<'a>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.