Class JsonElementComparer
Compares two System.Text.Json.JsonElement instances.
Inheritance
Implements
Inherited Members
Namespace: JsonCons.Utilities
Assembly: JsonCons.Utilities.dll
Syntax
public sealed class JsonElementComparer : IComparer<JsonElement>, IComparer
Constructors
| Improve this Doc View SourceJsonElementComparer()
Constructs a JsonElementComparer
Declaration
public JsonElementComparer()
Properties
| Improve this Doc View SourceInstance
Gets a singleton instance of JsonElementComparer. This property is read-only.
Declaration
public static JsonElementComparer Instance { get; }
Property Value
Type | Description |
---|---|
JsonElementComparer |
Methods
| Improve this Doc View SourceCompare(JsonElement, JsonElement)
Compares two System.Text.Json.JsonElement instances.
If the two System.Text.Json.JsonElement instances have different data types, they are compared according to their ValueKind property, which gives this ordering:
Undefined
Object
Array
String
Number
True
False
Null
If both System.Text.Json.JsonElement instances are null, true, or false, they are equal.
If both are strings, they are compared with the String.CompareTo method.
If both are numbers, and both can be represented by a System.Decimal, they are compared with the Decimal.CompareTo method, otherwise they are compared as doubles.
If both are objects, they are compared accoring to the following rules:
- Order each object's properties by name and compare sequentially. The properties are compared first by name with the String.CompareTo method, then by value with JsonElementComparer
- The first mismatching property defines which System.Text.Json.JsonElement instance is less or greater than the other.
- If the two sequences have no mismatching properties until one of them ends, and the other is longer, the shorter sequence is less than the other.
- If the two sequences have no mismatching properties and have the same length, they are equal.
If both are arrays, they are compared element wise with JsonElementComparer. The first mismatching element defines which System.Text.Json.JsonElement instance is less or greater than the other. If the two arrays have no mismatching elements until one of them ends, and the other is longer, the shorter array is less than the other. If the two arrays have no mismatching elements and have the same length, they are equal.
Declaration
public int Compare(JsonElement lhs, JsonElement rhs)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | lhs | The first object of type cref="JsonElement"/> to compare. |
System.Text.Json.JsonElement | rhs | The second object of type cref="JsonElement"/> to compare. |
Returns
Type | Description |
---|---|
System.Int32 |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | Unable to compare numbers as either System.Decimal or double (shouldn't happen.) |
Explicit Interface Implementations
| Improve this Doc View SourceIComparer.Compare(Object, Object)
Declaration
int IComparer.Compare(object x, object y)
Parameters
Type | Name | Description |
---|---|---|
System.Object | x | |
System.Object | y |
Returns
Type | Description |
---|---|
System.Int32 |