Getting Started
Installation
You can install this crate as a development dependency using Cargo:
This crate is typically only needed as a dev-dependency.
In tests:
use *;
use json;
use json as j;
The crate re-exports a
jsonnamespace with everything you need:
json::primitive!(...)– match a value inside a JSONValuejson::matches_pattern!– explicitly match JSON objects by fields (withjson::pat!as an alias)json::elements_are![...]– match arrays element-by-element (ordered)json::unordered_elements_are![...]– match arrays element-by-element (unordered)json::contains_each![...]– require each matcher to match a unique element, extra allowedjson::is_contained_in![...]– assert containment of JSON elementsjson::is_empty_object()– match{}specificallyjson::has_paths(&[...])/json::has_only_paths(&[...])– assert required or exact object paths (supports.separators and array indices)
Features
- Match JSON primitive values (
string,number,bool) using thejson::primitive!macro, and matchnullvalues withjson::is_null(). - Pattern-match JSON objects by fields using the
json::matches_pattern!{...}macro; support both strict mode ( all fields must match and no extra fields) and non-strict mode (extra fields allowed via..). - Match arrays element-by-element (ordered, supports heterogeneous types) using the
json::elements_are![...]macro. - Match arrays where order does not matter using the
json::unordered_elements_are![...]macro. - Require each expected matcher to match a unique element in any order, allowing extra elements, with the
json::contains_each![...]macro. - Assert that an array is a subset of the expected matchers (every actual element is accounted for) using the
json::is_contained_in![...]macro. - Check object shape quickly with
json::is_empty_object(),json::has_paths(&[...]), andjson::has_only_paths(&[...]), supporting dot-paths (e.g.,user.id,items.0.id). - Provide clear, structured diagnostic failure messages showing which part of the JSON structure did not match and why.
- Helper matchers for validating JSON kinds and structure:
json::is_null(),json::is_not_null(),json::is_string(),json::is_number(),json::is_boolean(),json::is_array(),json::is_object(). - Custom predicates for ad‑hoc checks on
serde_json::Valuefields usingjson::predicate(|v| ...). - Direct
serde_json::Valuesupport: JSON matcher macros now accept directserde_json::Valuevalues for structural equality checks, equivalent to usingeq(json!(...)).
Examples
Primitives
Match JSON primitives with json::primitive
use *;
use json;
use json as j;
assert_that!;
assert_that!;
assert_that!;
assert_that!;
assert_that!;
assert_that!;
assert_that!;
assert_that!;
Predicates
Create custom matchers with json::predicate
use *;
use json;
use json as j;
// Match a number greater than zero
assert_that!;
// Match a string containing "foo"
assert_that!;
Objects
Match JSON objects with json::matches_pattern!
Strict and non-strict match examples with mixed usage of direct serde values and traditional matchers:
use *;
use json;
use json as j;
let v = j!;
assert_that!;
Non-strict match (with ..):
use *;
use json;
use json as j;
let v = j!;
assert_that!;
Arrays
Match arrays with json::elements_are! (ordered)
use *;
use json;
use json as j;
assert_that!;
assert_that!;
Match arrays with json::unordered_elements_are! (unordered)
use *;
use json;
use json as j;
assert_that!;
assert_that!;
Assert containment with json::is_contained_in!
use *;
use json;
use json as j;
assert_that!;
assert_that!;
Assert each matcher finds a unique element with json::contains_each!
use *;
use json;
use json as j;
// Array can have extra elements, but must contain all required ones
assert_that!;
assert_that!;
Assert object shape with key helpers
use *;
use json;
use json as j;
assert_that!;
assert_that!;
assert_that!;
Assert each element satisfies a matcher with json::each!
use *;
use json;
use json as j;
assert_that!;
assert_that!;
Assert array length with json::len!
use *;
use json;
use json as j;
assert_that!;
assert_that!;
assert_that!;
Array Matcher Quick Reference
Here’s a quick reference matrix comparing the array matchers:
| Matcher | Order Matters | Extra Elements OK | Missing Elements OK | Use Case |
|---|---|---|---|---|
elements_are! |
Yes | No | No | Exact ordered match of all elements |
unordered_elements_are! |
No | No | No | Exact unordered match of all elements |
contains_each! |
No | Yes | No | Require each matcher to match a unique element, extra allowed |
is_contained_in! |
No | No | Yes | Actual elements are subset of expected |
each! |
Yes | Yes | Yes | Assert each element satisfies the given matcher |
Combined example
Compose all together for complex structures. Here is a Rust struct with a JSON field, using matches_pattern! for the
struct and nested JSON matchers for the field:
use *;
use json;
use json as j;
let resp = Response ;
assert_that!;
Acknowledgements
Parts of this crate are adapted from googletest-rust, which is licensed under Apache 2.0.