pub struct JsonPath { /* private fields */ }Expand description
Type-safe JSON path for addressing nodes in JSON structures.
Represented as a sequence of PathSegments; the root path ($) is the
empty sequence. Every constructor funnels object keys through the same
validation rule (see validate_key), so a JsonPath can never contain
a key that would make Display/FromStr ambiguous — see the invariant
documented on JsonPath’s Display impl (JP-1).
Implementations§
Source§impl JsonPath
impl JsonPath
Sourcepub fn new(path: impl Into<String>) -> Result<JsonPath, DomainError>
pub fn new(path: impl Into<String>) -> Result<JsonPath, DomainError>
Parse a JSON path from its textual form (e.g. "$.users[0].name").
§Examples
use pjson_rs_domain::value_objects::JsonPath;
let path = JsonPath::new("$.users[0].name").unwrap();
assert_eq!(path.depth(), 3);
assert_eq!(path.to_string(), "$.users[0].name");
assert!(JsonPath::new("$.key[not_a_number]").is_err());Sourcepub fn from_segments(
segments: impl IntoIterator<Item = PathSegment>,
) -> Result<JsonPath, DomainError>
pub fn from_segments( segments: impl IntoIterator<Item = PathSegment>, ) -> Result<JsonPath, DomainError>
Build a path directly from segments, validating every PathSegment::Key
with the same rule as JsonPath::append_key.
§Examples
use pjson_rs_domain::value_objects::{JsonPath, PathSegment};
let path = JsonPath::from_segments(vec![
PathSegment::Key("users".to_string()),
PathSegment::Index(0),
])
.unwrap();
assert_eq!(path.to_string(), "$.users[0]");
// A key containing a delimiter is rejected, just like `append_key`.
let invalid = JsonPath::from_segments(vec![PathSegment::Key("a.b".to_string())]);
assert!(invalid.is_err());Sourcepub fn append_key(&self, key: &str) -> Result<JsonPath, DomainError>
pub fn append_key(&self, key: &str) -> Result<JsonPath, DomainError>
Append a key segment, producing a new path.
§Examples
use pjson_rs_domain::value_objects::JsonPath;
let path = JsonPath::root().append_key("users").unwrap();
assert_eq!(path.to_string(), "$.users");
// Keys containing '.', '[', ']', or the empty key are rejected.
assert!(JsonPath::root().append_key("").is_err());
assert!(JsonPath::root().append_key("a.b").is_err());Sourcepub fn append_index(&self, index: usize) -> JsonPath
pub fn append_index(&self, index: usize) -> JsonPath
Append an array index segment, producing a new path.
Sourcepub fn segments(&self) -> &[PathSegment]
pub fn segments(&self) -> &[PathSegment]
Borrow the path’s segments.
Sourcepub fn parent(&self) -> Option<JsonPath>
pub fn parent(&self) -> Option<JsonPath>
Get the parent path, or None if this is the root.
O(1) on the segmented representation. This corrects a bug in the
previous string-based implementation, which returned root for any
path ending in an index segment following a key (e.g. $.users[0]
incorrectly produced $ instead of $.users).
Sourcepub fn last_segment(&self) -> Option<&PathSegment>
pub fn last_segment(&self) -> Option<&PathSegment>
Get the last segment of the path, or None at root.
Distinct from JsonPath::last_key: this returns the literal final
segment, whether it is a key or an index.
Sourcepub fn last_key(&self) -> Option<&str>
pub fn last_key(&self) -> Option<&str>
Get the last Key segment, skipping any trailing Index segments.
Distinct from JsonPath::last_segment: for $.arr[5] this returns
Some("arr"), not None. Preserves the WASM/HTTP priority-heuristic
parity fixed in #242 — do not conflate the two methods.
Sourcepub fn is_prefix_of(&self, other: &JsonPath) -> bool
pub fn is_prefix_of(&self, other: &JsonPath) -> bool
Check whether self is a strict prefix of other (self-prefix is false).
Sourcepub fn to_json_pointer(&self) -> String
pub fn to_json_pointer(&self) -> String
Convert to a JSON Pointer (RFC 6901) string.
Does not escape ~ or / within keys; see follow-up issue for #379.
§Examples
use pjson_rs_domain::value_objects::JsonPath;
let path = JsonPath::new("$.users[0].name").unwrap();
assert_eq!(path.to_json_pointer(), "/users/0/name");
assert_eq!(JsonPath::root().to_json_pointer(), "/");Trait Implementations§
Source§impl<'de> Deserialize<'de> for JsonPath
impl<'de> Deserialize<'de> for JsonPath
Source§fn deserialize<D>(
deserializer: D,
) -> Result<JsonPath, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<JsonPath, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Display for JsonPath
INVARIANT (JP-1): Display is injective and total over representable
JsonPath values. It holds only because key validation (validate_key)
excludes ., [, ], and the empty key: those delimiters cannot occur
inside a valid key, so the boundary between a key and the next segment
marker is always unambiguous, and every rendered path re-parses via
FromStr to the same segments. Any future change that widens the key
alphabet to admit ., [, ], or the empty string must add an
escaping grammar (e.g. bracket-quote form with backslash-escaping) and a
round-trip proptest in the same change, or Display/FromStr become a
path-forgery primitive (see issue #333).
impl Display for JsonPath
INVARIANT (JP-1): Display is injective and total over representable
JsonPath values. It holds only because key validation (validate_key)
excludes ., [, ], and the empty key: those delimiters cannot occur
inside a valid key, so the boundary between a key and the next segment
marker is always unambiguous, and every rendered path re-parses via
FromStr to the same segments. Any future change that widens the key
alphabet to admit ., [, ], or the empty string must add an
escaping grammar (e.g. bracket-quote form with backslash-escaping) and a
round-trip proptest in the same change, or Display/FromStr become a
path-forgery primitive (see issue #333).
impl Eq for JsonPath
Source§impl FromStr for JsonPath
Parses the textual form produced by JsonPath’s Display impl.
See the injectivity/totality invariant documented there (JP-1).
impl FromStr for JsonPath
Parses the textual form produced by JsonPath’s Display impl.
See the injectivity/totality invariant documented there (JP-1).
Source§impl Serialize for JsonPath
impl Serialize for JsonPath
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl StructuralPartialEq for JsonPath
Auto Trait Implementations§
impl Freeze for JsonPath
impl RefUnwindSafe for JsonPath
impl Send for JsonPath
impl Sync for JsonPath
impl Unpin for JsonPath
impl UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more