JsonSerialization.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Grpc.Extension.Common
  7. {
  8. /// <summary>
  9. /// JsonSerialization
  10. /// </summary>
  11. public static class JsonSerialization
  12. {
  13. static JsonSerializerSettings settings = new JsonSerializerSettings()
  14. {
  15. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  16. PreserveReferencesHandling = PreserveReferencesHandling.Objects
  17. };
  18. /// <summary>
  19. /// 使用json序列化为字符串
  20. /// </summary>
  21. /// <param name="input"></param>
  22. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/"</param>
  23. /// <param name="ignoreNullValue"></param>
  24. /// <param name="isIndented"></param>
  25. /// <returns></returns>
  26. public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true, bool isIndented = false)
  27. {
  28. settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
  29. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  30. {
  31. var jsonConverter = new List<JsonConverter>()
  32. {
  33. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  34. };
  35. settings.Converters = jsonConverter;
  36. }
  37. //no format
  38. var format = isIndented ? Newtonsoft.Json.Formatting.Indented : Formatting.None;
  39. var json = JsonConvert.SerializeObject(input, format, settings);
  40. return json;
  41. }
  42. /// <summary>
  43. /// 从序列化字符串里反序列化
  44. /// </summary>
  45. /// <typeparam name="T"></typeparam>
  46. /// <param name="input"></param>
  47. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  48. /// <param name="ignoreNullValue"></param>
  49. /// <returns></returns>
  50. public static T TryFromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  51. {
  52. try
  53. {
  54. return input.FromJson<T>(dateTimeFormat, ignoreNullValue);
  55. }
  56. catch
  57. {
  58. return default(T);
  59. }
  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. /// <param name="ignoreNullValue"></param>
  68. /// <returns></returns>
  69. public static T FromJson<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. };
  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. /// <param name="input"></param>
  91. /// <param name="type"></param>
  92. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  93. /// <param name="ignoreNullValue"></param>
  94. /// <returns></returns>
  95. public static object FromJson(this string input, Type type, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  96. {
  97. var settings = new JsonSerializerSettings()
  98. {
  99. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  100. PreserveReferencesHandling = PreserveReferencesHandling.Objects,
  101. };
  102. if (ignoreNullValue)
  103. {
  104. settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  105. }
  106. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  107. {
  108. var jsonConverter = new List<JsonConverter>()
  109. {
  110. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  111. };
  112. settings.Converters = jsonConverter;
  113. }
  114. return JsonConvert.DeserializeObject(input, type, settings);
  115. }
  116. }
  117. }