TextJsonSerializer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Specialized;
  2. using System.IO;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. namespace EasyDevCore.Remote.HttpAccess
  6. {
  7. /// <summary>
  8. /// ISerializer implementation that uses System.Text.Json
  9. /// Default serializer used in calls to GetJson, etc.
  10. /// </summary>
  11. public class TextJsonSerializer : ISerializer
  12. {
  13. private static JsonSerializerOptions _defaultOptions;
  14. private JsonSerializerOptions _options;
  15. private JsonSerializerOptions _serializerOptions => _options ?? _defaultOptions;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="TextJsonSerializer"/> class.
  18. /// </summary>
  19. public TextJsonSerializer()
  20. {
  21. if (_defaultOptions == null)
  22. {
  23. _defaultOptions = new JsonSerializerOptions()
  24. {
  25. PropertyNameCaseInsensitive = true,
  26. WriteIndented = true,
  27. };
  28. _defaultOptions.Converters.Add(new JsonStringEnumConverter());
  29. _defaultOptions.Converters.Add(new JsonDateTimeConverter());
  30. }
  31. }
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="TextJsonSerializer"/> class.
  34. /// </summary>
  35. /// <param name="options">The options.</param>
  36. public TextJsonSerializer(JsonSerializerOptions options)
  37. {
  38. _options = options;
  39. }
  40. /// <summary>
  41. /// Serializes the specified object.
  42. /// </summary>
  43. /// <param name="obj">The object.</param>
  44. /// <returns></returns>
  45. public string Serialize(object obj)
  46. {
  47. return JsonSerializer.Serialize(obj, _serializerOptions);
  48. }
  49. /// <summary>
  50. /// Serializes the specified object.
  51. /// </summary>
  52. /// <typeparam name="T"></typeparam>
  53. /// <param name="obj">The object.</param>
  54. /// <returns></returns>
  55. public string Serialize<T>(T obj)
  56. {
  57. return JsonSerializer.Serialize(obj, _serializerOptions);
  58. }
  59. /// <summary>
  60. /// Deserializes the specified s.
  61. /// </summary>
  62. /// <typeparam name="T"></typeparam>
  63. /// <param name="s">The s.</param>
  64. /// <returns></returns>
  65. public T Deserialize<T>(string s)
  66. {
  67. return JsonSerializer.Deserialize<T>(s, _serializerOptions);
  68. }
  69. /// <summary>
  70. /// Deserializes the specified stream.
  71. /// </summary>
  72. /// <typeparam name="T"></typeparam>
  73. /// <param name="stream">The stream.</param>
  74. /// <returns></returns>
  75. public T Deserialize<T>(Stream stream)
  76. {
  77. return JsonSerializer.Deserialize<T>(stream, _serializerOptions);
  78. }
  79. /// <summary>
  80. /// Deserializes the asynchronous.
  81. /// </summary>
  82. /// <typeparam name="T"></typeparam>
  83. /// <param name="stream">The stream.</param>
  84. /// <returns></returns>
  85. public async Task<T> DeserializeAsync<T>(Stream stream)
  86. {
  87. return await JsonSerializer.DeserializeAsync<T>(stream, _serializerOptions).ConfigureAwait(false);
  88. }
  89. }
  90. }