Created
August 14, 2016 07:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <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