JsonSerializer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /// <summary>
  55. /// Deserializes from file.
  56. /// </summary>
  57. /// <param name="type">The type.</param>
  58. /// <param name="file">The file.</param>
  59. /// <returns>System.Object.</returns>
  60. /// <exception cref="System.ArgumentNullException">type</exception>
  61. public object DeserializeFromFile(Type type, string file)
  62. {
  63. if (type == null)
  64. {
  65. throw new ArgumentNullException("type");
  66. }
  67. if (string.IsNullOrEmpty(file))
  68. {
  69. throw new ArgumentNullException("file");
  70. }
  71. using (Stream stream = File.OpenRead(file))
  72. {
  73. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  74. }
  75. }
  76. /// <summary>
  77. /// Deserializes from file.
  78. /// </summary>
  79. /// <typeparam name="T"></typeparam>
  80. /// <param name="file">The file.</param>
  81. /// <returns>``0.</returns>
  82. /// <exception cref="System.ArgumentNullException">file</exception>
  83. public T DeserializeFromFile<T>(string file)
  84. where T : class
  85. {
  86. if (string.IsNullOrEmpty(file))
  87. {
  88. throw new ArgumentNullException("file");
  89. }
  90. using (Stream stream = File.OpenRead(file))
  91. {
  92. return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
  93. }
  94. }
  95. /// <summary>
  96. /// Deserializes from stream.
  97. /// </summary>
  98. /// <typeparam name="T"></typeparam>
  99. /// <param name="stream">The stream.</param>
  100. /// <returns>``0.</returns>
  101. /// <exception cref="System.ArgumentNullException">stream</exception>
  102. public T DeserializeFromStream<T>(Stream stream)
  103. {
  104. if (stream == null)
  105. {
  106. throw new ArgumentNullException("stream");
  107. }
  108. return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
  109. }
  110. /// <summary>
  111. /// Deserializes from string.
  112. /// </summary>
  113. /// <typeparam name="T"></typeparam>
  114. /// <param name="text">The text.</param>
  115. /// <returns>``0.</returns>
  116. /// <exception cref="System.ArgumentNullException">text</exception>
  117. public T DeserializeFromString<T>(string text)
  118. {
  119. if (string.IsNullOrEmpty(text))
  120. {
  121. throw new ArgumentNullException("text");
  122. }
  123. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text);
  124. }
  125. /// <summary>
  126. /// Deserializes from stream.
  127. /// </summary>
  128. /// <param name="stream">The stream.</param>
  129. /// <param name="type">The type.</param>
  130. /// <returns>System.Object.</returns>
  131. /// <exception cref="System.ArgumentNullException">stream</exception>
  132. public object DeserializeFromStream(Stream stream, Type type)
  133. {
  134. if (stream == null)
  135. {
  136. throw new ArgumentNullException("stream");
  137. }
  138. if (type == null)
  139. {
  140. throw new ArgumentNullException("type");
  141. }
  142. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  143. }
  144. /// <summary>
  145. /// Configures this instance.
  146. /// </summary>
  147. private void Configure()
  148. {
  149. ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
  150. ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
  151. ServiceStack.Text.JsConfig.IncludeNullValues = false;
  152. ServiceStack.Text.JsConfig.AlwaysUseUtc = true;
  153. ServiceStack.Text.JsConfig.AssumeUtc = true;
  154. }
  155. /// <summary>
  156. /// Deserializes from string.
  157. /// </summary>
  158. /// <param name="json">The json.</param>
  159. /// <param name="type">The type.</param>
  160. /// <returns>System.Object.</returns>
  161. /// <exception cref="System.ArgumentNullException">json</exception>
  162. public object DeserializeFromString(string json, Type type)
  163. {
  164. if (string.IsNullOrEmpty(json))
  165. {
  166. throw new ArgumentNullException("json");
  167. }
  168. if (type == null)
  169. {
  170. throw new ArgumentNullException("type");
  171. }
  172. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  173. }
  174. /// <summary>
  175. /// Serializes to string.
  176. /// </summary>
  177. /// <param name="obj">The obj.</param>
  178. /// <returns>System.String.</returns>
  179. /// <exception cref="System.ArgumentNullException">obj</exception>
  180. public string SerializeToString(object obj)
  181. {
  182. if (obj == null)
  183. {
  184. throw new ArgumentNullException("obj");
  185. }
  186. return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
  187. }
  188. /// <summary>
  189. /// Serializes to bytes.
  190. /// </summary>
  191. /// <param name="obj">The obj.</param>
  192. /// <returns>System.Byte[][].</returns>
  193. /// <exception cref="System.ArgumentNullException">obj</exception>
  194. public byte[] SerializeToBytes(object obj)
  195. {
  196. if (obj == null)
  197. {
  198. throw new ArgumentNullException("obj");
  199. }
  200. using (var stream = new MemoryStream())
  201. {
  202. SerializeToStream(obj, stream);
  203. return stream.ToArray();
  204. }
  205. }
  206. }
  207. }