Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MrAntix/47efb3bc249199cee4e7a151ca6e3ac3 to your computer and use it in GitHub Desktop.
Save MrAntix/47efb3bc249199cee4e7a151ca6e3ac3 to your computer and use it in GitHub Desktop.
Configure JSON.NET for camels and dates
public class SpecialCamelCasePropertyNamesContractResolver :
CamelCasePropertyNamesContractResolver
{
protected override JsonDictionaryContract CreateDictionaryContract(
Type objectType)
{
var contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = propertyName => propertyName;
return contract;
}
}
public static HttpConfiguration ConfigureForJson(
this HttpConfiguration configuration)
{
var formatter = configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver
= new SpecialCamelCasePropertyNamesContractResolver();
configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
new IsoDateTimeConverter()
{
DateTimeFormat = "yyyy-MM-ddTHH:mmZ"
});
configuration.Formatters.Clear();
configuration.Formatters.Add(formatter);
return configuration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment