Enum jsonpath_rust::JsonPath
source · pub enum JsonPath {
Root,
Field(String),
Chain(Vec<JsonPath>),
Descent(String),
DescentW,
Index(JsonPathIndex),
Current(Box<JsonPath>),
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>)
The whole chain of the path.
Descent(String)
The .. operator
DescentW
The ..* operator
Index(JsonPathIndex)
The indexes for array
Current(Box<JsonPath>)
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 JsonPath
impl JsonPath
sourcepub fn find_slice<'a>(
&'a self,
json: &'a Value,
) -> Vec<JsonPathValue<'a, Value>>
pub fn find_slice<'a>( &'a self, json: &'a Value, ) -> Vec<JsonPathValue<'a, Value>>
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 Value) -> Vec<JsonPtr<'a, Value>>
pub fn find_slice_ptr<'a>(&'a self, json: &'a Value) -> Vec<JsonPtr<'a, Value>>
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: &Value) -> Value
pub fn find(&self, json: &Value) -> Value
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: &Value) -> Value
pub fn find_as_path(&self, json: &Value) -> Value
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 Freeze for JsonPath
impl RefUnwindSafe for JsonPath
impl Send for JsonPath
impl Sync for JsonPath
impl Unpin for JsonPath
impl UnwindSafe for JsonPath
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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)