Table of Contents

Class JsonCollectionComparerAttribute

Namespace
Nerdbank.Json
Assembly
Nerdbank.Json.dll

Selects the IEqualityComparer<T> or IComparer<T> that the deserializer uses when it constructs the collection for the decorated dictionary or set member.

[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
[AssociatedTypeAttribute("comparerType", TypeShapeRequirements.Constructor)]
public sealed class JsonCollectionComparerAttribute : Attribute
Inheritance
JsonCollectionComparerAttribute
Inherited Members

Examples

Remarks

The serializer already exposes a global ComparerProvider, but that provider cannot preserve member-specific semantics such as a case-insensitive dictionary. Apply this attribute to a property, field, or constructor parameter whose type is a mutable dictionary or set to control the comparer used for the freshly constructed collection during deserialization.

The comparer type must declare a public parameterless constructor and implement IEqualityComparer<T> or IComparer<T> for the collection's key or element type. It is activated through a source-generated type shape rather than runtime reflection, so the feature remains trimming-safe and NativeAOT-compatible. The comparer only influences the instances the deserializer creates; specify the same comparer in the member's initializer to influence instances that application code creates.

Getter-only collection initializers remain the preferred zero-configuration alternative: when a member is getter-only and already initialized with the desired comparer, the deserializer populates that instance and this attribute is unnecessary.

Constructors

JsonCollectionComparerAttribute(Type)

Initializes a new instance of the JsonCollectionComparerAttribute class.

public JsonCollectionComparerAttribute(Type comparerType)

Parameters

comparerType Type

The comparer type to activate. It must declare a public parameterless constructor and implement IEqualityComparer<T> or IComparer<T> for the decorated collection's key or element type.

Properties

ComparerType

Gets the comparer type activated for the decorated collection member.

public Type ComparerType { get; }

Property Value

Type