JsonSerialization.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace WebTest.JsonPath
  6. {
  7. public static class JsonSerialization
  8. {
  9. static JsonSerializerSettings settings = new JsonSerializerSettings()
  10. {
  11. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  12. PreserveReferencesHandling = PreserveReferencesHandling.Objects
  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. /// 从序列化字符串里反序列化
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="input"></param>
  40. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  41. /// <returns></returns>
  42. public static T TryFromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  43. {
  44. try
  45. {
  46. return input.FromJson<T>(dateTimeFormat, ignoreNullValue);
  47. }
  48. catch
  49. {
  50. return default(T);
  51. }
  52. }
  53. /// <summary>
  54. /// 从序列化字符串里反序列化
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="input"></param>
  58. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  59. /// <returns></returns>
  60. public static T FromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  61. {
  62. var settings = new JsonSerializerSettings()
  63. {
  64. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  65. PreserveReferencesHandling = PreserveReferencesHandling.Objects,
  66. };
  67. settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
  68. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  69. {
  70. var jsonConverter = new List<JsonConverter>()
  71. {
  72. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  73. };
  74. settings.Converters = jsonConverter;
  75. }
  76. return JsonConvert.DeserializeObject<T>(input, settings);
  77. }
  78. /// <summary>
  79. /// 从序列化字符串里反序列化
  80. /// </summary>
  81. /// <typeparam name="T"></typeparam>
  82. /// <param name="input"></param>
  83. /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
  84. /// <returns></returns>
  85. public static object FromJson(this string input, Type type, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
  86. {
  87. var settings = new JsonSerializerSettings()
  88. {
  89. ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
  90. PreserveReferencesHandling = PreserveReferencesHandling.Objects,
  91. };
  92. if (ignoreNullValue)
  93. {
  94. settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  95. }
  96. if (!string.IsNullOrWhiteSpace(dateTimeFormat))
  97. {
  98. var jsonConverter = new List<JsonConverter>()
  99. {
  100. new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
  101. };
  102. settings.Converters = jsonConverter;
  103. }
  104. return JsonConvert.DeserializeObject(input, type, settings);
  105. }
  106. }
  107. }