pub struct JsonPartial { /* private fields */ }
Expand description
Matches on JSON patterns, useful for matching on all messages that have a certain field, or matching data of only some type.
JsonPartial
takes a serde_json
Value
, and will match on anything that exhibits the same
structure and matches all primitives and arrays given by the pattern. Objects are matched recursively,
meaning that any keys present in the pattern must be present and compare equally to those in the
object being matched, but the object being matched can have other keys not present in the pattern.
§Example: Matching by Message Type
Matching on only data with a particular field (without caring about additional data) can be useful for matching messages of a particular type, such as disregarding heartbeats or metadata to focus on data messages only.
use serde_json::json;
use ws_mock::matchers::{JsonExact, JsonPartial, Matcher};
let heartbeat = r#"{"type": "heartbeat"}"#;
let data = r#"{"type": "data", "data": [0, 1, 0]}"#;
let metadata = r#"{"type": "metadata", "data": "details"}"#;
let pattern = json!({"type": "data"});
let matcher = JsonPartial::new(pattern);
assert!(!matcher.matches(heartbeat));
assert!(matcher.matches(data));
assert!(!matcher.matches(metadata));
Implementations§
Trait Implementations§
source§impl Debug for JsonPartial
impl Debug for JsonPartial
Auto Trait Implementations§
impl Freeze for JsonPartial
impl RefUnwindSafe for JsonPartial
impl Send for JsonPartial
impl Sync for JsonPartial
impl Unpin for JsonPartial
impl UnwindSafe for JsonPartial
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
Mutably borrows from an owned value. Read more