Created
January 23, 2016 07:01
-
-
Save vicentedealencar/35472cb5708974ac1b63 to your computer and use it in GitHub Desktop.
JsonExtensions
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
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
namespace CyberBar.BaseAPI.Util | |
{ | |
public static class JsonExtensions | |
{ | |
private static JsonSerializerSettings _settings = new JsonSerializerSettings() | |
{ | |
ContractResolver = new CamelCasePropertyNamesContractResolver(), | |
DateFormatHandling = DateFormatHandling.IsoDateFormat, | |
DateParseHandling = DateParseHandling.DateTime, | |
}; | |
public static string ToJson(this object obj, bool pretty = false) | |
{ | |
return JsonConvert.SerializeObject(obj, pretty ? Formatting.Indented : Formatting.None, _settings); | |
} | |
public static T FromJson<T>(this string str) | |
{ | |
return JsonConvert.DeserializeObject<T>(str, _settings); | |
} | |
public static object FromJson(this string str) | |
{ | |
return JsonConvert.DeserializeObject(str, _settings); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment