2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2019-09-25 11:24:39 +00:00
|
|
|
using System.Collections.Concurrent;
|
2016-10-29 05:40:15 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
2016-11-04 19:51:59 +00:00
|
|
|
using System.Xml.Serialization;
|
2020-08-07 17:48:52 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2017-08-16 06:43:41 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2017-08-16 06:43:41 +00:00
|
|
|
namespace Emby.Server.Implementations.Serialization
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Provides a wrapper around third party xml serialization.
|
|
|
|
/// </summary>
|
2016-11-04 19:51:59 +00:00
|
|
|
public class MyXmlSerializer : IXmlSerializer
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
// Need to cache these
|
|
|
|
// http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html
|
2019-09-25 11:24:39 +00:00
|
|
|
private static readonly ConcurrentDictionary<string, XmlSerializer> _serializers =
|
|
|
|
new ConcurrentDictionary<string, XmlSerializer>();
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2019-09-25 11:24:39 +00:00
|
|
|
private static XmlSerializer GetSerializer(Type type)
|
2021-05-20 19:28:18 +00:00
|
|
|
=> _serializers.GetOrAdd(
|
|
|
|
type.FullName ?? throw new ArgumentException($"Invalid type {type}."),
|
2021-05-23 22:30:41 +00:00
|
|
|
(_, t) => new XmlSerializer(t),
|
|
|
|
type);
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Serializes to writer.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <param name="writer">The writer.</param>
|
|
|
|
private void SerializeToWriter(object obj, XmlWriter writer)
|
|
|
|
{
|
|
|
|
var netSerializer = GetSerializer(obj.GetType());
|
|
|
|
netSerializer.Serialize(writer, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2021-05-20 19:28:18 +00:00
|
|
|
public object? DeserializeFromStream(Type type, Stream stream)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
using (var reader = XmlReader.Create(stream))
|
|
|
|
{
|
|
|
|
var netSerializer = GetSerializer(type);
|
|
|
|
return netSerializer.Deserialize(reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Serializes to stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
public void SerializeToStream(object obj, Stream stream)
|
|
|
|
{
|
2020-08-07 17:48:52 +00:00
|
|
|
using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true))
|
|
|
|
using (var textWriter = new XmlTextWriter(writer))
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
2020-08-07 17:48:52 +00:00
|
|
|
textWriter.Formatting = Formatting.Indented;
|
|
|
|
SerializeToWriter(obj, textWriter);
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Serializes to file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
public void SerializeToFile(object obj, string file)
|
|
|
|
{
|
|
|
|
using (var stream = new FileStream(file, FileMode.Create))
|
|
|
|
{
|
|
|
|
SerializeToStream(obj, stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2021-05-20 19:28:18 +00:00
|
|
|
public object? DeserializeFromFile(Type type, string file)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
2019-01-26 21:31:59 +00:00
|
|
|
using (var stream = File.OpenRead(file))
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
return DeserializeFromStream(type, stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from bytes.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <param name="buffer">The buffer.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2021-05-20 19:28:18 +00:00
|
|
|
public object? DeserializeFromBytes(Type type, byte[] buffer)
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
2020-08-07 15:38:01 +00:00
|
|
|
using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true))
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
return DeserializeFromStream(type, stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|