Skip to content

Commit

Permalink
U4-8614 JsonCamelCaseFormatter breaks the global configuration for th…
Browse files Browse the repository at this point in the history
…e default JsonMediaTypeFormatter
  • Loading branch information
Shazwazza committed Jun 20, 2016
1 parent 465a02d commit bc2b9ab
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Umbraco.Web/WebApi/JsonCamelCaseFormatter.cs
@@ -1,5 +1,8 @@
using System;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http.Controllers;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Umbraco.Web.WebApi
Expand All @@ -11,7 +14,21 @@ public class JsonCamelCaseFormatter : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
controllerSettings.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//remove all json formatters then add our custom one
var toRemove = controllerSettings.Formatters.Where(t => (t is JsonMediaTypeFormatter)).ToList();
foreach (var r in toRemove)
{
controllerSettings.Formatters.Remove(r);
}

var jsonFormatter = new JsonMediaTypeFormatter
{
SerializerSettings =
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
};
controllerSettings.Formatters.Add(jsonFormatter);
}
}
}

0 comments on commit bc2b9ab

Please sign in to comment.