-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Quoted number deserialization breaking change #21135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...e-changes/serialization/5.0/jsonserializer-allows-reading-numbers-as-strings.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| ### ASP.NET Core apps allow deserializing quoted numbers | ||
|
|
||
| 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. | ||
|
|
||
| #### Change description | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| > [!TIP] | ||
| > | ||
| > - There is no behavior change for the default, standalone <xref:System.Text.Json.JsonSerializer> or <xref:System.Text.Json.JsonSerializerOptions>. | ||
| > - 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. | ||
| > - 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. | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #### Version introduced | ||
|
|
||
| 5.0 | ||
|
|
||
| #### Reason for change | ||
|
|
||
| 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. | ||
|
|
||
| #### Recommended action | ||
|
|
||
| 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>. | ||
|
|
||
| For ASP.NET Core MVC and web API apps, you can configure the option in `Startup` by using the following code: | ||
|
|
||
| ```csharp | ||
| services.AddControllers() | ||
| .AddJsonOptions(options.NumberHandling = JsonNumberHandling.Strict); | ||
| ``` | ||
|
|
||
| #### Category | ||
|
|
||
| - ASP.NET Core | ||
layomia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Serialization | ||
|
|
||
| #### Affected APIs | ||
|
|
||
| - <xref:System.Text.Json.JsonSerializer.Deserialize%2A?displayProperty=fullName> | ||
| - <xref:System.Text.Json.JsonSerializer.DeserializeAsync%2A?displayProperty=fullName> | ||
|
|
||
| <!-- | ||
|
|
||
| #### Affected APIs | ||
|
|
||
| - `Overload:System.Text.Json.JsonSerializer.Deserialize` | ||
| - `Overload:System.Text.Json.JsonSerializer.DeserializeAsync` | ||
|
|
||
| --> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.