JsonSerializer.cs 7.2 KB

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