using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace MEAS.Common { public class JsonHelper { private static JsonHelper _jsonHelper = new JsonHelper(); public static JsonHelper Instance { get { return _jsonHelper; } } public string Serialize(object obj) { return JsonConvert.SerializeObject(obj, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }); } public string SerializeDateFormat(object obj) { return JsonConvert.SerializeObject(obj, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" }); } public string SerializeByConverter(object obj, params JsonConverter[] converters) { return JsonConvert.SerializeObject(obj, converters); } public T Deserialize<T>(string input) { return JsonConvert.DeserializeObject<T>(input, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); } public T DeserializeByConverter<T>(string input, params JsonConverter[] converter) { return JsonConvert.DeserializeObject<T>(input, converter); } public T DeserializeBySetting<T>(string input, JsonSerializerSettings settings) { return JsonConvert.DeserializeObject<T>(input, settings); } private object NullToEmpty(object obj) { return null; } } }
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!