I know that I can set and make all enums to be converted to strings during serialization like this (from JSON serialization of enum as string):
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.Converters.Add(
new StringEnumConverter
{
CamelCaseText = true
}
);
but it seems that is Global ignored in all cases I tried Dictionary<int,List<SomeEnum>>
, Dictionary<int, SomeEnum>
, List<SomeEnum>
or even SomeEnum!
Here is my configuration:
public static class WebApiConfig
{
//https://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome/20556625#20556625
private class BrowserJsonFormatter : JsonMediaTypeFormatter
{
public BrowserJsonFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue("application/json");
}
}
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Formatters.Add(new BrowserJsonFormatter());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// DateTime Formatter
config.Formatters.JsonFormatter.SerializerSettings
.DateFormatString = "o";
// enum Formatter to String
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
}
}
Any help is appreciated!