JsonSerializer.cs 8.6 KB

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