Class JsonRoundtripAssertions
- Namespace
- JsonAssertions.TUnit
- Assembly
- JsonAssertions.TUnit.dll
Fluent assertion that a typed value round-trips cleanly through STJ source-gen via the
consumer's JsonTypeInfo<T>. Catches the common "we added a property and
forgot to update the JsonSerializerContext" regression class: a missing
[JsonSerializable(typeof(T))] entry or a property the context doesn't know about
causes the round-trip to produce a different JSON string than the original.
[SuppressMessage("Usage", "VSTHRD200:Use \"Async\" suffix for async methods", Justification = "This is a [GenerateAssertion] source method: the method name becomes the fluent chain entry point (Assert.That(value).RoundtripsCleanlyVia(...)), so an Async suffix would corrupt the assertion surface. The method is synchronous anyway (no IO, no await).")]
public static class JsonRoundtripAssertions
- Inheritance
-
JsonRoundtripAssertions
- Inherited Members
Remarks
Mechanism: Serialize(value, jsonTypeInfo) -> Deserialize(json1, jsonTypeInfo)
-> Serialize(roundtripped, jsonTypeInfo). The two serialized strings are compared
ordinally; if they differ, the round-trip is lossy and the assertion fails with both
JSON strings rendered for diagnosis.
AOT-clean: the supplied JsonTypeInfo<T> is the source-generated entry
for T in the consumer's JsonSerializerContext; the assertion
uses no runtime reflection.
Use case: consumers shipping
AOT-compatible web APIs typically have a JsonSerializerContext but rarely write
regression tests verifying that EVERY type in the context round-trips correctly. A
RoundtripsCleanlyVia regression test for each type in each context is a 1-2 hour
sweep that catches future "added a property, forgot the context" bugs at CI time.
Methods
RoundtripsCleanlyVia<T>(T, JsonTypeInfo<T>)
Asserts the value round-trips cleanly through the supplied
JsonTypeInfo<T>: serializing it produces a JSON string that, when
deserialized and re-serialized via the same context, yields a byte-identical JSON
string. Catches missing [JsonSerializable] entries, property orderings that
drift on round-trip, and serializer-options-vs-context inconsistencies.
[GenerateAssertion]
public static AssertionResult RoundtripsCleanlyVia<T>(this T value, JsonTypeInfo<T> jsonTypeInfo)
Parameters
valueTThe value to round-trip.
jsonTypeInfoJsonTypeInfo<T>The JsonTypeInfo<T> from the consumer's
JsonSerializerContext; AOT-clean.
Returns
- AssertionResult
Type Parameters
TThe value type whose round-trip is asserted.