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.
jellyfin/MediaBrowser.Common.Impleme.../Serialization/XmlSerializer.cs

141 lines
4.3 KiB

12 years ago
using MediaBrowser.Model.Serialization;
using System;
using System.IO;
using System.Xml;
12 years ago
namespace MediaBrowser.Common.Implementations.Serialization
{
/// <summary>
/// Provides a wrapper around third party xml serialization.
/// </summary>
12 years ago
public class XmlSerializer : IXmlSerializer
{
/// <summary>
/// Serializes to writer.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="writer">The writer.</param>
12 years ago
private void SerializeToWriter(object obj, XmlTextWriter writer)
{
writer.Formatting = Formatting.Indented;
var netSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
netSerializer.Serialize(writer, obj);
}
/// <summary>
/// Deserializes from stream.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <returns>``0.</returns>
12 years ago
public T DeserializeFromStream<T>(Stream stream)
{
using (var reader = new XmlTextReader(stream))
{
var netSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)netSerializer.Deserialize(reader);
}
}
/// <summary>
/// Deserializes from stream.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="stream">The stream.</param>
/// <returns>System.Object.</returns>
12 years ago
public object DeserializeFromStream(Type type, Stream stream)
{
using (var reader = new XmlTextReader(stream))
{
var netSerializer = new System.Xml.Serialization.XmlSerializer(type);
return netSerializer.Deserialize(reader);
}
}
/// <summary>
/// Serializes to stream.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="stream">The stream.</param>
12 years ago
public void SerializeToStream(object obj, Stream stream)
{
using (var writer = new XmlTextWriter(stream, null))
{
SerializeToWriter(obj, writer);
}
}
/// <summary>
/// Deserializes from file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="file">The file.</param>
/// <returns>``0.</returns>
12 years ago
public T DeserializeFromFile<T>(string file)
{
using (var stream = File.OpenRead(file))
{
return DeserializeFromStream<T>(stream);
}
}
/// <summary>
/// Serializes to file.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="file">The file.</param>
12 years ago
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>
12 years ago
public object DeserializeFromFile(Type type, string file)
{
using (var stream = File.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>
12 years ago
public object DeserializeFromBytes(Type type, byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
return DeserializeFromStream(type, stream);
}
}
/// <summary>
/// Serializes to bytes.
/// </summary>
/// <param name="obj">The obj.</param>
/// <returns>System.Byte[][].</returns>
12 years ago
public byte[] SerializeToBytes(object obj)
{
using (var stream = new MemoryStream())
{
SerializeToStream(obj, stream);
return stream.ToArray();
}
}
}
}