2
0

JsonSerializer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Model.IO;
  4. using MediaBrowser.Model.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.Debug("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 async Task<T> DeserializeFromStreamAsync<T>(Stream stream)
  123. {
  124. if (stream == null)
  125. {
  126. throw new ArgumentNullException("stream");
  127. }
  128. using (var reader = new StreamReader(stream))
  129. {
  130. var json = await reader.ReadToEndAsync().ConfigureAwait(false);
  131. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(json);
  132. }
  133. }
  134. /// <summary>
  135. /// Deserializes from string.
  136. /// </summary>
  137. /// <typeparam name="T"></typeparam>
  138. /// <param name="text">The text.</param>
  139. /// <returns>``0.</returns>
  140. /// <exception cref="System.ArgumentNullException">text</exception>
  141. public T DeserializeFromString<T>(string text)
  142. {
  143. if (string.IsNullOrEmpty(text))
  144. {
  145. throw new ArgumentNullException("text");
  146. }
  147. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text);
  148. }
  149. /// <summary>
  150. /// Deserializes from stream.
  151. /// </summary>
  152. /// <param name="stream">The stream.</param>
  153. /// <param name="type">The type.</param>
  154. /// <returns>System.Object.</returns>
  155. /// <exception cref="System.ArgumentNullException">stream</exception>
  156. public object DeserializeFromStream(Stream stream, Type type)
  157. {
  158. if (stream == null)
  159. {
  160. throw new ArgumentNullException("stream");
  161. }
  162. if (type == null)
  163. {
  164. throw new ArgumentNullException("type");
  165. }
  166. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  167. }
  168. public async Task<object> DeserializeFromStreamAsync(Stream stream, Type type)
  169. {
  170. if (stream == null)
  171. {
  172. throw new ArgumentNullException("stream");
  173. }
  174. if (type == null)
  175. {
  176. throw new ArgumentNullException("type");
  177. }
  178. using (var reader = new StreamReader(stream))
  179. {
  180. var json = await reader.ReadToEndAsync().ConfigureAwait(false);
  181. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  182. }
  183. }
  184. /// <summary>
  185. /// Configures this instance.
  186. /// </summary>
  187. private void Configure()
  188. {
  189. ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601;
  190. ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
  191. ServiceStack.Text.JsConfig.IncludeNullValues = false;
  192. ServiceStack.Text.JsConfig.AlwaysUseUtc = true;
  193. ServiceStack.Text.JsConfig.AssumeUtc = true;
  194. ServiceStack.Text.JsConfig<Guid>.SerializeFn = SerializeGuid;
  195. }
  196. private string SerializeGuid(Guid guid)
  197. {
  198. if (guid.Equals(Guid.Empty))
  199. {
  200. return null;
  201. }
  202. return guid.ToString("N");
  203. }
  204. /// <summary>
  205. /// Deserializes from string.
  206. /// </summary>
  207. /// <param name="json">The json.</param>
  208. /// <param name="type">The type.</param>
  209. /// <returns>System.Object.</returns>
  210. /// <exception cref="System.ArgumentNullException">json</exception>
  211. public object DeserializeFromString(string json, Type type)
  212. {
  213. if (string.IsNullOrEmpty(json))
  214. {
  215. throw new ArgumentNullException("json");
  216. }
  217. if (type == null)
  218. {
  219. throw new ArgumentNullException("type");
  220. }
  221. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  222. }
  223. /// <summary>
  224. /// Serializes to string.
  225. /// </summary>
  226. /// <param name="obj">The obj.</param>
  227. /// <returns>System.String.</returns>
  228. /// <exception cref="System.ArgumentNullException">obj</exception>
  229. public string SerializeToString(object obj)
  230. {
  231. if (obj == null)
  232. {
  233. throw new ArgumentNullException("obj");
  234. }
  235. return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
  236. }
  237. }
  238. }