| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Xml; |
| | | 5 | | using System.Xml.Serialization; |
| | | 6 | | using MediaBrowser.Model.IO; |
| | | 7 | | using MediaBrowser.Model.Serialization; |
| | | 8 | | |
| | | 9 | | namespace Emby.Server.Implementations.Serialization |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides a wrapper around third party xml serialization. |
| | | 13 | | /// </summary> |
| | | 14 | | public class MyXmlSerializer : IXmlSerializer |
| | | 15 | | { |
| | | 16 | | // Need to cache these |
| | | 17 | | // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html |
| | 63 | 18 | | private readonly ConcurrentDictionary<string, XmlSerializer> _serializers = new(); |
| | | 19 | | |
| | | 20 | | private XmlSerializer GetSerializer(Type type) |
| | 275 | 21 | | => _serializers.GetOrAdd( |
| | 275 | 22 | | type.FullName ?? throw new ArgumentException($"Invalid type {type}."), |
| | 275 | 23 | | static (_, t) => new XmlSerializer(t), |
| | 275 | 24 | | type); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Serializes to writer. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="obj">The obj.</param> |
| | | 30 | | /// <param name="writer">The writer.</param> |
| | | 31 | | private void SerializeToWriter(object obj, XmlWriter writer) |
| | | 32 | | { |
| | 210 | 33 | | var netSerializer = GetSerializer(obj.GetType()); |
| | 210 | 34 | | netSerializer.Serialize(writer, obj); |
| | 210 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Deserializes from stream. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="type">The type.</param> |
| | | 41 | | /// <param name="stream">The stream.</param> |
| | | 42 | | /// <returns>System.Object.</returns> |
| | | 43 | | public object? DeserializeFromStream(Type type, Stream stream) |
| | | 44 | | { |
| | 65 | 45 | | using (var reader = XmlReader.Create(stream)) |
| | | 46 | | { |
| | 65 | 47 | | var netSerializer = GetSerializer(type); |
| | 65 | 48 | | return netSerializer.Deserialize(reader); |
| | | 49 | | } |
| | 65 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Serializes to stream. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="obj">The obj.</param> |
| | | 56 | | /// <param name="stream">The stream.</param> |
| | | 57 | | public void SerializeToStream(object obj, Stream stream) |
| | | 58 | | { |
| | 210 | 59 | | using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true)) |
| | 210 | 60 | | using (var textWriter = new XmlTextWriter(writer)) |
| | | 61 | | { |
| | 210 | 62 | | textWriter.Formatting = Formatting.Indented; |
| | 210 | 63 | | SerializeToWriter(obj, textWriter); |
| | 210 | 64 | | } |
| | 210 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Serializes to file. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="obj">The obj.</param> |
| | | 71 | | /// <param name="file">The file.</param> |
| | | 72 | | public void SerializeToFile(object obj, string file) |
| | | 73 | | { |
| | 168 | 74 | | using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write)) |
| | | 75 | | { |
| | 168 | 76 | | SerializeToStream(obj, stream); |
| | 168 | 77 | | } |
| | 168 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Deserializes from file. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="type">The type.</param> |
| | | 84 | | /// <param name="file">The file.</param> |
| | | 85 | | /// <returns>System.Object.</returns> |
| | | 86 | | public object? DeserializeFromFile(Type type, string file) |
| | | 87 | | { |
| | 86 | 88 | | using (var stream = File.OpenRead(file)) |
| | | 89 | | { |
| | 44 | 90 | | return DeserializeFromStream(type, stream); |
| | | 91 | | } |
| | 44 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Deserializes from bytes. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="type">The type.</param> |
| | | 98 | | /// <param name="buffer">The buffer.</param> |
| | | 99 | | /// <returns>System.Object.</returns> |
| | | 100 | | public object? DeserializeFromBytes(Type type, byte[] buffer) |
| | | 101 | | { |
| | 21 | 102 | | using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true)) |
| | | 103 | | { |
| | 21 | 104 | | return DeserializeFromStream(type, stream); |
| | | 105 | | } |
| | 21 | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |