jellyfin-server/Emby.Server.Implementations/Serialization/XmlSerializer.cs

130 lines
4.1 KiB
C#
Raw Normal View History

using System;
2016-10-29 05:40:15 +00:00
using System.Collections.Generic;
using System.IO;
using System.Xml;
2016-11-04 19:51:59 +00:00
using System.Xml.Serialization;
2016-10-29 05:40:15 +00:00
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
using MediaBrowser.Model.Serialization;
2016-10-29 05:40:15 +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
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
2016-11-04 19:51:59 +00:00
public MyXmlSerializer(IFileSystem fileSystem, ILogger logger)
2016-10-29 05:40:15 +00:00
{
_fileSystem = fileSystem;
_logger = logger;
}
// Need to cache these
// http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html
2016-11-04 19:51:59 +00:00
private readonly Dictionary<string, XmlSerializer> _serializers =
new Dictionary<string, XmlSerializer>();
2016-10-29 05:40:15 +00:00
2016-11-04 19:51:59 +00:00
private XmlSerializer GetSerializer(Type type)
2016-10-29 05:40:15 +00:00
{
var key = type.FullName;
lock (_serializers)
{
2016-11-04 19:51:59 +00:00
XmlSerializer serializer;
2016-10-29 05:40:15 +00:00
if (!_serializers.TryGetValue(key, out serializer))
{
2016-11-04 19:51:59 +00:00
serializer = new XmlSerializer(type);
2016-10-29 05:40:15 +00:00
_serializers[key] = serializer;
}
return serializer;
}
}
/// <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)
{
2017-05-24 19:12:55 +00:00
using (var writer = new XmlTextWriter(stream, null))
2016-10-29 05:40:15 +00:00
{
2016-11-04 19:51:59 +00:00
writer.Formatting = Formatting.Indented;
2016-10-29 05:40:15 +00:00
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)
{
_logger.LogDebug("Serializing to file {0}", file);
2016-10-29 05:40:15 +00:00
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)
{
_logger.LogDebug("Deserializing file {0}", file);
2016-10-29 05:40:15 +00:00
using (var stream = _fileSystem.OpenRead(file))
{
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);
}
}
}
}