1

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!

Community
  • 1
  • 1
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
  • 1
    Maybe you need to remove the pre-existing formatter? See http://stackoverflow.com/questions/25224824/how-to-change-default-web-api-2-to-json-formatter and also http://stackoverflow.com/questions/20191980/how-to-change-default-asp-net-mvc-web-api-media-formatter – dbc Feb 02 '16 at 21:59
  • @dbc When I remove the BrowserJsonFormatter it throws exceptions on all the serializations - I tried to clear all the formatters without any result – Michail Michailidis Feb 03 '16 at 14:31
  • @dbc Actually the clear you suggested and changing the order made it!! – Michail Michailidis Feb 03 '16 at 14:40

1 Answers1

1

As @dbc suggested, putting a config.Formatters.Clear() for the previous formatters and changing the order - made it work.

Here is the final code that converts enums to strings in any kind of data-structure:

        public static void Register(HttpConfiguration config)
        {



            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Formatters.Clear();
            config.Formatters.Add(new BrowserJsonFormatter());

            //enum formatter as strings
            config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());

            // DateTime Formatter
            config.Formatters.JsonFormatter.SerializerSettings
                .DateFormatString = "o";


            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );




        }
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106