title | description | ms.date |
---|---|---|
Breaking change: JsonNumberHandlingAttribute on non-number collection properties |
Learn about the .NET 6 breaking change where JsonNumberHandlingAttribute can now only be applied to properties that are collections of numbers. |
11/05/2021 |
A minor breaking change was introduced in .NET 6 with regard to the xref:System.Text.Json.Serialization.JsonNumberHandlingAttribute attribute. If you apply the attribute to a property that's a collection of non-number values and attempt to serialize or deserialize the property, an xref:System.InvalidOperationException is thrown. The attribute is only valid for properties that are collections of number types, for example:
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<int> MyList { get; set; }
Although it was ignored during serialization, xref:System.Text.Json.Serialization.JsonNumberHandlingAttribute could be applied to properties that were collections of non-number types. For example:
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<MyClass> MyList { get; set; }
Starting in .NET 6, if you apply xref:System.Text.Json.Serialization.JsonNumberHandlingAttribute to a property that's a collection of non-number values and attempt to serialize or deserialize the property, an xref:System.InvalidOperationException is thrown.
.NET 6
This change can affect binary compatibility.
This change was a side effect of a performance optimization for the number handling feature.
Remove the xref:System.Text.Json.Serialization.JsonNumberHandlingAttribute attribute from incompatible collection properties.
All of the xref:System.Text.Json.JsonSerializer?displayProperty=fullName serialization and deserialization methods.