Closed mcquiggd closed 4 years ago
Same question; have not been able to get the output to be camelCased.
Were you able to resolve this?
DefaultPropertyNameHandling Is deprecated. Use serializersettings property with a camel case contract resolver, see newtonsoft docs
This is a while ago now, but I was actually using SerializerSettings:
settings.SerializerSettings = new JsonSerializerSettings();
settings.SerializerSettings.ContractResolver = new
CamelCasePropertyNamesContractResolver();
var generator = new AzureFunctionsV2ToSwaggerGenerator(settings);
I believe this was not working, because I was using a common Startup with a FunctionHost. For example, when using MVC, you can set that behaviour as a global setting in one of extension methods (such as AddControllers), and that is then propagated.
The sample code from NewtonSoft does work in a quick Function App I just tested:
// settings will automatically be used by JsonConvert.SerializeObject/DeserializeObject
JsonConvert.DefaultSettings = () =>
new JsonSerializerSettings
{ Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver() };
var s = new Staff
{
FirstName = "Eric",
LastName = "Example",
BirthDate = new DateTime(1980, 4, 20, 0, 0, 0, DateTimeKind.Utc),
Department = "IT",
JobTitle = "Web Dude"
};
var json = JsonConvert.SerializeObject(s);
Console.WriteLine(json);
I'll need to find my original code to verify if this works in a Startup class (again, without using MVC), but for now I will close this... thanks.
I am using Azure Functions v2; I am not using MVC. In my startup, I have the following code that correctly sets the behavior of NewtonSoft Json Serialization, to be ordered, and camel cased (the OrderedContractResolver inherits from CamelCasePropertyNamesContractResolver).
I have a Function, to generate the Swagger Json:
I have tried explicitly setting the "obsolete" property of
settings.GeneratorSettings.DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase;
And also:
However, the generated json is not Camel Cased:
Any ideas... ?