Skip to content

Instantly share code, notes, and snippets.

@mishrsud
Created August 14, 2016 07:22
Show Gist options
  • Select an option

  • Save mishrsud/ba75cc799decb8e3f1a8185352736250 to your computer and use it in GitHub Desktop.

Select an option

Save mishrsud/ba75cc799decb8e3f1a8185352736250 to your computer and use it in GitHub Desktop.
Customize JSON serialization using Newtonsoft.Json to use camel case on property names and strings for Enum
/// <summary>
/// Customize JSON serialization using Newtonsoft.Json
/// </summary>
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static class JsonExtensions
{
public static string ToJson<T>(this T obj, bool includeNull = true)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new JsonConvert[] { new StringEnumConverter() },
NullValueHandling = includeNull ? NullValueHandling.Include : NullValueHandling.Exclude;
};
return JsonConvert.SerializeObject(obj, settings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment