JsonSerializer.cs 6.8 KB

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