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