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

115 lines
3.7 KiB
C#
Raw Normal View History

2013-02-24 21:53:54 +00:00
using MediaBrowser.Model.Serialization;
2013-02-21 01:33:05 +00:00
using System;
2014-12-21 18:58:17 +00:00
using System.Collections.Concurrent;
2013-02-21 01:33:05 +00:00
using System.IO;
using System.Xml;
2015-10-04 04:23:11 +00:00
using CommonIO;
2013-02-21 01:33:05 +00:00
2013-02-24 21:53:54 +00:00
namespace MediaBrowser.Common.Implementations.Serialization
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Provides a wrapper around third party xml serialization.
/// </summary>
2013-02-24 21:53:54 +00:00
public class XmlSerializer : IXmlSerializer
2013-02-21 01:33:05 +00:00
{
2015-09-13 21:32:02 +00:00
private IFileSystem _fileSystem;
public XmlSerializer(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
2014-12-21 18:58:17 +00: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-21 01:33:05 +00:00
/// <summary>
/// Serializes to writer.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="writer">The writer.</param>
2013-02-24 21:53:54 +00:00
private void SerializeToWriter(object obj, XmlTextWriter writer)
2013-02-21 01:33:05 +00:00
{
writer.Formatting = Formatting.Indented;
2014-12-21 18:58:17 +00:00
var netSerializer = GetSerializer(obj.GetType());
2013-02-21 01:33:05 +00: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 21:53:54 +00:00
public object DeserializeFromStream(Type type, Stream stream)
2013-02-21 01:33:05 +00:00
{
using (var reader = new XmlTextReader(stream))
{
2014-12-21 18:58:17 +00:00
var netSerializer = GetSerializer(type);
2013-02-21 01:33:05 +00: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 21:53:54 +00:00
public void SerializeToStream(object obj, Stream stream)
2013-02-21 01:33:05 +00: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 21:53:54 +00:00
public void SerializeToFile(object obj, string file)
2013-02-21 01:33:05 +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>
2013-02-24 21:53:54 +00:00
public object DeserializeFromFile(Type type, string file)
2013-02-21 01:33:05 +00:00
{
2015-09-13 21:32:02 +00:00
using (var stream = _fileSystem.OpenRead(file))
2013-02-21 01:33:05 +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>
2013-02-24 21:53:54 +00:00
public object DeserializeFromBytes(Type type, byte[] buffer)
2013-02-21 01:33:05 +00:00
{
using (var stream = new MemoryStream(buffer))
{
return DeserializeFromStream(type, stream);
}
}
}
}