using System; using System.Collections.Concurrent; using System.IO; using System.Xml; using System.Xml.Serialization; using MediaBrowser.Model.IO; using MediaBrowser.Model.Serialization; namespace Emby.Server.Implementations.Serialization { /// /// Provides a wrapper around third party xml serialization. /// public class MyXmlSerializer : IXmlSerializer { // Need to cache these // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html private static readonly ConcurrentDictionary _serializers = new ConcurrentDictionary(); private static XmlSerializer GetSerializer(Type type) => _serializers.GetOrAdd( type.FullName ?? throw new ArgumentException($"Invalid type {type}."), (_, t) => new XmlSerializer(t), type); /// /// 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) { using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true)) using (var textWriter = new XmlTextWriter(writer)) { textWriter.Formatting = Formatting.Indented; SerializeToWriter(obj, textWriter); } } /// /// 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, 0, buffer.Length, false, true)) { return DeserializeFromStream(type, stream); } } } }