JsonNetHelper.cs 8.7 KB

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