Home JsonMath Class
JsonMath Class
Cancel

JsonMath Class

Namespace: Json.Schema

Inheritance: JsonMath 🡒 object

Provides static methods for performing mathematical operations and comparisons on JSON numeric values represented by System.Text.Json.JsonElement instances.

Remarks

The methods in this class are designed to work directly with the textual representation of JSON numbers, enabling precise comparison and divisibility checks without converting to .NET numeric types. This is useful for scenarios where exact JSON number semantics are required, such as schema validation or custom JSON

Methods

Divides(JsonElement dividend, JsonElement divisor)

Determines whether the numeric value represented by the specified JSON element is evenly divisible by the value of another JSON element.

Declaration

1
public static bool Divides(JsonElement dividend, JsonElement divisor)
ParameterTypeDescription
dividendJsonElementThe JSON element representing the dividend. Must contain a valid numeric value.
divisorJsonElementThe JSON element representing the divisor. Must contain a valid numeric value.

Returns

true if the dividend is evenly divisible by the divisor; otherwise, false.

Remarks

Both parameters must represent valid JSON numbers. If the divisor is zero or not a valid number,

IsInteger(JsonElement value)

Determines whether the numeric value represented by the specified JSON element is a mathematical integer (i.e. has no fractional part), without converting to a .NET numeric type.

Declaration

1
public static bool IsInteger(JsonElement value)
ParameterTypeDescription
valueJsonElementA System.Text.Json.JsonElement whose System.Text.Json.JsonElement.ValueKind must be

Returns

if the value has no fractional part; otherwise .

Remarks

Works directly from the JSON text representation so arbitrarily large integers are recognised correctly, avoiding overflow issues that arise when using TryGetInt64.

NumberCompare(JsonElement a, JsonElement b)

Compares two JSON number values represented by System.Text.Json.JsonElement instances and determines their relative order.

Declaration

1
public static int NumberCompare(JsonElement a, JsonElement b)
ParameterTypeDescription
aJsonElementThe first System.Text.Json.JsonElement containing a JSON number to compare.
bJsonElementThe second System.Text.Json.JsonElement containing a JSON number to compare.

Returns

A signed integer that indicates the relative order of the numbers: less than zero if is less than; zero if they are equal; greater than zero if is greater than

Remarks

This method performs a numeric comparison based on the JSON number representations, handling differences in sign, digit sequence, and exponent. Both parameters must represent valid JSON numbers; otherwise,

Contents