pub struct Selector<'a> { /* private fields */ }Expand description
Represents the state of a JSON Path selection process.
It holds the root JSONB value and the intermediate results (items) found during
the execution of a JsonPath.
Implementations§
Source§impl<'a> Selector<'a>
impl<'a> Selector<'a>
Sourcepub fn new(root_jsonb: RawJsonb<'a>) -> Self
pub fn new(root_jsonb: RawJsonb<'a>) -> Self
Creates a new Selector for the given root RawJsonb.
§Arguments
root_jsonb- TheRawJsonbdata to select from.
Sourcepub fn select_values(
&mut self,
json_path: &'a JsonPath<'a>,
) -> Result<Vec<OwnedJsonb>, Error>
pub fn select_values( &mut self, json_path: &'a JsonPath<'a>, ) -> Result<Vec<OwnedJsonb>, Error>
Executes the JsonPath and collects all matching items into a Vec<OwnedJsonb>.
This function returns all matching elements as a Vec<OwnedJsonb>.
§Arguments
self- The JSONPath selector.json_path- The JSONPath expression.
§Returns
Ok(Vec<OwnedJsonb>)- A vector containing the selectedOwnedJsonbvalues.Err(Error)- If the JSONB data is invalid or if an error occurs during path evaluation.
§Examples
use lance_jsonb::jsonpath::parse_json_path;
use lance_jsonb::jsonpath::Selector;
use lance_jsonb::OwnedJsonb;
let jsonb_value = r#"{"a": {"b": [1, 2, 3]}, "c": 4}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = jsonb_value.as_raw();
let mut selector = Selector::new(raw_jsonb);
let path = parse_json_path("$.a.b[*]".as_bytes()).unwrap();
let result = selector.select_values(&path).unwrap();
assert_eq!(result.len(), 3);
assert_eq!(result[0].to_string(), "1");
assert_eq!(result[1].to_string(), "2");
assert_eq!(result[2].to_string(), "3");§See Also
RawJsonb::select_by_path.
Sourcepub fn select_value(
&mut self,
json_path: &'a JsonPath<'a>,
) -> Result<Option<OwnedJsonb>, Error>
pub fn select_value( &mut self, json_path: &'a JsonPath<'a>, ) -> Result<Option<OwnedJsonb>, Error>
Executes the JsonPath and returns a single value or an array of values.
If exactly one element matches, it is returned directly (wrapped in Some).
If multiple elements match, they are returned as a JSON array (wrapped in Some).
If no elements match, None is returned.
§Arguments
self- The JSONPath selector.json_path- The JSONPath expression.
§Returns
Ok(Some(OwnedJsonb))- A singleOwnedJsonbcontaining the matched values.Ok(None)- The path does not match any values.Err(Error)- If the JSONB data is invalid or if an error occurs during path evaluation.
§Examples
use lance_jsonb::jsonpath::parse_json_path;
use lance_jsonb::jsonpath::Selector;
use lance_jsonb::OwnedJsonb;
let jsonb_value = r#"{"a": [{"b": 1}, {"b": 2}], "c": 3}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = jsonb_value.as_raw();
let mut selector = Selector::new(raw_jsonb);
let path = parse_json_path("$.c".as_bytes()).unwrap(); // Matches a single value.
let result = selector.select_value(&path).unwrap();
assert_eq!(result.unwrap().to_string(), "3");
let path = parse_json_path("$.a[*].b".as_bytes()).unwrap(); // Matches multiple values.
let result = selector.select_value(&path).unwrap();
assert_eq!(result.unwrap().to_string(), "[1,2]");
let path = parse_json_path("$.x".as_bytes()).unwrap(); // No match.
let result = selector.select_value(&path).unwrap();
assert!(result.is_none());§See Also
RawJsonb::select_value_by_path.
Sourcepub fn exists(&mut self, json_path: &'a JsonPath<'a>) -> Result<bool, Error>
pub fn exists(&mut self, json_path: &'a JsonPath<'a>) -> Result<bool, Error>
Executes the JsonPath and checks if any item matches.
§Arguments
self- The JSONPath selector.json_path- The JSONPath expression.
§Returns
Ok(true)- If the JSON path exists.Ok(false)- If the JSON path does not exist.Err(Error)- If the JSONB data is invalid or if an error occurs during path evaluation. This could also indicate issues with thejson_pathitself.
§Examples
use lance_jsonb::jsonpath::parse_json_path;
use lance_jsonb::jsonpath::Selector;
use lance_jsonb::OwnedJsonb;
let jsonb_value = r#"{"a": {"b": [1, 2, 3]}, "c": 4}"#.parse::<OwnedJsonb>().unwrap();
let raw_jsonb = jsonb_value.as_raw();
let mut selector = Selector::new(raw_jsonb);
// Valid paths
let path1 = parse_json_path("$.a.b[1]".as_bytes()).unwrap();
assert!(selector.exists(&path1).unwrap());
let path2 = parse_json_path("$.c".as_bytes()).unwrap();
assert!(selector.exists(&path2).unwrap());
// Invalid paths
let path3 = parse_json_path("$.a.x".as_bytes()).unwrap(); // "x" does not exist
assert!(!selector.exists(&path3).unwrap());§See Also
RawJsonb::path_exists.
Auto Trait Implementations§
impl<'a> Freeze for Selector<'a>
impl<'a> RefUnwindSafe for Selector<'a>
impl<'a> Send for Selector<'a>
impl<'a> Sync for Selector<'a>
impl<'a> Unpin for Selector<'a>
impl<'a> UnsafeUnpin for Selector<'a>
impl<'a> UnwindSafe for Selector<'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
Mutably borrows from an owned value. Read more