Skip to main content

Selector

Struct Selector 

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

Source

pub fn new(root_jsonb: RawJsonb<'a>) -> Self

Creates a new Selector for the given root RawJsonb.

§Arguments
  • root_jsonb - The RawJsonb data to select from.
Source

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 selected OwnedJsonb 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, 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.
Source

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 single OwnedJsonb containing 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.
Source

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 the json_path itself.
§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> 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.