JsonSerializer.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Model.IO;
  5. using MediaBrowser.Model.Serialization;
  6. using Microsoft.Extensions.Logging;
  7. namespace Emby.Common.Implementations.Serialization
  8. {
  9. /// <summary>
  10. /// Provides a wrapper around third party json serialization.
  11. /// </summary>
  12. public class JsonSerializer : IJsonSerializer
  13. {
  14. private readonly IFileSystem _fileSystem;
  15. public JsonSerializer(
  16. IFileSystem fileSystem)
  17. {
  18. _fileSystem = fileSystem;
  19. Configure();
  20. }
  21. /// <summary>
  22. /// Serializes to stream.
  23. /// </summary>
  24. /// <param name="obj">The obj.</param>
  25. /// <param name="stream">The stream.</param>
  26. /// <exception cref="ArgumentNullException">obj</exception>
  27. public void SerializeToStream(object obj, Stream stream)
  28. {
  29. if (obj == null)
  30. {
  31. throw new ArgumentNullException(nameof(obj));
  32. }
  33. if (stream == null)
  34. {
  35. throw new ArgumentNullException(nameof(stream));
  36. }
  37. ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
  38. }
  39. /// <summary>
  40. /// Serializes to file.
  41. /// </summary>
  42. /// <param name="obj">The obj.</param>
  43. /// <param name="file">The file.</param>
  44. /// <exception cref="ArgumentNullException">obj</exception>
  45. public void SerializeToFile(object obj, string file)
  46. {
  47. if (obj == null)
  48. {
  49. throw new ArgumentNullException(nameof(obj));
  50. }
  51. if (string.IsNullOrEmpty(file))
  52. {
  53. throw new ArgumentNullException(nameof(file));
  54. }
  55. using (var stream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
  56. {
  57. SerializeToStream(obj, stream);
  58. }
  59. }
  60. private static Stream OpenFile(string path)
  61. {
  62. return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 131072);
  63. }
  64. /// <summary>
  65. /// Deserializes from file.
  66. /// </summary>
  67. /// <param name="type">The type.</param>
  68. /// <param name="file">The file.</param>
  69. /// <returns>System.Object.</returns>
  70. /// <exception cref="ArgumentNullException">type</exception>
  71. public object DeserializeFromFile(Type type, string file)
  72. {
  73. if (type == null)
  74. {
  75. throw new ArgumentNullException(nameof(type));
  76. }
  77. if (string.IsNullOrEmpty(file))
  78. {
  79. throw new ArgumentNullException(nameof(file));
  80. }
  81. using (var stream = OpenFile(file))
  82. {
  83. return DeserializeFromStream(stream, type);
  84. }
  85. }
  86. /// <summary>
  87. /// Deserializes from file.
  88. /// </summary>
  89. /// <typeparam name="T"></typeparam>
  90. /// <param name="file">The file.</param>
  91. /// <returns>``0.</returns>
  92. /// <exception cref="ArgumentNullException">file</exception>
  93. public T DeserializeFromFile<T>(string file)
  94. where T : class
  95. {
  96. if (string.IsNullOrEmpty(file))
  97. {
  98. throw new ArgumentNullException(nameof(file));
  99. }
  100. using (var stream = OpenFile(file))
  101. {
  102. return DeserializeFromStream<T>(stream);
  103. }
  104. }
  105. /// <summary>
  106. /// Deserializes from stream.
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <param name="stream">The stream.</param>
  110. /// <returns>``0.</returns>
  111. /// <exception cref="ArgumentNullException">stream</exception>
  112. public T DeserializeFromStream<T>(Stream stream)
  113. {
  114. if (stream == null)
  115. {
  116. throw new ArgumentNullException(nameof(stream));
  117. }
  118. return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
  119. }
  120. public Task<T> DeserializeFromStreamAsync<T>(Stream stream)
  121. {
  122. if (stream == null)
  123. {
  124. throw new ArgumentNullException(nameof(stream));
  125. }
  126. return ServiceStack.Text.JsonSerializer.DeserializeFromStreamAsync<T>(stream);
  127. }
  128. /// <summary>
  129. /// Deserializes from string.
  130. /// </summary>
  131. /// <typeparam name="T"></typeparam>
  132. /// <param name="text">The text.</param>
  133. /// <returns>``0.</returns>
  134. /// <exception cref="ArgumentNullException">text</exception>
  135. public T DeserializeFromString<T>(string text)
  136. {
  137. if (string.IsNullOrEmpty(text))
  138. {
  139. throw new ArgumentNullException(nameof(text));
  140. }
  141. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text);
  142. }
  143. /// <summary>
  144. /// Deserializes from stream.
  145. /// </summary>
  146. /// <param name="stream">The stream.</param>
  147. /// <param name="type">The type.</param>
  148. /// <returns>System.Object.</returns>
  149. /// <exception cref="ArgumentNullException">stream</exception>
  150. public object DeserializeFromStream(Stream stream, Type type)
  151. {
  152. if (stream == null)
  153. {
  154. throw new ArgumentNullException(nameof(stream));
  155. }
  156. if (type == null)
  157. {
  158. throw new ArgumentNullException(nameof(type));
  159. }
  160. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  161. }
  162. public async Task<object> DeserializeFromStreamAsync(Stream stream, Type type)
  163. {
  164. if (stream == null)
  165. {
  166. throw new ArgumentNullException(nameof(stream));
  167. }
  168. if (type == null)
  169. {
  170. throw new ArgumentNullException(nameof(type));
  171. }
  172. using (var reader = new StreamReader(stream))
  173. {
  174. var json = await reader.ReadToEndAsync().ConfigureAwait(false);
  175. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  176. }
  177. }
  178. /// <summary>
  179. /// Configures this instance.
  180. /// </summary>
  181. private void Configure()
  182. {
  183. ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601;
  184. ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
  185. ServiceStack.Text.JsConfig.IncludeNullValues = false;
  186. ServiceStack.Text.JsConfig.AlwaysUseUtc = true;
  187. ServiceStack.Text.JsConfig.AssumeUtc = true;
  188. ServiceStack.Text.JsConfig<Guid>.SerializeFn = SerializeGuid;
  189. }
  190. private static string SerializeGuid(Guid guid)
  191. {
  192. if (guid.Equals(Guid.Empty))
  193. {
  194. return null;
  195. }
  196. return guid.ToString("N");
  197. }
  198. /// <summary>
  199. /// Deserializes from string.
  200. /// </summary>
  201. /// <param name="json">The json.</param>
  202. /// <param name="type">The type.</param>
  203. /// <returns>System.Object.</returns>
  204. /// <exception cref="ArgumentNullException">json</exception>
  205. public object DeserializeFromString(string json, Type type)
  206. {
  207. if (string.IsNullOrEmpty(json))
  208. {
  209. throw new ArgumentNullException(nameof(json));
  210. }
  211. if (type == null)
  212. {
  213. throw new ArgumentNullException(nameof(type));
  214. }
  215. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  216. }
  217. /// <summary>
  218. /// Serializes to string.
  219. /// </summary>
  220. /// <param name="obj">The obj.</param>
  221. /// <returns>System.String.</returns>
  222. /// <exception cref="ArgumentNullException">obj</exception>
  223. public string SerializeToString(object obj)
  224. {
  225. if (obj == null)
  226. {
  227. throw new ArgumentNullException(nameof(obj));
  228. }
  229. return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
  230. }
  231. }
  232. }