You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
105 lines
3.4 KiB
6 years ago
|
using System;
|
||
5 years ago
|
using System.Collections.Concurrent;
|
||
8 years ago
|
using System.IO;
|
||
|
using System.Xml;
|
||
8 years ago
|
using System.Xml.Serialization;
|
||
7 years ago
|
using MediaBrowser.Model.Serialization;
|
||
8 years ago
|
|
||
7 years ago
|
namespace Emby.Server.Implementations.Serialization
|
||
8 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Provides a wrapper around third party xml serialization.
|
||
|
/// </summary>
|
||
8 years ago
|
public class MyXmlSerializer : IXmlSerializer
|
||
8 years ago
|
{
|
||
|
// Need to cache these
|
||
|
// http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html
|
||
5 years ago
|
private static readonly ConcurrentDictionary<string, XmlSerializer> _serializers =
|
||
|
new ConcurrentDictionary<string, XmlSerializer>();
|
||
8 years ago
|
|
||
5 years ago
|
private static XmlSerializer GetSerializer(Type type)
|
||
|
=> _serializers.GetOrAdd(type.FullName, _ => new XmlSerializer(type));
|
||
8 years ago
|
|
||
|
/// <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>
|
||
|
public object DeserializeFromStream(Type type, Stream stream)
|
||
|
{
|
||
|
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)
|
||
|
{
|
||
8 years ago
|
using (var writer = new XmlTextWriter(stream, null))
|
||
8 years ago
|
{
|
||
8 years ago
|
writer.Formatting = Formatting.Indented;
|
||
8 years ago
|
SerializeToWriter(obj, writer);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <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>
|
||
|
public object DeserializeFromFile(Type type, string file)
|
||
|
{
|
||
6 years ago
|
using (var stream = File.OpenRead(file))
|
||
8 years ago
|
{
|
||
|
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>
|
||
|
public object DeserializeFromBytes(Type type, byte[] buffer)
|
||
|
{
|
||
|
using (var stream = new MemoryStream(buffer))
|
||
|
{
|
||
|
return DeserializeFromStream(type, stream);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|