JsonSerializer.cs 8.9 KB

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