2012-09-03 18:54:20 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-08-13 16:51:42 +02: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 19:19:48 +02:00
|
|
|
|
ServiceStack.Text.XmlSerializer.SerializeToStream(obj, stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromStream<T>(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
return ServiceStack.Text.XmlSerializer.DeserializeFromStream<T>(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object DeserializeFromStream(Type type, Stream stream)
|
|
|
|
|
{
|
|
|
|
|
return ServiceStack.Text.XmlSerializer.DeserializeFromStream(type, stream);
|
2012-08-13 16:51:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SerializeToFile<T>(T obj, string file)
|
|
|
|
|
{
|
2012-09-11 21:37:14 +02:00
|
|
|
|
using (var stream = new FileStream(file, FileMode.Create))
|
2012-08-13 16:51:42 +02:00
|
|
|
|
{
|
2012-09-11 20:20:12 +02:00
|
|
|
|
SerializeToStream(obj, stream);
|
2012-08-13 16:51:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromFile<T>(string file)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
2012-09-03 19:19:48 +02:00
|
|
|
|
return DeserializeFromStream<T>(stream);
|
2012-09-03 18:54:20 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 23:56:30 +02:00
|
|
|
|
public static void SerializeToFile(object obj, string file)
|
|
|
|
|
{
|
2012-09-11 21:37:14 +02:00
|
|
|
|
using (var stream = new FileStream(file, FileMode.Create))
|
2012-09-03 23:56:30 +02:00
|
|
|
|
{
|
|
|
|
|
ServiceStack.Text.XmlSerializer.SerializeToStream(obj, stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 18:54:20 +02:00
|
|
|
|
public static object DeserializeFromFile(Type type, string file)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
2012-09-03 19:19:48 +02:00
|
|
|
|
return DeserializeFromStream(type, stream);
|
2012-08-13 16:51:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|