Enum Serialization Json vs XML Enum Serialization Json vs XML xml xml

Enum Serialization Json vs XML


Since Web API RC you can get string representations of enums by applying a StringEnumConvert to the existing JsonMediaTypeFormatter converter collection during Application_Start():

var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;var enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter();jsonFormatter.SerializerSettings.Converters.Add(enumConverter);


You can accomplish this easily if you switch to using a formatter based upon Json.NET (which will ship out of the box with next drop of ASP.NET Web API). See this SO post for details:

How to tell Json.Net globally to apply the StringEnumConverter to all enums


To use JsonMediaTypeFormatter and enumConverter both we can use below code. //code start here

var serializerSettings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;var enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter();serializerSettings.Converters.Add(enumConverter);GlobalConfiguration.Configuration.Formatters.Clear();GlobalConfiguration.Configuration.Formatters.Add(new PartialJsonMediaTypeFormatter(){     IgnoreCase = true,     SerializerSettings = serializerSettings});