jellyfin/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs

120 lines
3.9 KiB
C#
Raw Normal View History

2013-02-24 14:53:54 -07:00
using MediaBrowser.Model.Serialization;
2013-02-20 18:33:05 -07:00
using System;
2014-12-21 11:58:17 -07:00
using System.Collections.Concurrent;
2013-02-20 18:33:05 -07:00
using System.IO;
using System.Xml;
2015-10-03 21:23:11 -07:00
using CommonIO;
2016-06-29 11:48:26 -07:00
using MediaBrowser.Model.Logging;
2013-02-20 18:33:05 -07:00
2013-02-24 14:53:54 -07:00
namespace MediaBrowser.Common.Implementations.Serialization
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Provides a wrapper around third party xml serialization.
/// </summary>
2013-02-24 14:53:54 -07:00
public class XmlSerializer : IXmlSerializer
2013-02-20 18:33:05 -07:00
{
2016-06-29 11:48:26 -07:00
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
2015-09-13 14:32:02 -07:00
2016-06-29 11:48:26 -07:00
public XmlSerializer(IFileSystem fileSystem, ILogger logger)
{
_fileSystem = fileSystem;
_logger = logger;
}
2015-09-13 14:32:02 -07:00
2014-12-21 11:58:17 -07:00
// Need to cache these
// http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html
private readonly ConcurrentDictionary<string, System.Xml.Serialization.XmlSerializer> _serializers =
new ConcurrentDictionary<string, System.Xml.Serialization.XmlSerializer>();
private System.Xml.Serialization.XmlSerializer GetSerializer(Type type)
{
var key = type.FullName;
return _serializers.GetOrAdd(key, k => new System.Xml.Serialization.XmlSerializer(type));
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Serializes to writer.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="writer">The writer.</param>
2013-02-24 14:53:54 -07:00
private void SerializeToWriter(object obj, XmlTextWriter writer)
2013-02-20 18:33:05 -07:00
{
writer.Formatting = Formatting.Indented;
2014-12-21 11:58:17 -07:00
var netSerializer = GetSerializer(obj.GetType());
2013-02-20 18:33:05 -07:00
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>
2013-02-24 14:53:54 -07:00
public object DeserializeFromStream(Type type, Stream stream)
2013-02-20 18:33:05 -07:00
{
using (var reader = new XmlTextReader(stream))
{
2014-12-21 11:58:17 -07:00
var netSerializer = GetSerializer(type);
2013-02-20 18:33:05 -07:00
return netSerializer.Deserialize(reader);
}
}
/// <summary>
/// Serializes to stream.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="stream">The stream.</param>
2013-02-24 14:53:54 -07:00
public void SerializeToStream(object obj, Stream stream)
2013-02-20 18:33:05 -07:00
{
using (var writer = new XmlTextWriter(stream, null))
{
SerializeToWriter(obj, writer);
}
}
/// <summary>
/// Serializes to file.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="file">The file.</param>
2013-02-24 14:53:54 -07:00
public void SerializeToFile(object obj, string file)
2013-02-20 18:33:05 -07:00
{
2016-06-30 18:06:18 -07:00
_logger.Debug("Serializing to file {0}", file);
2013-02-20 18:33:05 -07: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>
2013-02-24 14:53:54 -07:00
public object DeserializeFromFile(Type type, string file)
2013-02-20 18:33:05 -07:00
{
2016-06-30 18:06:18 -07:00
_logger.Debug("Deserializing file {0}", file);
2015-09-13 14:32:02 -07:00
using (var stream = _fileSystem.OpenRead(file))
2013-02-20 18:33:05 -07: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>
2013-02-24 14:53:54 -07:00
public object DeserializeFromBytes(Type type, byte[] buffer)
2013-02-20 18:33:05 -07:00
{
using (var stream = new MemoryStream(buffer))
{
return DeserializeFromStream(type, stream);
}
}
}
}