Table of Contents

Class JsonEquivalence

Namespace
JsonAssertions
Assembly
JsonAssertions.TUnit.dll

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, and 1e0 are 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.

public static class JsonEquivalence
Inheritance
JsonEquivalence
Inherited Members

Methods

Compare(string, string, JsonEquivalenceOptions)

Compares two JSON document strings for structural equivalence.

public static JsonDifference? Compare(string expected, string actual, JsonEquivalenceOptions options)

Parameters

expected string

The expected JSON document text.

actual string

The actual JSON document text.

options JsonEquivalenceOptions

The comparison options.

Returns

JsonDifference

The first difference found, or null when equivalent.

Exceptions

ArgumentNullException

Any argument is null.

JsonException

Either argument is not valid JSON.

Compare(JsonElement, JsonElement, JsonEquivalenceOptions)

Compares two parsed JSON elements for structural equivalence. The elements must stay valid for the call (their backing documents alive).

public static JsonDifference? Compare(JsonElement expected, JsonElement actual, JsonEquivalenceOptions options)

Parameters

expected JsonElement

The expected JSON element.

actual JsonElement

The actual JSON element.

options JsonEquivalenceOptions

The comparison options.

Returns

JsonDifference

The first difference found, or null when equivalent.

Exceptions

ArgumentNullException

options is null.

ContainsAll(string, string, JsonEquivalenceOptions)

Determines whether actual contains expected as a subset: every property in the expected document must be present in the actual document with an equivalent value (recursively), while the actual document may carry additional properties. Every difference is collected, not only the first.

public static IReadOnlyList<JsonDifference> ContainsAll(string expected, string actual, JsonEquivalenceOptions options)

Parameters

expected string

The expected subset document text.

actual string

The actual JSON document text.

options JsonEquivalenceOptions

The comparison options.

Returns

IReadOnlyList<JsonDifference>

Every difference found; an empty list when actual contains the subset.

Exceptions

ArgumentNullException

Any argument is null.

JsonException

Either argument is not valid JSON.

ContainsAll(JsonElement, JsonElement, JsonEquivalenceOptions)

Determines whether actual contains expected as a subset (see ContainsAll(string, string, JsonEquivalenceOptions)), over parsed elements. The elements must stay valid for the call (their backing documents alive).

public static IReadOnlyList<JsonDifference> ContainsAll(JsonElement expected, JsonElement actual, JsonEquivalenceOptions options)

Parameters

expected JsonElement

The expected subset element.

actual JsonElement

The actual JSON element.

options JsonEquivalenceOptions

The comparison options.

Returns

IReadOnlyList<JsonDifference>

Every difference found; an empty list when actual contains the subset.

Exceptions

ArgumentNullException

options is null.