ProtobufSerializer.cs 1.4 KB

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