pub trait Queryablewhere
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 typeClone
: Allows creation of copies of valuesDebug
: Enables debug formattingFrom<&str>
: Conversion from string slicesFrom<bool>
: Conversion from boolean valuesFrom<i64>
: Conversion from 64-bit integersFrom<f64>
: Conversion from 64-bit floating point valuesFrom<Vec<Self>>
: Conversion from vectors of the same typeFrom<String>
: Conversion from owned stringsPartialEq
: 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§
Sourcefn get(&self, key: &str) -> Option<&Self>
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.
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>
Provided Methods§
fn extension_custom(_name: &str, _args: Vec<Cow<'_, Self>>) -> Self
Sourcefn reference<T>(&self, _path: T) -> Option<&Self>
fn reference<T>(&self, _path: T) -> Option<&Self>
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).
Sourcefn reference_mut<T>(&mut self, _path: T) -> Option<&mut Self>
fn reference_mut<T>(&mut self, _path: T) -> Option<&mut Self>
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
impl Queryable for Value
Source§fn extension_custom(name: &str, args: Vec<Cow<'_, Self>>) -> Self
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 ofCow<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