2
0

JsonSerializer.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.IO;
  3. namespace MediaBrowser.Common.Serialization
  4. {
  5. /// <summary>
  6. /// Provides a wrapper around third party json serialization.
  7. /// </summary>
  8. public class JsonSerializer
  9. {
  10. /// <summary>
  11. /// Serializes to stream.
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. /// <param name="obj">The obj.</param>
  15. /// <param name="stream">The stream.</param>
  16. /// <exception cref="System.ArgumentNullException">obj</exception>
  17. public static void SerializeToStream<T>(T obj, Stream stream)
  18. where T : class
  19. {
  20. if (obj == null)
  21. {
  22. throw new ArgumentNullException("obj");
  23. }
  24. if (stream == null)
  25. {
  26. throw new ArgumentNullException("stream");
  27. }
  28. Configure();
  29. ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
  30. }
  31. /// <summary>
  32. /// Serializes to file.
  33. /// </summary>
  34. /// <typeparam name="T"></typeparam>
  35. /// <param name="obj">The obj.</param>
  36. /// <param name="file">The file.</param>
  37. /// <exception cref="System.ArgumentNullException">obj</exception>
  38. public static void SerializeToFile<T>(T obj, string file)
  39. where T : class
  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. Configure();
  50. using (Stream stream = File.Open(file, FileMode.Create))
  51. {
  52. SerializeToStream(obj, stream);
  53. }
  54. }
  55. /// <summary>
  56. /// Deserializes from file.
  57. /// </summary>
  58. /// <param name="type">The type.</param>
  59. /// <param name="file">The file.</param>
  60. /// <returns>System.Object.</returns>
  61. /// <exception cref="System.ArgumentNullException">type</exception>
  62. public static object DeserializeFromFile(Type type, string file)
  63. {
  64. if (type == null)
  65. {
  66. throw new ArgumentNullException("type");
  67. }
  68. if (string.IsNullOrEmpty(file))
  69. {
  70. throw new ArgumentNullException("file");
  71. }
  72. Configure();
  73. using (Stream stream = File.OpenRead(file))
  74. {
  75. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  76. }
  77. }
  78. /// <summary>
  79. /// Deserializes from file.
  80. /// </summary>
  81. /// <typeparam name="T"></typeparam>
  82. /// <param name="file">The file.</param>
  83. /// <returns>``0.</returns>
  84. /// <exception cref="System.ArgumentNullException">file</exception>
  85. public static T DeserializeFromFile<T>(string file)
  86. where T : class
  87. {
  88. if (string.IsNullOrEmpty(file))
  89. {
  90. throw new ArgumentNullException("file");
  91. }
  92. Configure();
  93. using (Stream stream = File.OpenRead(file))
  94. {
  95. return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
  96. }
  97. }
  98. /// <summary>
  99. /// Deserializes from stream.
  100. /// </summary>
  101. /// <typeparam name="T"></typeparam>
  102. /// <param name="stream">The stream.</param>
  103. /// <returns>``0.</returns>
  104. /// <exception cref="System.ArgumentNullException">stream</exception>
  105. public static T DeserializeFromStream<T>(Stream stream)
  106. {
  107. if (stream == null)
  108. {
  109. throw new ArgumentNullException("stream");
  110. }
  111. Configure();
  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 static T DeserializeFromString<T>(string text)
  122. {
  123. if (string.IsNullOrEmpty(text))
  124. {
  125. throw new ArgumentNullException("text");
  126. }
  127. Configure();
  128. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text);
  129. }
  130. /// <summary>
  131. /// Deserializes from stream.
  132. /// </summary>
  133. /// <param name="stream">The stream.</param>
  134. /// <param name="type">The type.</param>
  135. /// <returns>System.Object.</returns>
  136. /// <exception cref="System.ArgumentNullException">stream</exception>
  137. public static object DeserializeFromStream(Stream stream, Type type)
  138. {
  139. if (stream == null)
  140. {
  141. throw new ArgumentNullException("stream");
  142. }
  143. if (type == null)
  144. {
  145. throw new ArgumentNullException("type");
  146. }
  147. Configure();
  148. return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
  149. }
  150. /// <summary>
  151. /// The _is configured
  152. /// </summary>
  153. private static bool _isConfigured;
  154. /// <summary>
  155. /// Configures this instance.
  156. /// </summary>
  157. internal static void Configure()
  158. {
  159. if (!_isConfigured)
  160. {
  161. ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
  162. ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
  163. ServiceStack.Text.JsConfig.IncludeNullValues = false;
  164. _isConfigured = true;
  165. }
  166. }
  167. /// <summary>
  168. /// Deserializes from string.
  169. /// </summary>
  170. /// <param name="json">The json.</param>
  171. /// <param name="type">The type.</param>
  172. /// <returns>System.Object.</returns>
  173. /// <exception cref="System.ArgumentNullException">json</exception>
  174. public static object DeserializeFromString(string json, Type type)
  175. {
  176. if (string.IsNullOrEmpty(json))
  177. {
  178. throw new ArgumentNullException("json");
  179. }
  180. if (type == null)
  181. {
  182. throw new ArgumentNullException("type");
  183. }
  184. Configure();
  185. return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
  186. }
  187. /// <summary>
  188. /// Serializes to string.
  189. /// </summary>
  190. /// <typeparam name="T"></typeparam>
  191. /// <param name="obj">The obj.</param>
  192. /// <returns>System.String.</returns>
  193. /// <exception cref="System.ArgumentNullException">obj</exception>
  194. public static string SerializeToString<T>(T obj)
  195. where T : class
  196. {
  197. if (obj == null)
  198. {
  199. throw new ArgumentNullException("obj");
  200. }
  201. Configure();
  202. return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
  203. }
  204. /// <summary>
  205. /// Serializes to bytes.
  206. /// </summary>
  207. /// <typeparam name="T"></typeparam>
  208. /// <param name="obj">The obj.</param>
  209. /// <returns>System.Byte[][].</returns>
  210. /// <exception cref="System.ArgumentNullException">obj</exception>
  211. public static byte[] SerializeToBytes<T>(T obj)
  212. where T : class
  213. {
  214. if (obj == null)
  215. {
  216. throw new ArgumentNullException("obj");
  217. }
  218. using (var stream = new MemoryStream())
  219. {
  220. SerializeToStream(obj, stream);
  221. return stream.ToArray();
  222. }
  223. }
  224. }
  225. }