JsonSerializer.cs 8.8 KB

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