using MediaBrowser.Model.Serialization; using System; using System.Collections.Concurrent; using System.IO; using System.Xml; namespace MediaBrowser.Common.Implementations.Serialization { /// /// Provides a wrapper around third party xml serialization. /// public class XmlSerializer : IXmlSerializer { // Need to cache these // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html private readonly ConcurrentDictionary _serializers = new ConcurrentDictionary(); private System.Xml.Serialization.XmlSerializer GetSerializer(Type type) { var key = type.FullName; return _serializers.GetOrAdd(key, k => new System.Xml.Serialization.XmlSerializer(type)); } /// /// Serializes to writer. /// /// The obj. /// The writer. private void SerializeToWriter(object obj, XmlTextWriter writer) { writer.Formatting = Formatting.Indented; var netSerializer = GetSerializer(obj.GetType()); netSerializer.Serialize(writer, obj); } /// /// Deserializes from stream. /// /// The type. /// The stream. /// System.Object. public object DeserializeFromStream(Type type, Stream stream) { using (var reader = new XmlTextReader(stream)) { var netSerializer = GetSerializer(type); return netSerializer.Deserialize(reader); } } /// /// Serializes to stream. /// /// The obj. /// The stream. public void SerializeToStream(object obj, Stream stream) { using (var writer = new XmlTextWriter(stream, null)) { SerializeToWriter(obj, writer); } } /// /// Serializes to file. /// /// The obj. /// The file. public void SerializeToFile(object obj, string file) { using (var stream = new FileStream(file, FileMode.Create)) { SerializeToStream(obj, stream); } } /// /// Deserializes from file. /// /// The type. /// The file. /// System.Object. public object DeserializeFromFile(Type type, string file) { using (var stream = File.OpenRead(file)) { return DeserializeFromStream(type, stream); } } /// /// Deserializes from bytes. /// /// The type. /// The buffer. /// System.Object. public object DeserializeFromBytes(Type type, byte[] buffer) { using (var stream = new MemoryStream(buffer)) { return DeserializeFromStream(type, stream); } } } }