2012-09-03 21:56:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-08-22 13:19:18 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Serialization
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This adds support for ServiceStack's proprietary JSV output format.
|
2012-08-23 18:35:44 +00:00
|
|
|
|
/// It's a hybrid of Json and Csv but the serializer performs about 25% faster and output runs about 10% smaller
|
2012-08-22 13:19:18 +00:00
|
|
|
|
/// http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.100000-times.2010-08-17.html
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class JsvSerializer
|
|
|
|
|
{
|
|
|
|
|
public static void SerializeToStream<T>(T obj, Stream stream)
|
|
|
|
|
{
|
|
|
|
|
ServiceStack.Text.TypeSerializer.SerializeToStream<T>(obj, stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromStream<T>(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
return ServiceStack.Text.TypeSerializer.DeserializeFromStream<T>(stream);
|
|
|
|
|
}
|
2012-08-22 17:01:05 +00:00
|
|
|
|
|
2012-09-03 21:56:30 +00:00
|
|
|
|
public static object DeserializeFromStream(Stream stream, Type type)
|
|
|
|
|
{
|
|
|
|
|
return ServiceStack.Text.TypeSerializer.DeserializeFromStream(type, stream);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 17:01:05 +00:00
|
|
|
|
public static void SerializeToFile<T>(T obj, string file)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = File.Open(file, FileMode.Create))
|
|
|
|
|
{
|
2012-09-03 21:56:30 +00:00
|
|
|
|
SerializeToStream<T>(obj, stream);
|
2012-08-22 17:01:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromFile<T>(string file)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
2012-09-03 21:56:30 +00:00
|
|
|
|
return DeserializeFromStream<T>(stream);
|
2012-08-22 17:01:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|