Aspnetcore: Serializing to Json using System.Text.Json does not preserve the case of the property names.

Created on 10 May 2019  路  2Comments  路  Source: dotnet/aspnetcore

Creating a new Web API ASP.NET Core 3 project and removing the call to .AddNewtonsoftJson(), then changing the Get() method on the controller to return an object with properties causes it to return JSON, but with all of the property names converted to camel case.

The only option for SerializerOptions.PropertyNamingPolicy is JsonNamingPolicy.CamelCase; there is no option to not modify the case at all.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core '3.0.0-preview5-19227-01'
  2. Create a new ASP.NET Core Web Application
  3. Choose API
  4. In Startup.cs, remove the call to .AddNewtonsoftJson().
  5. In ValuesController.cs, add a class:
    ```C#
    public class TestModel
    {
    public string MultiWordValue { get; }
    public TestModel(string value)
    {
        MultiWordValue = value;
    }
}

```

  1. Change the Get() method body to return new TestModel("value");
  2. Run the application and GET api/values returns {"multiWordValue":"value"}

Expected behavior

I expect it to not change the casing on the property names unless I explicitly ask for that behaviour; barring that, I expect to have a way to tell it to preserve casing: {"MultiWordValue":"value"}

Additional context

.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview5-011568
Commit: b487ff10aa

Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnetsdk\3.0.100-preview5-011568\

Host (useful for support):
Version: 3.0.0-preview5-27626-15
Commit: 61f30f5a23

.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnetsdk]
2.1.504 [C:\Program Files\dotnetsdk]
2.1.505 [C:\Program Files\dotnetsdk]
2.1.600 [C:\Program Files\dotnetsdk]
2.1.601 [C:\Program Files\dotnetsdk]
2.1.602 [C:\Program Files\dotnetsdk]
2.1.700-preview-009597 [C:\Program Files\dotnetsdk]
2.1.700-preview-009601 [C:\Program Files\dotnetsdk]
2.1.700-preview-009618 [C:\Program Files\dotnetsdk]
2.2.104 [C:\Program Files\dotnetsdk]
3.0.100-preview5-011568 [C:\Program Files\dotnetsdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview5-19227-01 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnetshared\Microsoft.WindowsDesktop.App]

area-mvc question

Most helpful comment

MVC expects camel-case JSON input and will camel-case all JSON output by default. To use property's original casing, you can set the PropertyNamingPolicy to null.

/cc @steveharter \ @ahsonkhan

All 2 comments

Using [JsonPropertyName("MultiWordValue")] on the property does have it use the specified casing, but having to put that Attribute, redundantly, on all properties can be a lot of work that is prone to copy and paste errors/typos.

MVC expects camel-case JSON input and will camel-case all JSON output by default. To use property's original casing, you can set the PropertyNamingPolicy to null.

/cc @steveharter \ @ahsonkhan

Was this page helpful?
0 / 5 - 0 ratings