jsonpath_rust

Enum JsonPath

Source
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,

Source

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

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

Source

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

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§

Source§

impl<T: Clone> Clone for JsonPath<T>

Source§

fn clone(&self) -> JsonPath<T>

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<T: Debug> Debug for JsonPath<T>

Source§

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

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

impl<T: Display> Display for JsonPath<T>

Source§

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

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

impl<T> FromStr for JsonPath<T>
where T: JsonLike,

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.

Source§

type Err = JsonPathParserError

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

impl<T> PartialEq for JsonPath<T>
where T: PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> TryFrom<&str> for JsonPath<T>
where T: JsonLike,

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.

Source§

type Error = JsonPathParserError

The type returned in the event of a conversion error.

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> 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§

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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.