JsonSerializer.cs 7.0 KB

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