Trait Queryable

Source
pub trait Queryable
where Self: Default + Clone + Debug + for<'a> From<&'a str> + From<bool> + From<i64> + From<f64> + From<Vec<Self>> + From<String> + PartialEq,
{ // Required methods fn get(&self, key: &str) -> Option<&Self>; fn as_array(&self) -> Option<&Vec<Self>>; fn as_object(&self) -> Option<Vec<(&String, &Self)>>; fn as_str(&self) -> Option<&str>; fn as_i64(&self) -> Option<i64>; fn as_f64(&self) -> Option<f64>; fn as_bool(&self) -> Option<bool>; fn null() -> Self; // Provided methods fn extension_custom(_name: &str, _args: Vec<Cow<'_, Self>>) -> Self { ... } fn reference<T>(&self, _path: T) -> Option<&Self> where T: Into<QueryPath> { ... } fn reference_mut<T>(&mut self, _path: T) -> Option<&mut Self> where T: Into<QueryPath> { ... } }
Expand description

A trait that abstracts JSON-like data structures for JSONPath queries

This trait provides the essential operations needed to traverse and query hierarchical data structures in a JSONPath-compatible way. Implementors of this trait can be used with the JSONPath query engine.

The trait requires several standard type conversions to be implemented to ensure that query operations can properly handle various data types.

§Type Requirements

Implementing types must satisfy these trait bounds:

  • Default: Provides a default value for the type
  • Clone: Allows creation of copies of values
  • Debug: Enables debug formatting
  • From<&str>: Conversion from string slices
  • From<bool>: Conversion from boolean values
  • From<i64>: Conversion from 64-bit integers
  • From<f64>: Conversion from 64-bit floating point values
  • From<Vec<Self>>: Conversion from vectors of the same type
  • From<String>: Conversion from owned strings
  • PartialEq: Allows equality comparisons

§Examples

The trait is primarily implemented for serde_json::Value to enable JSONPath queries on JSON data structures:

use serde_json::json;
use jsonpath_rust::JsonPath;

let data = json!({
    "store": {
        "books": [
            {"title": "Book 1", "price": 10},
            {"title": "Book 2", "price": 15}
        ]
    }
});

// Access data using the Queryable trait
let books = data.query("$.store.books[*].title").expect("no errors");

Required Methods§

Source

fn get(&self, key: &str) -> Option<&Self>

Retrieves a reference to the value associated with the given key. It is the responsibility of the implementation to handle enclosing single and double quotes. The key will be normalized (quotes trimmed, whitespace handled, the escape symbols handled) before lookup.

Source

fn as_array(&self) -> Option<&Vec<Self>>

Source

fn as_object(&self) -> Option<Vec<(&String, &Self)>>

Source

fn as_str(&self) -> Option<&str>

Source

fn as_i64(&self) -> Option<i64>

Source

fn as_f64(&self) -> Option<f64>

Source

fn as_bool(&self) -> Option<bool>

Source

fn null() -> Self

Returns a null value.

Provided Methods§

Source

fn extension_custom(_name: &str, _args: Vec<Cow<'_, Self>>) -> Self

Source

fn reference<T>(&self, _path: T) -> Option<&Self>
where T: Into<QueryPath>,

Retrieves a reference to the element at the specified path. The path is specified as a string and can be obtained from the query.

§Arguments
  • path - A json path to the element specified as a string (root, field, index only).
Source

fn reference_mut<T>(&mut self, _path: T) -> Option<&mut Self>
where T: Into<QueryPath>,

Retrieves a mutable reference to the element at the specified path.

§Arguments
  • path - A json path to the element specified as a string (root, field, index only).
§Examples
use serde_json::json;
use jsonpath_rust::JsonPath;
use jsonpath_rust::query::queryable::Queryable;
let mut json = json!({
            "a": {
                "b": {
                    "c": 42
                }
            }
        });
        if let Some(path) = json.query_only_path("$.a.b.c").unwrap().first() {
            if let Some(v) = json.reference_mut("$.a.b.c") {
                *v = json!(43);
            }

            assert_eq!(
                json,
                json!({
                    "a": {
                        "b": {
                            "c": 43
                        }
                    }
                })
            );
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Queryable for Value

Source§

fn extension_custom(name: &str, args: Vec<Cow<'_, Self>>) -> Self

Custom extension function for JSONPath queries.

This function allows for custom operations to be performed on JSON data based on the provided name and args.

§Arguments
  • name - A string slice that holds the name of the custom function.
  • args - A vector of Cow<Self> that holds the arguments for the custom function.
§Returns

Returns a Self value which is the result of the custom function. If the function name is not recognized, it returns Self::null().

§Custom Functions
  • "in" - Checks if the first argument is in the array provided as the second argument. Example: $.elems[?in(@, $.list)] - Returns elements from $.elems that are present in $.list

  • "nin" - Checks if the first argument is not in the array provided as the second argument. Example: $.elems[?nin(@, $.list)] - Returns elements from $.elems that are not present in $.list

  • "none_of" - Checks if none of the elements in the first array are in the second array. Example: $.elems[?none_of(@, $.list)] - Returns arrays from $.elems that have no elements in common with $.list

  • "any_of" - Checks if any of the elements in the first array are in the second array. Example: $.elems[?any_of(@, $.list)] - Returns arrays from $.elems that have at least one element in common with $.list

  • "subset_of" - Checks if all elements in the first array are in the second array. Example: $.elems[?subset_of(@, $.list)] - Returns arrays from $.elems where all elements are present in $.list

Source§

fn get(&self, key: &str) -> Option<&Self>

Source§

fn as_array(&self) -> Option<&Vec<Self>>

Source§

fn as_object(&self) -> Option<Vec<(&String, &Self)>>

Source§

fn as_str(&self) -> Option<&str>

Source§

fn as_i64(&self) -> Option<i64>

Source§

fn as_f64(&self) -> Option<f64>

Source§

fn as_bool(&self) -> Option<bool>

Source§

fn null() -> Self

Source§

fn reference<T>(&self, path: T) -> Option<&Self>
where T: Into<QueryPath>,

Source§

fn reference_mut<T>(&mut self, path: T) -> Option<&mut Self>
where T: Into<QueryPath>,

Implementors§