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
json
namespace with everything you need:
json::primitive!(...)
– match a value inside a JSONValue
json::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 elements
Features
- Match JSON primitive values (
string
,number
,bool
) using thejson::primitive!
macro, and matchnull
values 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. - 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::Value
fields usingjson::predicate(|v| ...)
.
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!;
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 match:
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!;
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 |
💡 Note: All JSON matcher macros support both direct matchers (e.g.
starts_with("x")
) and explicitjson::primitive!(...)
wrappers. Use whichever makes intent clearer.
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.