Table of Contents

Class JsonValueComparison

Namespace
JsonAssertions
Assembly
JsonAssertions.TUnit.dll

Framework-agnostic value comparison for a resolved JsonElement. Each Matches overload pairs a JSON value kind with a CLR expected value and reports whether the element both is of the matching kind and carries the expected value. Kind mismatches return false rather than throwing, so the caller can render a "found a String, expected a Number" diagnostic instead of catching an exception.

public static class JsonValueComparison
Inheritance
JsonValueComparison
Inherited Members

Remarks

Named JsonValueComparison rather than JsonValue to avoid colliding with JsonValue for consumers who use the System.Text.Json.Nodes object model alongside this package.

Methods

Matches(JsonElement, bool)

Reports whether element is a JSON boolean equal to expected.

public static bool Matches(JsonElement element, bool expected)

Parameters

element JsonElement
expected bool

Returns

bool

Matches(JsonElement, double)

Reports whether element is a JSON number equal to expected. The element is read as a double; values beyond double precision are out of scope for this overload (use the long / ulong overloads for exact 64-bit integers, which also accept the string-encoded form).

public static bool Matches(JsonElement element, double expected)

Parameters

element JsonElement
expected double

Returns

bool

Matches(JsonElement, int)

Reports whether element is a JSON integer equal to expected, encoded either as a JSON number (read as an exact 32-bit integer via TryGetInt32(out int)) or as a numeric JSON string that parses to a int against InvariantCulture. A number that is fractional or out of int range, a non-numeric string, or any other kind returns false. Both encodings are accepted because the JSON form of an integer depends on the serializer: System.Text.Json writes a 32-bit integer as a JSON number, while Protobuf's JsonFormatter can emit it as a JSON string.

public static bool Matches(JsonElement element, int expected)

Parameters

element JsonElement
expected int

Returns

bool

Matches(JsonElement, long)

Reports whether element is a JSON 64-bit integer equal to expected, encoded either as a JSON number (read as an exact 64-bit integer via TryGetInt64(out long)) or as a numeric JSON string that parses to a long against InvariantCulture. A number that is fractional or out of long range, a non-numeric string, or any other kind returns false. The 64-bit overload exists because Protobuf's JsonFormatter serializes int64 as a JSON string to avoid 53-bit precision loss, while System.Text.Json writes it as a JSON number; both encodings are matched.

public static bool Matches(JsonElement element, long expected)

Parameters

element JsonElement
expected long

Returns

bool

Matches(JsonElement, string)

Reports whether element is a JSON string equal to expected (ordinal comparison).

public static bool Matches(JsonElement element, string expected)

Parameters

element JsonElement
expected string

Returns

bool

Matches(JsonElement, uint)

Reports whether element is a JSON unsigned integer equal to expected, encoded either as a JSON number (read as an exact unsigned 32-bit integer via TryGetUInt32(out uint)) or as a numeric JSON string that parses to a uint against InvariantCulture. A number that is fractional, negative, or out of uint range, a non-numeric string, or any other kind returns false.

public static bool Matches(JsonElement element, uint expected)

Parameters

element JsonElement
expected uint

Returns

bool

Matches(JsonElement, ulong)

Reports whether element is a JSON unsigned 64-bit integer equal to expected, encoded either as a JSON number (read as an exact unsigned 64-bit integer via TryGetUInt64(out ulong)) or as a numeric JSON string that parses to a ulong against InvariantCulture. A number that is fractional, negative, or out of ulong range, a non-numeric string, or any other kind returns false. Mirrors the long overload for unsigned values (Protobuf's JsonFormatter serializes uint64 as a JSON string).

public static bool Matches(JsonElement element, ulong expected)

Parameters

element JsonElement
expected ulong

Returns

bool

MatchesAny(JsonElement, params double[])

Reports whether element is a JSON number equal to any of candidates. Element values beyond double precision are out of scope for this overload.

public static bool MatchesAny(JsonElement element, params double[] candidates)

Parameters

element JsonElement
candidates double[]

Returns

bool

MatchesAny(JsonElement, params int[])

Reports whether element is a JSON 32-bit integer equal to any of candidates, encoded either as a JSON number (read via TryGetInt32(out int)) or as a numeric JSON string that parses to a int against InvariantCulture. A fractional or out-of-range number, a non-numeric string, or any other kind returns false.

public static bool MatchesAny(JsonElement element, params int[] candidates)

Parameters

element JsonElement
candidates int[]

Returns

bool

MatchesAny(JsonElement, params long[])

Reports whether element is a JSON 64-bit integer equal to any of candidates, encoded either as a JSON number (read via TryGetInt64(out long)) or as a numeric JSON string that parses to a long against InvariantCulture. A fractional or out-of-range number, a non-numeric string, or any other kind returns false.

public static bool MatchesAny(JsonElement element, params long[] candidates)

Parameters

element JsonElement
candidates long[]

Returns

bool

MatchesAny(JsonElement, params string[])

Reports whether element is a JSON string equal (ordinal) to any of candidates. false when the element is not a JSON string, or when none of the candidates matches.

public static bool MatchesAny(JsonElement element, params string[] candidates)

Parameters

element JsonElement
candidates string[]

Returns

bool

MatchesAny(JsonElement, params uint[])

Reports whether element is a JSON unsigned 32-bit integer equal to any of candidates, encoded either as a JSON number (read via TryGetUInt32(out uint)) or as a numeric JSON string that parses to a uint against InvariantCulture. A fractional, negative, or out-of-range number, a non-numeric string, or any other kind returns false.

public static bool MatchesAny(JsonElement element, params uint[] candidates)

Parameters

element JsonElement
candidates uint[]

Returns

bool

MatchesAny(JsonElement, params ulong[])

Reports whether element is a JSON unsigned 64-bit integer equal to any of candidates, encoded either as a JSON number (read via TryGetUInt64(out ulong)) or as a numeric JSON string that parses to a ulong against InvariantCulture. A fractional, negative, or out-of-range number, a non-numeric string, or any other kind returns false.

public static bool MatchesAny(JsonElement element, params ulong[] candidates)

Parameters

element JsonElement
candidates ulong[]

Returns

bool