Selecting a NamingStrategy when using a JsonConverter on a class property
Okay, this appears to work:
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter),
converterParameters:typeof(CamelCaseNamingStrategy))]
public ChartType ChartType { get; }
As NamingStrategy
is a property of the StringEnumConverter
it's applied using the converterParameters
parameter. This got my desired output. I think an example of this would be useful in Newtonsoft documentation.
Another possible solution is using JsonSerializerSettings
var settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> {
new StringEnumConverter(new CamelCaseNamingStrategy())
}
};
var result = JsonConvert.SerializeObject(obj, settings);
This works for me for enabling camel casing on a single place in a .Net Core web api:
[JsonConverter(typeof(StringEnumConverter), true)]
Note that you can append constructor parameters to the type given by the first parameter and StringEnumConverter
has the following overloaded constructor:
StringEnumConverter(bool camelCaseText)
Of course, enabling this globally is normally preferred, as discussed here for example.