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

source

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)]
);
source

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

source

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})]));
source

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§

source§

impl Clone for JsonPath

source§

fn clone(&self) -> JsonPath

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for JsonPath

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for JsonPath

source§

fn from_str(value: &str) -> Result<Self, Self::Err>

Parses a string into a JsonPath.

§Errors

Returns a variant of JsonPathParserError if the parsing operation failed.

§

type Err = JsonPathParserError

The associated error which can be returned from parsing.
source§

impl PartialEq for JsonPath

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl TryFrom<&str> for JsonPath

source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Parses a string into a JsonPath.

§Errors

Returns a variant of JsonPathParserError if the parsing operation failed.

§

type Error = JsonPathParserError

The type returned in the event of a conversion error.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.