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 property in typed object not serialized to camelCase JSON #9543

Closed
Mortana89 opened this issue Apr 19, 2019 · 7 comments
Closed

Dynamic property in typed object not serialized to camelCase JSON #9543

Mortana89 opened this issue Apr 19, 2019 · 7 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates question
Milestone

Comments

@Mortana89
Copy link

Mortana89 commented Apr 19, 2019

Describe the bug

When calling an API that returns a typed object that holds a dynamic property, the properties of the typed object are (correctly) returned as camelCase, while the dynamic property properties are in (Upper)PascalCase.
Setting the default JsonSerializer ContractResolver to CamelCase does not seem to work.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core 2.2
  2. Create an API Controller that returns a typed object, with a dynamic property
  3. See error

Expected behavior

Properties of dynamic objects should follow the configured JSON Contract Resolver settings.

@davidfowl
Copy link
Member

You likely need to set ProcessDictionaryKeys to true on the CamelCaseNamingStrategy. Something like

new DefaultContractResolver()
            {
                NamingStrategy = new CamelCaseNamingStrategy()
                {
                    ProcessDictionaryKeys = true
                }
            };

cc @JamesNK

@Mortana89
Copy link
Author

You likely need to set ProcessDictionaryKeys to true on the CamelCaseNamingStrategy. Something like

new DefaultContractResolver()
            {
                NamingStrategy = new CamelCaseNamingStrategy()
                {
                    ProcessDictionaryKeys = true
                }
            };

cc @JamesNK

I tried this as well, but it didn't bring any solution, the situation is the same, with one minor difference that, when setting the contractresolver to a defaultcontractresolver with above settings, any property that has a JsonProperty annotation, is serialized according to that jsonproperty annotation, what we don't want either.

Thanks for the suggestion, any other ideas?

@JamesNK
Copy link
Member

JamesNK commented Apr 19, 2019

Is your dynamic property a JObject? A JObject's JSON is not changed by a naming strategy, it is serialized as is.

@Mortana89
Copy link
Author

No, basically our dynamic object is an event class.
We have events being thrown on a service bus, and one of the consumers picks up the events, and puts these in a 'payload' property of another object type. But the event is typed, and at runtime converted to dynamic.

So the flow is like this:

  • An event object (A) is created
  • The event is serialized to JSON
  • It's put on the service bus
  • A consumer picks up the event from the service bus
  • The event is casted to the original event object type (A)
  • A new object (B) is created, and passed the instance of this event object(A) (the method signature expects a dynamic, so runtime casting)
  • This object (B) is stored in our database

API Call:

  • The JSON is retrieved from the database
  • JSON is serialized to B
  • B is returned

At the moment the API call is being done, and the data is being fetched, there's no track anymore of the original event object's type (A), so perhaps that's the issue here, and this is causing it to be handled as a JObject? I don't see a way around this though :/

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 19, 2019
@JamesNK
Copy link
Member

JamesNK commented Apr 19, 2019

No, basically our dynamic object is an event class.

and this is causing it to be handled as a JObject?

I'm confused, is it a JObject or not? A JObject will not be effected by a naming standard. It is written as is because as far as the serializer is concerned it is already JSON.

@danroth27 danroth27 added this to the Discussions milestone Apr 22, 2019
@darrynhoskingluna
Copy link

darrynhoskingluna commented Nov 12, 2019

I have the same problem.

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

public class Program
{
	public class TestViewModel
	{
		public string P1 { get;set; }
		public dynamic SomeDymamic { get;set; }
	}

	public static void Main()
	{
		var test = new TestViewModel();
		test.P1 = "Test";
		var settings = new JsonSerializerSettings();
		settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
		test.SomeDymamic = JsonConvert.DeserializeObject<dynamic>("{\"Property\":\"test\"}", settings);
		Console.WriteLine(JsonConvert.SerializeObject(test, settings));
	}
}

Output: {"p1":"Test","someDymamic":{"Property":"test"}}
Expected Output: {"p1":"Test","someDymamic":{"property":"test"}}

when returned from the controller i would expect the above to follow the settings provided in the DefaultContractResolver (in my case camel case), except it defaults to pascal case.

Currently you would get a mix of casing returned. The view model will follow Camel Case, but the dynamic property would not.

** Update
Here is a link to an article with some solutions.
https://andrewlock.net/serializing-a-pascalcase-newtonsoft-json-jobject-to-camelcase/
These mostly seem like hackey work arounds tho :(

@ghost
Copy link

ghost commented Dec 7, 2019

Thank you for contacting us. Due to no activity on this 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.

@ghost ghost closed this as completed Dec 7, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2019
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 question
Projects
None yet
Development

No branches or pull requests

6 participants