JsonSerialization.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace TEAMModelOS.SDK.Helper.Common.JsonHelper
  6. {
  7. public static class JsonSerialization
  8. {
  9. static JsonSerializerSettings settings = new JsonSerializerSettings()
  10. {
  11. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  12. PreserveReferencesHandling = PreserveReferencesHandling.None
  13. };
  14. /// <summary>
  15. /// 使用json序列化为字符串
  16. /// </summary>
  17. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/"</param>
  18. /// <returns></returns>
  19. public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true, bool isIndented = false)
  20. {
  21. settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
  22. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  23. {
  24. var jsonConverter = new List<JsonConverter>()
  25. {
  26. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  27. };
  28. settings.Converters = jsonConverter;
  29. }
  30. //no format
  31. var format = isIndented ? Newtonsoft.Json.Formatting.Indented : Formatting.None;
  32. var json = JsonConvert.SerializeObject(input, format, settings);
  33. return json;
  34. }
  35. /// <summary>
  36. /// 使用json序列化为字符串
  37. /// </summary>
  38. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/"</param>
  39. /// <returns></returns>
  40. public static string ToJsonAbs(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true, bool isIndented = false)
  41. {
  42. settings = new JsonSerializerSettings
  43. {
  44. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  45. TypeNameHandling = TypeNameHandling.All
  46. };
  47. settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
  48. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  49. {
  50. var jsonConverter = new List<JsonConverter>()
  51. {
  52. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  53. };
  54. settings.Converters = jsonConverter;
  55. }
  56. //no format
  57. var format = isIndented ? Newtonsoft.Json.Formatting.Indented : Formatting.None;
  58. var json = JsonConvert.SerializeObject(input, format, settings);
  59. return json;
  60. }
  61. /// <summary>
  62. /// 从序列化字符串里反序列化
  63. /// </summary>
  64. /// <typeparam name="T"></typeparam>
  65. /// <param name="input"></param>
  66. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  67. /// <returns></returns>
  68. public static T FromJsonAbs<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  69. {
  70. var settings = new JsonSerializerSettings()
  71. {
  72. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  73. PreserveReferencesHandling = PreserveReferencesHandling.Objects,
  74. TypeNameHandling = TypeNameHandling.All
  75. };
  76. settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
  77. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  78. {
  79. var jsonConverter = new List<JsonConverter>()
  80. {
  81. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  82. };
  83. settings.Converters = jsonConverter;
  84. }
  85. return JsonConvert.DeserializeObject<T>(input, settings);
  86. }
  87. /// <summary>
  88. /// 从序列化字符串里反序列化
  89. /// </summary>
  90. /// <typeparam name="T"></typeparam>
  91. /// <param name="input"></param>
  92. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  93. /// <returns></returns>
  94. public static T TryFromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  95. {
  96. try
  97. {
  98. return input.FromJson<T>(dateTimeFormat, ignoreNullValue);
  99. }
  100. catch
  101. {
  102. return default(T);
  103. }
  104. }
  105. /// <summary>
  106. /// 从字典获取对象
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <param name="input"></param>
  110. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  111. /// <returns></returns>
  112. public static T DictToObj<T>(this Dictionary<string,object> dict, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  113. {
  114. try
  115. {
  116. string input= ToJson(dict);
  117. return input.FromJson<T>(dateTimeFormat, ignoreNullValue);
  118. }
  119. catch
  120. {
  121. return default(T);
  122. }
  123. }
  124. /// <summary>
  125. /// 从对象获取字典
  126. /// </summary>
  127. /// <param name="obj"></param>
  128. /// <param name="dateTimeFormat"></param>
  129. /// <param name="ignoreNullValue"></param>
  130. /// <returns></returns>
  131. public static Dictionary<string ,object> ObjToDict(this object obj, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  132. {
  133. string input = ToJson(obj);
  134. return input.FromJson<Dictionary<string, object>>(dateTimeFormat, ignoreNullValue);
  135. }
  136. /// <summary>
  137. /// 从序列化字符串里反序列化
  138. /// </summary>
  139. /// <typeparam name="T"></typeparam>
  140. /// <param name="input"></param>
  141. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  142. /// <returns></returns>
  143. public static T FromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  144. {
  145. var settings = new JsonSerializerSettings()
  146. {
  147. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  148. PreserveReferencesHandling = PreserveReferencesHandling.Objects,
  149. };
  150. settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
  151. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  152. {
  153. var jsonConverter = new List<JsonConverter>()
  154. {
  155. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  156. };
  157. settings.Converters = jsonConverter;
  158. }
  159. return JsonConvert.DeserializeObject<T>(input, settings);
  160. }
  161. /// <summary>
  162. /// 从序列化字符串里反序列化
  163. /// </summary>
  164. /// <typeparam name="T"></typeparam>
  165. /// <param name="input"></param>
  166. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  167. /// <returns></returns>
  168. public static object FromJson(this string input, Type type, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  169. {
  170. var settings = new JsonSerializerSettings()
  171. {
  172. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  173. PreserveReferencesHandling = PreserveReferencesHandling.Objects,
  174. };
  175. if (ignoreNullValue)
  176. {
  177. settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  178. }
  179. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  180. {
  181. var jsonConverter = new List<JsonConverter>()
  182. {
  183. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  184. };
  185. settings.Converters = jsonConverter;
  186. }
  187. return JsonConvert.DeserializeObject(input, type, settings);
  188. }
  189. }
  190. }