Skip to content

Add property to MvcNewtonsoftJsonOptions to support culture-aware deserialization #31334

Closed
@martincostello

Description

@martincostello

Background and Motivation

Adds a new option to the Newtonsoft.Json-based NuGet package for MVC to enable culture-aware deserialization of request bodies as proposed by #9994 (comment).

I missed the need to propose the API before doing the PR as I was looking at help wanted issues, so the below is already implemented in #31331.

Proposed API

namespace Microsoft.AspNetCore.Mvc
{
    public static class MvcNewtonsoftJsonOptions
    {
+       public bool ReadJsonWithRequestCulture { get; set; }
    }
}

Usage Examples

The application would opt-in to the behavior like so:

services.AddControllersWithViews()
        .AddNewtonsoftJson(options => options.ReadJsonWithRequestCulture = true);

Which would then allow requests such as the following to succeed:

curl http://localhost:20991/api/values/date3 -X POST -d "{\"Date\":\"15\/5\/2019\"}" -H "culture: es-MX" -H "Content-Type: application/json"

Alternative Designs

None considered.

Risks

None known - new behaviour this flag would enable would be guarded behind it, with its default value being false to retain current behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-model-binding

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions