-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Use request culture to deserialize JSON body #31331
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
Use request culture to deserialize JSON body #31331
Conversation
Add new ReadJsonWithRequestCulture option to support deserializing JSON using the current HTTP request's culture when using the Newtonsoft.Json input formatter with MVC. Addresses dotnet#9994.
Needs an API proposal. |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
/// <value> | ||
/// The default value is <see langword="false"/>. | ||
/// </value> | ||
public bool ReadJsonWithRequestCulture { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this might be a better name if this worked for both input and output formatting?
public bool ReadJsonWithRequestCulture { get; set; } | |
public bool UseCurrentCulture { get; set; } |
@martincostello can this also use the culture for output formatting? Seems weird it only works for reading, but not for writing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that makes sense. Initially I just went down the route outlined in the initial issue, but I agree it would be better to have it be bi-directional.
I'll go with that suggested name for now and look at having the output formatter use it too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So from playing around with this locally, the serializer always writes in ISO format for DateTime
values (or whatever is configured by the JsonSerializerSettings
, e.g. the DateFormatString
property).
Setting the writer/serializer to use the current culture has no effect on this and I'm not sure there's a sensible default to overwrite it for or that it's a good idea to special-case for DateTime
/DateTimeOffset
.
I'd also have to introduce a new constructor (or make a breaking change) to allow the MvcNewtonsoftJsonOptions
to be passed into the output formatter to observe the value of the option to know if to set the culture or not.
Thoughts on best route forward?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @JamesNK in case he has thoughts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the main place culture is used with Newtonsoft.Json is when deserializing strings into .NET types.
For example:
{"Amount": "1.0"}
public class Product
{
public decimal Amount { get; set; }
}
By default the culture is InvariantCulture
. Changing the culture can be used in apps that want to allow strings like "1,0"
(seems to be a European thing) to successfully convert to a decimal/double/float.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semi-related to this is how we want to handle culture for simple minimal action parameters (#32377). First we need to default to InvariantCulture
, but I think we might want a way to configure this.
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
Add missing summary tag from merge with main.
Thanks for the PR! |
PR Title
Add support for reading localized parsing of dates etc. in JSON model binding.
PR Description
Adds a new
ReadJsonWithRequestCulture
option to support deserializing JSON using the current HTTP request's culture when using the Newtonsoft.Json input formatter with MVC as suggested by #9994 (comment).Addresses #9994