Show / Hide Table of Contents

Class JsonElementComparer

Compares two System.Text.Json.JsonElement instances.

Inheritance
System.Object
JsonElementComparer
Implements
System.Collections.Generic.IComparer<System.Text.Json.JsonElement>
System.Collections.IComparer
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: JsonCons.Utilities
Assembly: JsonCons.Utilities.dll
Syntax
public sealed class JsonElementComparer : IComparer<JsonElement>, IComparer

Constructors

| Improve this Doc View Source

JsonElementComparer()

Constructs a JsonElementComparer

Declaration
public JsonElementComparer()

Properties

| Improve this Doc View Source

Instance

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 Source

Compare(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 Source

IComparer.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

Implements

System.Collections.Generic.IComparer<T>
System.Collections.IComparer
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX