Namespace JsonAssertions
Classes
- JsonCanonicalizeOptions
Options for Canonicalize(string, Action<JsonCanonicalizeOptions>?): the JSON-path locations whose values are replaced with a stable token before the document is re-serialized, and the token text itself. Configured through the fluent ScrubPath(string) and WithScrubToken(string) methods inside the configure callback.
- JsonCanonicalizer
Produces a deterministic, structural canonical form of a JSON document: object keys sorted ordinally, stable two-space indentation, LF line endings, and every value preserved (so a new or removed field surfaces as a text diff), with optional JSON-path scrubbing of volatile fields. Unlike ReformatJson<T>(JsonTypeInfo<T>) this needs no typed
JsonSerializerContextand keeps unknown properties, which is what makes it suitable for pinning the whole shape of a response as a snapshot baseline.
- JsonDifference
The first structural difference found between two JSON documents by JsonEquivalence. Captures the location and a rendered description of each side at the point they diverge, so the difference outlives the JsonDocument it was found in (a JsonElement is valid only while its backing document is alive).
- JsonEquivalence
Framework-agnostic structural comparison of two JSON documents. Two documents are equivalent when they carry the same values at the same paths, independent of object-property order and of the lexical form of numbers (
1,1.0, and1e0are equal). Arrays are position-sensitive unless IgnoreArrayOrder() is enabled, and any path registered with IgnorePath(string) is excluded from the comparison. Compare(JsonElement, JsonElement, JsonEquivalenceOptions) returns the first JsonDifference found, or null when the documents are equivalent. ContainsAll(JsonElement, JsonElement, JsonEquivalenceOptions) performs a one-directional subset comparison (the actual document may carry properties beyond the expected set) and returns every difference rather than only the first.
- JsonEquivalenceOptions
Options for Compare(JsonElement, JsonElement, JsonEquivalenceOptions): the JSON-path locations excluded from the comparison, and whether arrays are compared order-insensitively. Configured through the fluent IgnorePath(string) and IgnoreArrayOrder() methods inside the configure callback.
- JsonExtraction
Extraction helpers that read a typed value out of a JSON document at a dot/bracket path and return it, for tests that need the value rather than an assertion: pulling an id from a response to drive the next request, or reading a count to cross-check against an array length. Each helper navigates with the same JsonPath resolver the assertions use and throws a JsonExtractionException whose message says where the path stopped and why, replacing a hand-rolled
JsonDocument.Parse(...).RootElement.GetProperty(...).GetInt32()chain (which throws bare BCL exceptions with no path context). These are plain extension methods, not TUnit assertions; the value they return is used directly.
- JsonExtractionException
Thrown by the
GetJsonValue/GetJsonString/GetJsonElementextraction helpers when the requested value cannot be produced: the path does not resolve, the value at the path is the wrong JSON kind, its text does not parse to the requested type, or the source text is not valid JSON. The message carries the same path-context diagnostics the assertions render (how far the path resolved and why it stopped), so an extraction failure explains itself rather than surfacing a bare KeyNotFoundException or FormatException from a hand-rolledGetProperty(...).GetInt32()chain.
- JsonFailureMessage
Renders failure-message text for the JSON assertions. The boolean / resolution primitives in JsonPath and JsonValueComparison remain authoritative for pass / fail; these helpers produce the human-readable explanation surfaced in the assertion exception when an assertion fails.
- JsonPath
Framework-agnostic JSON path navigation primitives. This
JsonAssertionsnamespace holds the matching core; the TUnit-specific fluent entry points live in the siblingJsonAssertions.TUnitnamespace. Both ship in the singleJsonAssertions.TUnitpackage: the two-namespace split keeps the same consumer feel as the rest of the assertion family (a framework-agnostic core plus a TUnit adapter) and future-proofs a package split if the bareJsonAssertionsidentifier ever becomes available.
- JsonRenderers
Static factories returning JSON-string renderers for composition with framework-agnostic snapshot or comparison consumers. Each method produces a Func<T, TResult> of string to string that maps a JSON document into a canonical re-serialized form via the consumer's JsonTypeInfo<T>. The output stability is the consumer's
JsonSerializerContext's stability — STJ source-gen produces deterministic property ordering, so the canonical form is reliable across runs.
- JsonShape
Framework-agnostic shape predicates for a resolved JsonElement: its value kind, and, for arrays, its length. Like JsonValueComparison, the predicates return false on a kind mismatch rather than throwing, so a caller can render a "found a String, expected an array" diagnostic instead of catching an exception.
- JsonValueComparison
Framework-agnostic value comparison for a resolved JsonElement. Each
Matchesoverload pairs a JSON value kind with a CLR expected value and reports whether the element both is of the matching kind and carries the expected value. Kind mismatches return false rather than throwing, so the caller can render a "found a String, expected a Number" diagnostic instead of catching an exception.
Structs
- JsonPathResolution
The outcome of resolving a dot-separated JSON path against a JsonElement. Carries enough context for an assertion to render a failure message that says where on the path resolution stopped, not merely that it did, the diagnostics that justify this being a package rather than a hand-rolled
TryGetProperty(...).IsTrue()helper.
Enums
- JsonDifferenceKind
The category of a JsonDifference.