pub enum JsonPath<T = Value> {
Root,
Field(String),
Chain(Vec<JsonPath<T>>),
Descent(String),
DescentW,
Index(JsonPathIndex<T>),
Current(Box<JsonPath<T>>),
Wildcard,
Empty,
Fn(Function),
}
Expand description
The basic structures for parsing json paths. The common logic of the structures pursues to correspond the internal parsing structure.
usually it’s created by using FromStr
or TryFrom<&str>
Variants§
Root
The $ operator
Field(String)
Field represents key
Chain(Vec<JsonPath<T>>)
The whole chain of the path.
Descent(String)
The .. operator
DescentW
The ..* operator
Index(JsonPathIndex<T>)
The indexes for array
Current(Box<JsonPath<T>>)
The @ operator
Wildcard
The * operator
Empty
The item uses to define the unresolved state
Fn(Function)
Functions that can calculate some expressions
Implementations§
Source§impl<T> JsonPath<T>where
T: JsonLike,
impl<T> JsonPath<T>where
T: JsonLike,
Sourcepub fn find_slice<'a>(&'a self, json: &'a T) -> Vec<JsonPathValue<'a, T>>
pub fn find_slice<'a>(&'a self, json: &'a T) -> Vec<JsonPathValue<'a, T>>
finds a slice of data in the set json. The result is a vector of references to the incoming structure.
In case, if there is no match Self::find_slice
will return vec!<JsonPathValue::NoValue
>.
§Example
use jsonpath_rust::{JsonPath, JsonPathValue};
use serde_json::json;
let data = json!({"first":{"second":[{"active":1},{"passive":1}]}});
let path = JsonPath::try_from("$.first.second[?(@.active)]").unwrap();
let slice_of_data = path.find_slice(&data);
let expected_value = json!({"active":1});
let expected_path = "$.['first'].['second'][0]".to_string();
assert_eq!(
slice_of_data,
vec![JsonPathValue::Slice(&expected_value, expected_path)]
);
Sourcepub fn find_slice_ptr<'a>(&'a self, json: &'a T) -> Vec<JsonPtr<'a, T>>
pub fn find_slice_ptr<'a>(&'a self, json: &'a T) -> Vec<JsonPtr<'a, T>>
like Self::find_slice
but returns a vector of JsonPtr
, which has no JsonPathValue::NoValue
.
if there is no match, it will return an empty vector
Sourcepub fn find(&self, json: &T) -> T
pub fn find(&self, json: &T) -> T
finds a slice of data and wrap it with Value::Array by cloning the data. Returns either an array of elements or Json::Null if the match is incorrect.
In case, if there is no match find
will return json!(null)
.
§Example
use jsonpath_rust::{JsonPath, JsonPathValue};
use serde_json::{Value, json};
let data = json!({"first":{"second":[{"active":1},{"passive":1}]}});
let path = JsonPath::try_from("$.first.second[?(@.active)]").unwrap();
let cloned_data = path.find(&data);
assert_eq!(cloned_data, Value::Array(vec![json!({"active":1})]));
Sourcepub fn find_as_path(&self, json: &T) -> T
pub fn find_as_path(&self, json: &T) -> T
finds a path describing the value, instead of the value itself. If the values has been obtained by moving the data out of the initial json the path is absent.
** If the value has been modified during the search, there is no way to find a path of a new value. It can happen if we try to find a length() of array, for in stance.**
§Example
use jsonpath_rust::{JsonPath, JsonPathValue};
use serde_json::{Value, json};
let data = json!({"first":{"second":[{"active":1},{"passive":1}]}});
let path = JsonPath::try_from("$.first.second[?(@.active)]").unwrap();
let slice_of_data: Value = path.find_as_path(&data);
let expected_path = "$.['first'].['second'][0]".to_string();
assert_eq!(slice_of_data, Value::Array(vec![Value::String(expected_path)]));
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for JsonPath<T>where
T: Freeze,
impl<T> RefUnwindSafe for JsonPath<T>where
T: RefUnwindSafe,
impl<T> Send for JsonPath<T>where
T: Send,
impl<T> Sync for JsonPath<T>where
T: Sync,
impl<T> Unpin for JsonPath<T>where
T: Unpin,
impl<T> UnwindSafe for JsonPath<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)