Skip to content

Commit f75abc2

Browse files
authored
Quoted number deserialization breaking change (#21135)
1 parent c8e10d0 commit f75abc2

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/core/compatibility/3.1-5.0.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ If you're migrating from version 3.1 of .NET Core, ASP.NET Core, or EF Core to v
99

1010
## ASP.NET Core
1111

12+
- [ASP.NET Core apps allow deserializing quoted numbers](#aspnet-core-apps-allow-deserializing-quoted-numbers)
1213
- [Authentication: AzureAD.UI and AzureADB2C.UI APIs and packages marked obsolete](#authentication-azureadui-and-azureadb2cui-apis-and-packages-marked-obsolete)
1314
- [Authorization: Resource in endpoint routing is HttpContext](#authorization-resource-in-endpoint-routing-is-httpcontext)
1415
- [Azure: Microsoft-prefixed Azure integration packages removed](#azure-microsoft-prefixed-azure-integration-packages-removed)
@@ -40,6 +41,10 @@ If you're migrating from version 3.1 of .NET Core, ASP.NET Core, or EF Core to v
4041
- [SignalR: UseSignalR and UseConnections methods removed](#signalr-usesignalr-and-useconnections-methods-removed)
4142
- [Static files: CSV content type changed to standards-compliant](#static-files-csv-content-type-changed-to-standards-compliant)
4243

44+
[!INCLUDE [ASP.NET Core apps allow deserializing quoted numbers](../../../includes/core-changes/serialization/5.0/jsonserializer-allows-reading-numbers-as-strings.md)]
45+
46+
***
47+
4348
[!INCLUDE[Authentication: AzureAD.UI and AzureADB2C.UI APIs and packages marked obsolete](~/includes/core-changes/aspnetcore/5.0/authentication-aad-packages-obsolete.md)]
4449

4550
***
@@ -418,12 +423,17 @@ If you're migrating from version 3.1 of .NET Core, ASP.NET Core, or EF Core to v
418423

419424
## Serialization
420425

426+
- [ASP.NET Core apps allow deserializing quoted numbers](#aspnet-core-apps-allow-deserializing-quoted-numbers)
421427
- [PropertyNamingPolicy, PropertyNameCaseInsensitive, and Encoder options are honored when serializing and deserializing key-value pairs](#propertynamingpolicy-propertynamecaseinsensitive-and-encoder-options-are-honored-when-serializing-and-deserializing-key-value-pairs)
422428
- [Non-public, parameterless constructors not used for deserialization](#non-public-parameterless-constructors-not-used-for-deserialization)
423429
- [JsonSerializer.Serialize throws ArgumentNullException when type parameter is null](#jsonserializerserialize-throws-argumentnullexception-when-type-parameter-is-null)
424430
- [JsonSerializer.Deserialize requires single-character string](#jsonserializerdeserialize-requires-single-character-string)
425431
- [BinaryFormatter.Deserialize rewraps some exceptions in SerializationException](#binaryformatterdeserialize-rewraps-some-exceptions-in-serializationexception)
426432

433+
[!INCLUDE [jsonserializer-allows-reading-numbers-as-strings](../../../includes/core-changes/serialization/5.0/jsonserializer-allows-reading-numbers-as-strings.md)]
434+
435+
***
436+
427437
[!INCLUDE [options-honored-when-serializing-key-value-pairs](../../../includes/core-changes/serialization/5.0/options-honored-when-serializing-key-value-pairs.md)]
428438

429439
***

docs/core/compatibility/serialization.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The following breaking changes are documented on this page:
99

1010
| Breaking change | Introduced version |
1111
| - | - |
12+
| [ASP.NET Core apps allow deserializing quoted numbers](#aspnet-core-apps-allow-deserializing-quoted-numbers) | 5.0 |
1213
| [PropertyNamingPolicy, PropertyNameCaseInsensitive, and Encoder options are honored when serializing and deserializing key-value pairs](#propertynamingpolicy-propertynamecaseinsensitive-and-encoder-options-are-honored-when-serializing-and-deserializing-key-value-pairs) | 5.0 |
1314
| [Non-public, parameterless constructors not used for deserialization](#non-public-parameterless-constructors-not-used-for-deserialization) | 5.0 |
1415
| [JsonSerializer.Serialize throws ArgumentNullException when type parameter is null](#jsonserializerserialize-throws-argumentnullexception-when-type-parameter-is-null) | 5.0 |
@@ -17,6 +18,10 @@ The following breaking changes are documented on this page:
1718

1819
## .NET 5.0
1920

21+
[!INCLUDE [jsonserializer-allows-reading-numbers-as-strings](../../../includes/core-changes/serialization/5.0/jsonserializer-allows-reading-numbers-as-strings.md)]
22+
23+
***
24+
2025
[!INCLUDE [options-honored-when-serializing-key-value-pairs](../../../includes/core-changes/serialization/5.0/options-honored-when-serializing-key-value-pairs.md)]
2126

2227
***
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
### ASP.NET Core apps allow deserializing quoted numbers
2+
3+
Starting in .NET 5.0, ASP.NET Core apps use the default deserialization options as specified by <xref:System.Text.Json.JsonSerializerDefaults.Web?displayProperty=nameWithType>. The <xref:System.Text.Json.JsonSerializerDefaults.Web> set of options includes setting <xref:System.Text.Json.JsonSerializerOptions.NumberHandling> to <xref:System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString?displayProperty=nameWithType>. This change means that ASP.NET Core apps will successfully deserialize numbers that are represented as JSON strings, instead of throwing an exception.
4+
5+
#### Change description
6+
7+
In .NET Core 3.0 - 3.1, <xref:System.Text.Json.JsonSerializer> throws a <xref:System.Text.Json.JsonException> during deserialization if it encounters a quoted number in a JSON payload. The quoted numbers are used to map with number properties in object graphs. In .NET Core 3.0 - 3.1, numbers are only read from <xref:System.Text.Json.JsonTokenType.Number?displayProperty=nameWithType> tokens.
8+
9+
Starting in .NET 5.0, quoted numbers in JSON payloads are considered valid, by default, for ASP.NET Core apps. No exception is thrown during deserialization of quoted numbers.
10+
11+
> [!TIP]
12+
>
13+
> - There is no behavior change for the default, standalone <xref:System.Text.Json.JsonSerializer> or <xref:System.Text.Json.JsonSerializerOptions>.
14+
> - This is technically not a breaking change, since it makes a scenario more permissive instead of more restrictive (that is, it succeeds in coercing a number from a JSON string instead of throwing an exception). However, since this is a significant behavioral change that affects many ASP.NET Core apps, it is documented here.
15+
> - The <xref:System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsync%2A> and <xref:System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync%2A> extension methods also use the <xref:System.Text.Json.JsonSerializerDefaults.Web> set of serialization options.
16+
17+
#### Version introduced
18+
19+
5.0
20+
21+
#### Reason for change
22+
23+
Multiple users have requested an option for more permissive number handling in <xref:System.Text.Json.JsonSerializer>. This feedback indicates that many JSON producers (for example, services across the web) emit quoted numbers. By allowing quoted numbers to be read (deserialized), .NET apps can successfully parse these payloads, by default, in web contexts. The configuration is exposed via <xref:System.Text.Json.JsonSerializerDefaults.Web?displayProperty=nameWithType> so that you can specify the same options across different application layers, for example, client, server, and shared.
24+
25+
#### Recommended action
26+
27+
If this change is disruptive, for example, if you depend on the strict number handling for validation, you can re-enable the previous behavior. Set the <xref:System.Text.Json.JsonSerializerOptions.NumberHandling?displayProperty=nameWithType> option to <xref:System.Text.Json.Serialization.JsonNumberHandling.Strict?displayProperty=nameWithType>.
28+
29+
For ASP.NET Core MVC and web API apps, you can configure the option in `Startup` by using the following code:
30+
31+
```csharp
32+
services.AddControllers()
33+
.AddJsonOptions(options.NumberHandling = JsonNumberHandling.Strict);
34+
```
35+
36+
#### Category
37+
38+
- ASP.NET Core
39+
- Serialization
40+
41+
#### Affected APIs
42+
43+
- <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=fullName>
44+
- <xref:System.Text.Json.JsonSerializer.DeserializeAsync%2A?displayProperty=fullName>
45+
46+
<!--
47+
48+
#### Affected APIs
49+
50+
- `Overload:System.Text.Json.JsonSerializer.Deserialize`
51+
- `Overload:System.Text.Json.JsonSerializer.DeserializeAsync`
52+
53+
-->

0 commit comments

Comments
 (0)