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/Serialization/ProtobufSerializer.cs

44 lines
1.4 KiB

using System;
using System.IO;
namespace MediaBrowser.Common.Serialization
{
/// <summary>
/// Protocol buffers is google's binary serialization format. This is a .NET implementation of it.
/// You have to tag your classes with some annoying attributes, but in return you get the fastest serialization around with the smallest possible output.
/// </summary>
public static class ProtobufSerializer
{
public static void SerializeToStream<T>(T obj, Stream stream)
{
ProtoBuf.Serializer.Serialize<T>(stream, obj);
}
public static T DeserializeFromStream<T>(Stream stream)
{
return ProtoBuf.Serializer.Deserialize<T>(stream);
}
public static object DeserializeFromStream(Stream stream, Type type)
{
throw new NotImplementedException();
}
public static void SerializeToFile<T>(T obj, string file)
{
using (Stream stream = File.Open(file, FileMode.Create))
{
SerializeToStream<T>(obj, stream);
}
}
public static T DeserializeFromFile<T>(string file)
{
using (Stream stream = File.OpenRead(file))
{
return DeserializeFromStream<T>(stream);
}
}
}
}