Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //using Newtonsoft.Json;
- //using Newtonsoft.Json.Serialization;
- public static class JsonExtensions
- {
- public static string ToJson(this object obj)
- {
- //as seen on http://www.newtonsoft.com/json/help/html/contractresolver.htm
- //return JsonConvert.SerializeObject(obj, Formatting.Indented,
- // new JsonSerializerSettings
- // {
- // ContractResolver = new CamelCasePropertyNamesContractResolver()
- // });
- //same code written a little bit more clearly
- JsonSerializerSettings jsonSerializerSetting = new JsonSerializerSettings();
- jsonSerializerSetting.ContractResolver = new CamelCasePropertyNamesContractResolver();
- return JsonConvert.SerializeObject(obj,Formatting.Indented, jsonSerializerSetting);
- }
- // Usage: responseJson.ToObject<List<object>>();
- public static T ToObject<T>(this string jsonString)
- {
- return (T)JsonConvert.DeserializeObject<T>(jsonString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement