2012-09-03 09:54:20 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-08-13 07:51:42 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Serialization
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a wrapper around third party xml serialization.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class XmlSerializer
|
|
|
|
|
{
|
|
|
|
|
public static void SerializeToStream<T>(T obj, Stream stream)
|
|
|
|
|
{
|
2012-09-03 09:54:20 -07:00
|
|
|
|
GetSerializer(typeof(T)).Serialize(stream, obj);
|
2012-08-13 07:51:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SerializeToFile<T>(T obj, string file)
|
|
|
|
|
{
|
2012-08-18 13:38:02 -07:00
|
|
|
|
using (FileStream stream = new FileStream(file, FileMode.Create))
|
2012-08-13 07:51:42 -07:00
|
|
|
|
{
|
2012-09-03 09:54:20 -07:00
|
|
|
|
GetSerializer(typeof(T)).Serialize(stream, obj);
|
2012-08-13 07:51:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromFile<T>(string file)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
2012-09-03 09:54:20 -07:00
|
|
|
|
return (T)GetSerializer(typeof(T)).Deserialize(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object DeserializeFromFile(Type type, string file)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
|
|
|
|
return GetSerializer(type).Deserialize(stream);
|
2012-08-13 07:51:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromStream<T>(Stream stream)
|
|
|
|
|
{
|
2012-09-03 09:54:20 -07:00
|
|
|
|
return (T)GetSerializer(typeof(T)).Deserialize(stream);
|
2012-08-13 07:51:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 09:54:20 -07:00
|
|
|
|
private static System.Xml.Serialization.XmlSerializer GetSerializer(Type type)
|
2012-08-13 07:51:42 -07:00
|
|
|
|
{
|
2012-09-03 09:54:20 -07:00
|
|
|
|
return new System.Xml.Serialization.XmlSerializer(type);
|
2012-08-13 07:51:42 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|