2
0

JsonSerializer.cs 8.4 KB

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