JsonSerializer.cs 8.2 KB

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