JsonSerializer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Model.IO;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Serialization;
  6. namespace Emby.Common.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. private readonly ILogger _logger;
  15. public JsonSerializer(IFileSystem fileSystem, ILogger logger)
  16. {
  17. _fileSystem = fileSystem;
  18. _logger = logger;
  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="System.ArgumentNullException">obj</exception>
  27. public void SerializeToStream(object obj, Stream stream)
  28. {
  29. if (obj == null)
  30. {
  31. throw new ArgumentNullException("obj");
  32. }
  33. if (stream == null)
  34. {
  35. throw new ArgumentNullException("stream");
  36. }
  37. ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
  38. }
  39. /// <summary>
  40. /// Serializes to file.
  41. /// </summary>
  42. /// <param name="obj">The obj.</param>
  43. /// <param name="file">The file.</param>
  44. /// <exception cref="System.ArgumentNullException">obj</exception>
  45. public void SerializeToFile(object obj, string file)
  46. {
  47. if (obj == null)
  48. {
  49. throw new ArgumentNullException("obj");
  50. }
  51. if (string.IsNullOrEmpty(file))
  52. {
  53. throw new ArgumentNullException("file");
  54. }
  55. using (Stream stream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
  56. {
  57. SerializeToStream(obj, stream);
  58. }
  59. }
  60. private Stream OpenFile(string path)
  61. {
  62. _logger.Debug("Deserializing file {0}", path);
  63. return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 131072);
  64. }
  65. /// <summary>
  66. /// Deserializes from file.
  67. /// </summary>
  68. /// <param name="type">The type.</param>
  69. /// <param name="file">The file.</param>
  70. /// <returns>System.Object.</returns>
  71. /// <exception cref="System.ArgumentNullException">type</exception>
  72. public object DeserializeFromFile(Type type, string file)
  73. {
  74. if (type == null)
  75. {
  76. throw new ArgumentNullException("type");
  77. }
  78. if (string.IsNullOrEmpty(file))
  79. {
  80. throw new ArgumentNullException("file");
  81. }
  82. using (Stream stream = OpenFile(file))
  83. {
  84. return DeserializeFromStream(stream, type);
  85. }
  86. }
  87. /// <summary>
  88. /// Deserializes from file.
  89. /// </summary>
  90. /// <typeparam name="T"></typeparam>
  91. /// <param name="file">The file.</param>
  92. /// <returns>``0.</returns>
  93. /// <exception cref="System.ArgumentNullException">file</exception>
  94. public T DeserializeFromFile<T>(string file)
  95. where T : class
  96. {
  97. if (string.IsNullOrEmpty(file))
  98. {
  99. throw new ArgumentNullException("file");
  100. }
  101. using (Stream stream = OpenFile(file))
  102. {
  103. return DeserializeFromStream<T>(stream);
  104. }
  105. }
  106. /// <summary>
  107. /// Deserializes from stream.
  108. /// </summary>
  109. /// <typeparam name="T"></typeparam>
  110. /// <param name="stream">The stream.</param>
  111. /// <returns>``0.</returns>
  112. /// <exception cref="System.ArgumentNullException">stream</exception>
  113. public T DeserializeFromStream<T>(Stream stream)
  114. {
  115. if (stream == null)
  116. {
  117. throw new ArgumentNullException("stream");
  118. }
  119. return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
  120. }
  121. /// <summary>
  122. /// Deserializes from string.
  123. /// </summary>
  124. /// <typeparam name="T"></typeparam>
  125. /// <param name="text">The text.</param>
  126. /// <returns>``0.</returns>
  127. /// <exception cref="System.ArgumentNullException">text</exception>
  128. public T DeserializeFromString<T>(string text)
  129. {
  130. if (string.IsNullOrEmpty(text))
  131. {
  132. throw new ArgumentNullException("text");
  133. }
  134. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text);
  135. }
  136. /// <summary>
  137. /// Deserializes from stream.
  138. /// </summary>
  139. /// <param name="stream">The stream.</param>
  140. /// <param name="type">The type.</param>
  141. /// <returns>System.Object.</returns>
  142. /// <exception cref="System.ArgumentNullException">stream</exception>
  143. public object DeserializeFromStream(Stream stream, Type type)
  144. {
  145. if (stream == null)
  146. {
  147. throw new ArgumentNullException("stream");
  148. }
  149. if (type == null)
  150. {
  151. throw new ArgumentNullException("type");
  152. }
  153. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  154. }
  155. /// <summary>
  156. /// Configures this instance.
  157. /// </summary>
  158. private void Configure()
  159. {
  160. ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601;
  161. ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
  162. ServiceStack.Text.JsConfig.IncludeNullValues = false;
  163. ServiceStack.Text.JsConfig.AlwaysUseUtc = true;
  164. ServiceStack.Text.JsConfig.AssumeUtc = true;
  165. }
  166. /// <summary>
  167. /// Deserializes from string.
  168. /// </summary>
  169. /// <param name="json">The json.</param>
  170. /// <param name="type">The type.</param>
  171. /// <returns>System.Object.</returns>
  172. /// <exception cref="System.ArgumentNullException">json</exception>
  173. public object DeserializeFromString(string json, Type type)
  174. {
  175. if (string.IsNullOrEmpty(json))
  176. {
  177. throw new ArgumentNullException("json");
  178. }
  179. if (type == null)
  180. {
  181. throw new ArgumentNullException("type");
  182. }
  183. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  184. }
  185. /// <summary>
  186. /// Serializes to string.
  187. /// </summary>
  188. /// <param name="obj">The obj.</param>
  189. /// <returns>System.String.</returns>
  190. /// <exception cref="System.ArgumentNullException">obj</exception>
  191. public string SerializeToString(object obj)
  192. {
  193. if (obj == null)
  194. {
  195. throw new ArgumentNullException("obj");
  196. }
  197. return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
  198. }
  199. }
  200. }