How to tell Json.Net globally to apply the StringEnumConverter to all enums
Add a StringEnumConverter
to the JsonSerializerSettings
Converters collection.
Documentation: Serialize with JsonConverters
If you want the serializer to use camelCasing, you can set this as well:
SerializerSettings.Converters.Add(
new StringEnumConverter { CamelCaseText = true });
This will serialize SomeValue
to someValue
.
The other answers work for ASP.NET, but if you want to set these settings generally for calling JsonConvert in any context you can do:
JsonConvert.DefaultSettings = (() =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
return settings;
});
(See http://james.newtonking.com/archive/2013/05/08/json-net-5-0-release-5-defaultsettings-and-extension-data)
In your Global.asax.cs add
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add
(new Newtonsoft.Json.Converters.StringEnumConverter());