Set default global json serializer settings

marcus picture marcus · Feb 16, 2014 · Viewed 57.8k times · Source

I'm trying to set the global serializer settings like this in my global.asax.

var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings = new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    TypeNameHandling = TypeNameHandling.Objects,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

When serializing object using the following code the global serializer settings are not used?

return new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new StringContent(JsonConvert.SerializeObject(page))
};

Isn't it possible to set the global serializer settings like this or am I missing something?

Answer

marcus picture marcus · Feb 16, 2014

Setting the JsonConvert.DefaultSettings did the trick.

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    TypeNameHandling = TypeNameHandling.Objects,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};