Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Starting in .NET 5, ASP.NET Core apps use the default deserialization options as specified by JsonSerializerDefaults.Web. The Web set of options includes setting NumberHandling to JsonNumberHandling.AllowReadingFromString. 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, JsonSerializer throws a 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 JsonTokenType.Number tokens.
Starting in .NET 5, 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 JsonSerializer or 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 HttpClientJsonExtensions.GetFromJsonAsync and HttpContentJsonExtensions.ReadFromJsonAsync extension methods also use the Web set of serialization options.
Version introduced
5.0
Reason for change
Multiple users have requested an option for more permissive number handling in 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 JsonSerializerDefaults.Web 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 JsonSerializerOptions.NumberHandling option to JsonNumberHandling.Strict.
For ASP.NET Core MVC and web API apps, you can configure the option in Startup
by using the following code:
services.AddControllers()
.AddJsonOptions(options => options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.Strict);