Table of Contents

Class JsonCanonicalizer

Namespace
JsonAssertions
Assembly
JsonAssertions.TUnit.dll

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 JsonSerializerContext and keeps unknown properties, which is what makes it suitable for pinning the whole shape of a response as a snapshot baseline.

public static class JsonCanonicalizer
Inheritance
JsonCanonicalizer
Inherited Members

Remarks

Pairs with a snapshot library's normalizer hook (for example SnapshotAssertions' SnapshotOptions.WithNormalizer): the consumer composes the two at its own call site, so neither package depends on the other.

await Assert.That(body).MatchesSnapshot(SnapshotOptions.Default
    .WithNormalizer(s => JsonCanonicalizer.Canonicalize(s, o => o
        .ScrubPath("grpc.serverUri")
        .ScrubPath("[*].session.connectionId"))));

Methods

Canonicalize(string)

Canonicalizes json with no scrubbing: sorted keys, stable indentation, all values preserved.

public static string Canonicalize(string json)

Parameters

json string

The JSON document text.

Returns

string

The canonical form.

Exceptions

ArgumentNullException

json is null.

JsonException

json is not valid JSON.

Canonicalize(string, Action<JsonCanonicalizeOptions>?)

Canonicalizes json, applying the scrubbing configured by configure: sorted keys, stable indentation, all values preserved, and each registered scrub path's value replaced with the scrub token.

public static string Canonicalize(string json, Action<JsonCanonicalizeOptions>? configure)

Parameters

json string

The JSON document text.

configure Action<JsonCanonicalizeOptions>

A callback that registers scrub paths and/or the scrub token. May be null for no scrubbing.

Returns

string

The canonical, scrubbed form.

Exceptions

ArgumentNullException

json is null.

JsonException

json is not valid JSON.