2
0

JsonSerializer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /// <typeparam name="T"></typeparam>
  19. /// <param name="obj">The obj.</param>
  20. /// <param name="stream">The stream.</param>
  21. /// <exception cref="System.ArgumentNullException">obj</exception>
  22. public void SerializeToStream<T>(T obj, Stream stream)
  23. where T : class
  24. {
  25. if (obj == null)
  26. {
  27. throw new ArgumentNullException("obj");
  28. }
  29. if (stream == null)
  30. {
  31. throw new ArgumentNullException("stream");
  32. }
  33. ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
  34. }
  35. /// <summary>
  36. /// Serializes to file.
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  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<T>(T obj, string file)
  43. where T : class
  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 = File.Open(file, FileMode.Create))
  54. {
  55. SerializeToStream(obj, stream);
  56. }
  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 = File.OpenRead(file))
  76. {
  77. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  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 = File.OpenRead(file))
  95. {
  96. return ServiceStack.Text.JsonSerializer.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.JsonDateHandler.ISO8601;
  154. ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
  155. ServiceStack.Text.JsConfig.IncludeNullValues = false;
  156. }
  157. /// <summary>
  158. /// Deserializes from string.
  159. /// </summary>
  160. /// <param name="json">The json.</param>
  161. /// <param name="type">The type.</param>
  162. /// <returns>System.Object.</returns>
  163. /// <exception cref="System.ArgumentNullException">json</exception>
  164. public object DeserializeFromString(string json, Type type)
  165. {
  166. if (string.IsNullOrEmpty(json))
  167. {
  168. throw new ArgumentNullException("json");
  169. }
  170. if (type == null)
  171. {
  172. throw new ArgumentNullException("type");
  173. }
  174. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  175. }
  176. /// <summary>
  177. /// Serializes to string.
  178. /// </summary>
  179. /// <typeparam name="T"></typeparam>
  180. /// <param name="obj">The obj.</param>
  181. /// <returns>System.String.</returns>
  182. /// <exception cref="System.ArgumentNullException">obj</exception>
  183. public string SerializeToString<T>(T obj)
  184. where T : class
  185. {
  186. if (obj == null)
  187. {
  188. throw new ArgumentNullException("obj");
  189. }
  190. return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
  191. }
  192. /// <summary>
  193. /// Serializes to bytes.
  194. /// </summary>
  195. /// <typeparam name="T"></typeparam>
  196. /// <param name="obj">The obj.</param>
  197. /// <returns>System.Byte[][].</returns>
  198. /// <exception cref="System.ArgumentNullException">obj</exception>
  199. public byte[] SerializeToBytes<T>(T obj)
  200. where T : class
  201. {
  202. if (obj == null)
  203. {
  204. throw new ArgumentNullException("obj");
  205. }
  206. using (var stream = new MemoryStream())
  207. {
  208. SerializeToStream(obj, stream);
  209. return stream.ToArray();
  210. }
  211. }
  212. }
  213. }