ASP.NET Core apps allow deserializing quoted numbers

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.


Version introduced


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.

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:

   .AddJsonOptions(options => options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.Strict);

Affected APIs