ProtobufSerializer.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.IO;
  2. namespace MediaBrowser.Common.Serialization
  3. {
  4. /// <summary>
  5. /// Protocol buffers is google's binary serialization format. This is a .NET implementation of it.
  6. /// You have to tag your classes with some annoying attributes, but in return you get the fastest serialization around with the smallest possible output.
  7. /// </summary>
  8. public static class ProtobufSerializer
  9. {
  10. public static void SerializeToStream<T>(T obj, Stream stream)
  11. {
  12. ProtoBuf.Serializer.Serialize<T>(stream, obj);
  13. }
  14. public static T DeserializeFromStream<T>(Stream stream)
  15. {
  16. return ProtoBuf.Serializer.Deserialize<T>(stream);
  17. }
  18. public static void SerializeToFile<T>(T obj, string file)
  19. {
  20. using (Stream stream = File.Open(file, FileMode.Create))
  21. {
  22. SerializeToStream<T>(obj, stream);
  23. }
  24. }
  25. public static T DeserializeFromFile<T>(string file)
  26. {
  27. using (Stream stream = File.OpenRead(file))
  28. {
  29. return DeserializeFromStream<T>(stream);
  30. }
  31. }
  32. }
  33. }