JsonSerializer.cs 8.9 KB

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