using MediaBrowser.Model.Serialization; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Xml; using MediaBrowser.Common.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; namespace Emby.Common.Implementations.Serialization { /// /// Provides a wrapper around third party xml serialization. /// public class XmlSerializer : IXmlSerializer { private readonly IFileSystem _fileSystem; private readonly ILogger _logger; public XmlSerializer(IFileSystem fileSystem, ILogger logger) { _fileSystem = fileSystem; _logger = logger; } // Need to cache these // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html private readonly Dictionary _serializers = new Dictionary(); private System.Xml.Serialization.XmlSerializer GetSerializer(Type type) { var key = type.FullName; lock (_serializers) { System.Xml.Serialization.XmlSerializer serializer; if (!_serializers.TryGetValue(key, out serializer)) { serializer = new System.Xml.Serialization.XmlSerializer(type); _serializers[key] = serializer; } return serializer; } } /// /// Serializes to writer. /// /// The obj. /// The writer. private void SerializeToWriter(object obj, XmlWriter writer) { 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 = XmlReader.Create(stream)) { var netSerializer = GetSerializer(type); return netSerializer.Deserialize(reader); } } /// /// Serializes to stream. /// /// The obj. /// The stream. public void SerializeToStream(object obj, Stream stream) { #if NET46 using (var writer = new XmlTextWriter(stream, null)) { writer.Formatting = System.Xml.Formatting.Indented; SerializeToWriter(obj, writer); } #else using (var writer = XmlWriter.Create(stream)) { SerializeToWriter(obj, writer); } #endif } /// /// Serializes to file. /// /// The obj. /// The file. public void SerializeToFile(object obj, string file) { _logger.Debug("Serializing to file {0}", 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) { _logger.Debug("Deserializing file {0}", file); using (var stream = _fileSystem.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); } } } }