Table of Contents

Class JsonRenderers

Namespace
JsonAssertions
Assembly
JsonAssertions.TUnit.dll

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.

public static class JsonRenderers
Inheritance
JsonRenderers
Inherited Members

Remarks

Used to compose with SnapshotAssertions's MatchesSnapshot(Func<string, string>) overload (or any other framework-agnostic consumer of Func<string, string>). The projection-helper pattern keeps JsonAssertions decoupled from SnapshotAssertions — the composition happens at the consumer's call site via standard delegate types, per the family's CONVENTIONS.md v0.6 cross-package references rule.

Two-step composition (async body-read in test code + sync renderer here):

var body = await response.Content.ReadAsStringAsync(ct);
await Assert.That(body).MatchesSnapshot(JsonRenderers.ReformatJson(MyJsonContext.Default.MyDto));

The two-step pattern (async body-read + sync renderer) honors the family's "no sync-over-async" rule: the renderer is purely synchronous (parsing strings, not reading streams). HttpResponseMessage body-reading remains async in the consumer's code.

Lives in the JsonAssertions namespace ("core" namespace; framework-agnostic) rather than JsonAssertions.TUnit (adapter), because the helper returns standard BCL delegate types and depends only on System.Text.Json. A hypothetical future non-TUnit framework adapter could compose with the same renderer.

Methods

ReformatJson<T>(JsonTypeInfo<T>)

Returns a synchronous renderer that takes a JSON string, deserializes it via the supplied jsonTypeInfo, and re-serializes the result via the same JsonTypeInfo<T>. The output is the canonical form per the consumer's JsonSerializerContext options: property ordering follows the context's source-gen emission order, and whitespace / naming / null handling are inherited from the supplied context. AOT-clean: no runtime reflection, only the supplied JsonTypeInfo<T>'s pre-generated metadata.

public static Func<string, string> ReformatJson<T>(JsonTypeInfo<T> jsonTypeInfo)

Parameters

jsonTypeInfo JsonTypeInfo<T>

The source-generated JsonTypeInfo<T> from the consumer's JsonSerializerContext.

Returns

Func<string, string>

A reusable sync renderer that canonicalises a JSON string.

Type Parameters

T

The expected JSON document type.

Exceptions

ArgumentNullException

jsonTypeInfo is null.