Convert CamelCaseText to CamelCaseNamingStrategy in Newtonsoft.Json

Viewed 2142

I recently upgraded to the latest Newtonsoft.Json version 12.0.2. I was previously running an older version (11.0.2), and somewhere in between, the StringEnumConverter.CamelCaseText property was deprecated.

Per the StringEnumConverter class, "StringEnumConverter.CamelCaseText is obsolete. Set StringEnumConverter.NamingStrategy with CamelCaseNamingStrategy instead." As a result, I made the following change in scenarios where CamelCaseText == true:

// Deprecated approach
new StringEnumConverter() { CamelCaseText = true };

// New approach
new StringEnumConverter() { NamingStrategy = new CamelCaseNamingStrategy() };

While this seems straightforward, I am unsure how to approach scenarios where CamelCaseText == false. I have read the CamelCaseNamingStrategy class but do not know where I would disable the camel-casing. My guess is that I need to use a different naming strategy class than CamelCaseNamingStrategy such as DefaultNamingStrategy, but I am unsure what the difference is in the behavior of this class.

Can anybody point me to documentation explaining the difference, and/or help me understand which naming strategy to use in this case?

// Deprecated approach
new StringEnumConverter() { CamelCaseText = false };

// New approach
new StringEnumConverter() { NamingStrategy = ? };
2 Answers

You need to replace it with the NamingStrategy, use following code instead:

new StringEnumConverter { NamingStrategy = new DefaultNamingStrategy() };

Other alternatives are: CamelCaseNamingStrategy , KebabCaseNamingStrategy , SnakeCaseNamingStrategy

You can just do new StringEnumConverter() without modifying any properties.

By default, StringEnumConverter does not transform the enum names in any way. This is explained in the docs for the obsolete StringEnumConverter.CamelCaseText which state:

Gets or sets a value indicating whether the written enum text should be camel case. The default value is false.

If you feel a need to set the value of NamingStrategy you can set it to null (the default value), or new DefaultNamingStrategy(). Either choice will cause StringEnumConverter to not modify enum names.

Related