JsonSerializer.cs 7.0 KB

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