Skip to content
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

Dynamic properties not serialized camelCase when using Microsoft Json.NET package #30454

Closed
brgrz opened this issue Feb 25, 2021 · 4 comments
Closed
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-formatting Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Milestone

Comments

@brgrz
Copy link

brgrz commented Feb 25, 2021

Describe the bug

This is essentially a duplicate of #9543

It was locked there but no resolution was provided and solutions/workarounds that used to work with ASP.NET MVC 5 do not work anymore in ASP.NET Core.

The default System.Text.Json serializer does not support dynamics at all. We had to revert to using Json.NET via Microsoft's Nuget package Microsoft.AspNetCore.Mvc.NewtonsoftJson (v. 3.1.12) to get support for dynamic serialization.

Then enabled it like this

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

Comments for UseCamelCasing() specifically say

Configures the casing behavior of JSON serialization to use camel case for property names, and optionally for dynamic types and dictionary keys.

and

If true will camel case dictionary keys and properties of dynamic objects.

This does not work though. Properties for dynamic objects within a typed object remain PascalCase when serialized.
I think this warrants a revisit to the issue I linked above and a possible resolution.

To Reproduce

Here is an example what comes out of a serializer when provided with a dynamic object within a typed object:

"records": [
        {
            "shape": null,
            "layout": "",
            "content": {
                "Location": null,
                "Address": null,
                "OpenHours": null,
                "Information": null,

Further technical details

  • ASP.NET Core version: 3.1
@brgrz brgrz changed the title Dynamic properties not serialized camelCase when using Microsoft Json.net package Dynamic properties not serialized camelCase when using Microsoft Json.NET package Feb 25, 2021
@javiercn
Copy link
Member

@brgrz thanks for contacting us.

@JamesNK is there something specific you need to setup to enable this in Newtonsoft.Json?

@brgrz WRT to dynamic support in System.Text.Json I would suggest you file an issue on the runtime repo so that they can track the ask.

@javiercn javiercn added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 25, 2021
@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Feb 25, 2021
@pranavkm
Copy link
Contributor

@brgrz are you attempting to serialize a JObject? If so, according to #9543 (comment), Newtonsoft.Json will not transform the casing, so the behavior here is expected.

@pranavkm pranavkm added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Feb 27, 2021
@brgrz
Copy link
Author

brgrz commented Feb 27, 2021

It is a dynamic backed by a JObject, yes.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Feb 27, 2021
@ghost
Copy link

ghost commented Jun 17, 2021

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

@ghost ghost closed this as completed Jun 17, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jul 17, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-formatting Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

No branches or pull requests

4 participants