Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/core/compatibility/6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
| [IAsyncEnumerable serialization](serialization/6.0/iasyncenumerable-serialization.md) | ✔️ | ❌ | Preview 4 |
| [JSON source-generation API refactoring](serialization/6.0/json-source-gen-api-refactor.md) | ❌ | ✔️ | RC 2 |
| [JsonNode no longer supports C# `dynamic` type](serialization/6.0/jsonnode-dynamic-type.md) | ❌ | ✔️ | Preview 7 |
| [JsonNumberHandlingAttribute on collection properties](serialization/6.0/jsonnumberhandlingattribute-behavior.md) | ❌ | ✔️ | RC 1 |
| [New JsonSerializer source generator overloads](serialization/6.0/jsonserializer-source-generator-overloads.md) | ❌ | ✔️ | Preview 6 |

## Windows Forms
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "Breaking change: JsonNumberHandlingAttribute on non-number collection properties"
description: Learn about the .NET 6 breaking change where JsonNumberHandlingAttribute can now only be applied to properties that are collections of numbers.
ms.date: 11/05/2021
---
# JsonNumberHandlingAttribute on collection properties

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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
A 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:

I'm not sure it's a good idea to classify this as a "minor" breaking change. Any one who had code in place before that used this attribute in this way, would now experience an exception. It's still a breaking change, even though it may not affect as many consumers.


```csharp
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<int> MyList { get; set; }
```

## Previous behavior

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:

```csharp
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public List<MyClass> MyList { get; set; }
```

## New behavior

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.

## Version introduced

6.0 Preview 1

## Type of breaking change

This change can affect [binary compatibility](../../categories.md#binary-compatibility).

## Reason for change

This change was a side effect of a performance optimization for the number handling feature.

## Recommended action

Remove the <xref:System.Text.Json.Serialization.JsonNumberHandlingAttribute> attribute from incompatible collection properties.

## Affected APIs

All of the <xref:System.Text.Json.JsonSerializer?displayProperty=fullName> serialization and deserialization methods.
4 changes: 4 additions & 0 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ items:
href: serialization/6.0/json-source-gen-api-refactor.md
- name: JsonNode no longer supports C# `dynamic`
href: serialization/6.0/jsonnode-dynamic-type.md
- name: JsonNumberHandlingAttribute on collection properties
href: serialization/6.0/jsonnumberhandlingattribute-behavior.md
- name: New JsonSerializer source generator overloads
href: serialization/6.0/jsonserializer-source-generator-overloads.md
- name: Windows Forms
Expand Down Expand Up @@ -883,6 +885,8 @@ items:
href: serialization/6.0/json-source-gen-api-refactor.md
- name: JsonNode no longer supports C# `dynamic`
href: serialization/6.0/jsonnode-dynamic-type.md
- name: JsonNumberHandlingAttribute on collection properties
href: serialization/6.0/jsonnumberhandlingattribute-behavior.md
- name: New JsonSerializer source generator overloads
href: serialization/6.0/jsonserializer-source-generator-overloads.md
- name: .NET 5
Expand Down