jellyfin/Emby.Server.Implementations/Serialization/XmlSerializer.cs
William Taylor 65cd3ed597 Replaced injections of ILogger with ILoggerFactory
This makes resolving dependencies from the container much easier as
you cannot resolve with primitives parameters in a way that is any
more readable.

The aim of this commit is to change as little as possible with the end
result, loggers that were newed up for the parent object were given the same
name. Objects that used the base or app loggers, were given a new logger with
an appropriate name.

Also removed some unused dependencies.
2019-01-20 21:05:12 +00:00

131 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Serialization
{
/// <summary>
/// Provides a wrapper around third party xml serialization.
/// </summary>
public class MyXmlSerializer : IXmlSerializer
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
public MyXmlSerializer(
IFileSystem fileSystem,
ILoggerFactory loggerFactory)
{
_fileSystem = fileSystem;
_logger = loggerFactory.CreateLogger("XmlSerializer");
}
// Need to cache these
// http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html
private readonly Dictionary<string, XmlSerializer> _serializers =
new Dictionary<string, XmlSerializer>();
private XmlSerializer GetSerializer(Type type)
{
var key = type.FullName;
lock (_serializers)
{
if (!_serializers.TryGetValue(key, out var serializer))
{
serializer = new XmlSerializer(type);
_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)
{
using (var writer = new XmlTextWriter(stream, null))
{
writer.Formatting = Formatting.Indented;
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);
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);
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);
}
}
}
}