Table of Contents

Class JsonContextAssertions

Namespace
JsonAssertions.TUnit
Assembly
JsonAssertions.TUnit.dll

Fluent TUnit entry points for asserting the registration state of a JsonSerializerContext. AOT-clean (the context's own type registry is the authoritative source; no reflection over the asserted type needed) and STJ-shaped.

public static class JsonContextAssertions
Inheritance
JsonContextAssertions
Inherited Members

Remarks

The headline assertion is HasJsonTypeInfoFor<T>(IJsonContextAssertionSource): it catches the "added a property / new domain type but forgot to add [JsonSerializable(typeof(NewType))] to the context" regression class. Each production JsonSerializerContext in a consumer codebase wants a one-line regression test per registered type; the assertion makes that test trivial.

This is the educational-demand AOT-moat companion to RoundtripsCleanlyVia: where RoundtripsCleanlyVia verifies a value round-trips cleanly through a typed context, HasJsonTypeInfoFor verifies the context KNOWS about the type at all. A consumer that adopts both gets a complete "my serializer context is in sync with my domain types" CI gate.

Consumers chain the bridge extension AsJsonContext<TContext>(IAssertionSource<TContext>) before the leaf assertion to keep the call site to a single explicit type argument:

await Assert.That(MyContext.Default).AsJsonContext().HasJsonTypeInfoFor<MyDto>();

See IJsonContextAssertionSource for the technical rationale behind the bridge step.

Methods

AsJsonContext<TContext>(IAssertionSource<TContext>)

Bridges an invariant TUnit assertion source typed at a concrete JsonSerializerContext subtype into the JSON-context assertion family by returning an IJsonContextAssertionSource whose Context is typed at JsonSerializerContext. The single method-level generic TContext is inferred from the assertion source, so the call site reads Assert.That(MyContext.Default).AsJsonContext() without explicit type arguments.

public static IJsonContextAssertionSource AsJsonContext<TContext>(this IAssertionSource<TContext> source) where TContext : JsonSerializerContext

Parameters

source IAssertionSource<TContext>

The TUnit assertion source produced by Assert.That(myContext).

Returns

IJsonContextAssertionSource

A JSON-context assertion source ready for the family's fluent assertions.

Type Parameters

TContext

The concrete JsonSerializerContext subtype, inferred from source.

Exceptions

ArgumentNullException

source is null.

HasJsonTypeInfoFor<T>(IJsonContextAssertionSource)

Asserts the underlying JsonSerializerContext has a JsonTypeInfo<T> registered for T. Catches the common "added a property / new domain type but forgot to add [JsonSerializable(typeof(NewType))] to the context" regression class before any runtime serialization touches the unregistered type.

[SuppressMessage("Usage", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "Fluent assertion entry point; an Async suffix would corrupt the chain surface. The returned assertion is awaitable.")]
public static HasJsonTypeInfoForAssertion<T> HasJsonTypeInfoFor<T>(this IJsonContextAssertionSource source)

Parameters

source IJsonContextAssertionSource

A JSON-context assertion source obtained via AsJsonContext<TContext>(IAssertionSource<TContext>).

Returns

HasJsonTypeInfoForAssertion<T>

An awaitable HasJsonTypeInfoForAssertion<T> that resolves to passed when the context knows about T; otherwise a failed assertion identifying the missing type and context.

Type Parameters

T

The type whose registration is asserted.

Exceptions

ArgumentNullException

source is null.