XmlSerializer.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using MediaBrowser.Common.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml;
  6. namespace MediaBrowser.Common.Serialization
  7. {
  8. /// <summary>
  9. /// Provides a wrapper around third party xml serialization.
  10. /// </summary>
  11. public class XmlSerializer
  12. {
  13. /// <summary>
  14. /// Serializes to writer.
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="obj">The obj.</param>
  18. /// <param name="writer">The writer.</param>
  19. public static void SerializeToWriter<T>(T obj, XmlTextWriter writer)
  20. {
  21. writer.Formatting = Formatting.Indented;
  22. var netSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
  23. netSerializer.Serialize(writer, obj);
  24. }
  25. /// <summary>
  26. /// Serializes to writer.
  27. /// </summary>
  28. /// <param name="obj">The obj.</param>
  29. /// <param name="writer">The writer.</param>
  30. public static void SerializeToWriter(object obj, XmlTextWriter writer)
  31. {
  32. writer.Formatting = Formatting.Indented;
  33. var netSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
  34. netSerializer.Serialize(writer, obj);
  35. }
  36. /// <summary>
  37. /// Deserializes from stream.
  38. /// </summary>
  39. /// <typeparam name="T"></typeparam>
  40. /// <param name="stream">The stream.</param>
  41. /// <returns>``0.</returns>
  42. public static T DeserializeFromStream<T>(Stream stream)
  43. {
  44. using (var reader = new XmlTextReader(stream))
  45. {
  46. var netSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
  47. return (T)netSerializer.Deserialize(reader);
  48. }
  49. }
  50. /// <summary>
  51. /// Deserializes from stream.
  52. /// </summary>
  53. /// <param name="type">The type.</param>
  54. /// <param name="stream">The stream.</param>
  55. /// <returns>System.Object.</returns>
  56. public static object DeserializeFromStream(Type type, Stream stream)
  57. {
  58. using (var reader = new XmlTextReader(stream))
  59. {
  60. var netSerializer = new System.Xml.Serialization.XmlSerializer(type);
  61. return netSerializer.Deserialize(reader);
  62. }
  63. }
  64. /// <summary>
  65. /// Serializes to stream.
  66. /// </summary>
  67. /// <param name="obj">The obj.</param>
  68. /// <param name="stream">The stream.</param>
  69. public static void SerializeToStream(object obj, Stream stream)
  70. {
  71. using (var writer = new XmlTextWriter(stream, null))
  72. {
  73. SerializeToWriter(obj, writer);
  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. public static T DeserializeFromFile<T>(string file)
  83. {
  84. using (var stream = File.OpenRead(file))
  85. {
  86. return DeserializeFromStream<T>(stream);
  87. }
  88. }
  89. /// <summary>
  90. /// Serializes to file.
  91. /// </summary>
  92. /// <param name="obj">The obj.</param>
  93. /// <param name="file">The file.</param>
  94. public static void SerializeToFile(object obj, string file)
  95. {
  96. using (var stream = new FileStream(file, FileMode.Create))
  97. {
  98. SerializeToStream(obj, stream);
  99. }
  100. }
  101. /// <summary>
  102. /// Deserializes from file.
  103. /// </summary>
  104. /// <param name="type">The type.</param>
  105. /// <param name="file">The file.</param>
  106. /// <returns>System.Object.</returns>
  107. public static object DeserializeFromFile(Type type, string file)
  108. {
  109. using (var stream = File.OpenRead(file))
  110. {
  111. return DeserializeFromStream(type, stream);
  112. }
  113. }
  114. /// <summary>
  115. /// Deserializes from bytes.
  116. /// </summary>
  117. /// <param name="type">The type.</param>
  118. /// <param name="buffer">The buffer.</param>
  119. /// <returns>System.Object.</returns>
  120. public static object DeserializeFromBytes(Type type, byte[] buffer)
  121. {
  122. using (var stream = new MemoryStream(buffer))
  123. {
  124. return DeserializeFromStream(type, stream);
  125. }
  126. }
  127. /// <summary>
  128. /// Serializes to bytes.
  129. /// </summary>
  130. /// <param name="obj">The obj.</param>
  131. /// <returns>System.Byte[][].</returns>
  132. public static byte[] SerializeToBytes(object obj)
  133. {
  134. using (var stream = new MemoryStream())
  135. {
  136. SerializeToStream(obj, stream);
  137. return stream.ToArray();
  138. }
  139. }
  140. /// <summary>
  141. /// Reads an xml configuration file from the file system
  142. /// It will immediately re-serialize and save if new serialization data is available due to property changes
  143. /// </summary>
  144. /// <param name="type">The type.</param>
  145. /// <param name="path">The path.</param>
  146. /// <returns>System.Object.</returns>
  147. public static object GetXmlConfiguration(Type type, string path)
  148. {
  149. Logger.LogInfo("Loading {0} at {1}", type.Name, path);
  150. object configuration;
  151. byte[] buffer = null;
  152. // Use try/catch to avoid the extra file system lookup using File.Exists
  153. try
  154. {
  155. buffer = File.ReadAllBytes(path);
  156. configuration = DeserializeFromBytes(type, buffer);
  157. }
  158. catch (FileNotFoundException)
  159. {
  160. configuration = Activator.CreateInstance(type);
  161. }
  162. // Take the object we just got and serialize it back to bytes
  163. var newBytes = SerializeToBytes(configuration);
  164. // If the file didn't exist before, or if something has changed, re-save
  165. if (buffer == null || !buffer.SequenceEqual(newBytes))
  166. {
  167. Logger.LogInfo("Saving {0} to {1}", type.Name, path);
  168. // Save it after load in case we got new items
  169. File.WriteAllBytes(path, newBytes);
  170. }
  171. return configuration;
  172. }
  173. /// <summary>
  174. /// Reads an xml configuration file from the file system
  175. /// It will immediately save the configuration after loading it, just
  176. /// in case there are new serializable properties
  177. /// </summary>
  178. /// <typeparam name="T"></typeparam>
  179. /// <param name="path">The path.</param>
  180. /// <returns>``0.</returns>
  181. public static T GetXmlConfiguration<T>(string path)
  182. where T : class
  183. {
  184. return GetXmlConfiguration(typeof(T), path) as T;
  185. }
  186. }
  187. }