using System;using System.Collections.Generic;using System.Linq;using System.Text;using Newtonsoft.Json;using Newtonsoft.Json.Converters;using Newtonsoft.Json.Serialization; namespace CapMon.Utilities{ public static class JsonHelper { private static JsonSerializerSettings _jsonSettings; static JsonHelper() { IsoDateTimeConverter datetimeConverter = new IsoDateTimeConverter(); datetimeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; _jsonSettings = new JsonSerializerSettings(); _jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); _jsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; _jsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; _jsonSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; _jsonSettings.Converters.Add(datetimeConverter); } public static string ToJson(this object obj) { try { if (null == obj) return null; return JsonConvert.SerializeObject(obj, Formatting.None, _jsonSettings); } catch (Exception ex) { Logging.LogExeption(ex); return null; } } public static T FromJson(this string json) { try { return JsonConvert.DeserializeObject (json, _jsonSettings); } catch (Exception ex) { Logging.LogExeption(ex); return default(T); } } }}