using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; namespace TEAMModelOS.SDK.Helper.Common.JsonHelper { public static class JsonSerialization { static JsonSerializerSettings settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.None }; /// /// 使用json序列化为字符串 /// /// 默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/" /// public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true, bool isIndented = false) { settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include; if (!string.IsNullOrWhiteSpace(dateTimeFormat)) { var jsonConverter = new List() { new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss" }; settings.Converters = jsonConverter; } //no format var format = isIndented ? Newtonsoft.Json.Formatting.Indented : Formatting.None; var json = JsonConvert.SerializeObject(input, format, settings); return json; } /// /// 使用json序列化为字符串 /// /// 默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/" /// public static string ToJsonAbs(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true, bool isIndented = false) { settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, TypeNameHandling = TypeNameHandling.All }; settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include; if (!string.IsNullOrWhiteSpace(dateTimeFormat)) { var jsonConverter = new List() { new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss" }; settings.Converters = jsonConverter; } //no format var format = isIndented ? Newtonsoft.Json.Formatting.Indented : Formatting.None; var json = JsonConvert.SerializeObject(input, format, settings); return json; } /// /// 从序列化字符串里反序列化 /// /// /// /// 默认null,即使用json.net默认的序列化机制 /// public static T FromJsonAbs(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true) { var settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.All }; settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include; if (!string.IsNullOrWhiteSpace(dateTimeFormat)) { var jsonConverter = new List() { new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss" }; settings.Converters = jsonConverter; } return JsonConvert.DeserializeObject(input, settings); } /// /// 从序列化字符串里反序列化 /// /// /// /// 默认null,即使用json.net默认的序列化机制 /// public static T TryFromJson(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true) { try { return input.FromJson(dateTimeFormat, ignoreNullValue); } catch { return default(T); } } /// /// 从字典获取对象 /// /// /// /// 默认null,即使用json.net默认的序列化机制 /// public static T DictToObj(this Dictionary dict, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true) { try { string input= ToJson(dict); return input.FromJson(dateTimeFormat, ignoreNullValue); } catch { return default(T); } } /// /// 从对象获取字典 /// /// /// /// /// public static Dictionary ObjToDict(this object obj, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true) { string input = ToJson(obj); return input.FromJson>(dateTimeFormat, ignoreNullValue); } /// /// 从序列化字符串里反序列化 /// /// /// /// 默认null,即使用json.net默认的序列化机制 /// public static T FromJson(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true) { var settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.Objects, }; settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include; if (!string.IsNullOrWhiteSpace(dateTimeFormat)) { var jsonConverter = new List() { new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss" }; settings.Converters = jsonConverter; } return JsonConvert.DeserializeObject(input, settings); } /// /// 从序列化字符串里反序列化 /// /// /// /// 默认null,即使用json.net默认的序列化机制 /// public static object FromJson(this string input, Type type, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true) { var settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.Objects, }; if (ignoreNullValue) { settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; } if (!string.IsNullOrWhiteSpace(dateTimeFormat)) { var jsonConverter = new List() { new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss" }; settings.Converters = jsonConverter; } return JsonConvert.DeserializeObject(input, type, settings); } } }