ProtobufSerializer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using MediaBrowser.Model.Serialization;
  2. using ProtoBuf;
  3. using ProtoBuf.Meta;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. namespace MediaBrowser.Common.Implementations.Serialization
  9. {
  10. /// <summary>
  11. /// Creates a compiled protobuf serializer based on a set of assemblies
  12. /// </summary>
  13. public class ProtobufSerializer : IProtobufSerializer
  14. {
  15. /// <summary>
  16. /// Gets or sets the type model.
  17. /// </summary>
  18. /// <value>The type model.</value>
  19. private TypeModel TypeModel { get; set; }
  20. /// <summary>
  21. /// Serializes to stream.
  22. /// </summary>
  23. /// <param name="obj">The obj.</param>
  24. /// <param name="stream">The stream.</param>
  25. /// <exception cref="System.ArgumentNullException">obj</exception>
  26. public void SerializeToStream(object obj, Stream stream)
  27. {
  28. if (obj == null)
  29. {
  30. throw new ArgumentNullException("obj");
  31. }
  32. if (stream == null)
  33. {
  34. throw new ArgumentNullException("stream");
  35. }
  36. TypeModel.Serialize(stream, obj);
  37. }
  38. /// <summary>
  39. /// Deserializes from stream.
  40. /// </summary>
  41. /// <param name="stream">The stream.</param>
  42. /// <param name="type">The type.</param>
  43. /// <returns>System.Object.</returns>
  44. /// <exception cref="System.ArgumentNullException">stream</exception>
  45. public object DeserializeFromStream(Stream stream, Type type)
  46. {
  47. if (stream == null)
  48. {
  49. throw new ArgumentNullException("stream");
  50. }
  51. return TypeModel.Deserialize(stream, null, type);
  52. }
  53. /// <summary>
  54. /// Deserializes from stream.
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="stream">The stream.</param>
  58. /// <returns>``0.</returns>
  59. public T DeserializeFromStream<T>(Stream stream)
  60. where T : class
  61. {
  62. return DeserializeFromStream(stream, typeof(T)) as T;
  63. }
  64. /// <summary>
  65. /// Serializes to file.
  66. /// </summary>
  67. /// <typeparam name="T"></typeparam>
  68. /// <param name="obj">The obj.</param>
  69. /// <param name="file">The file.</param>
  70. /// <exception cref="System.ArgumentNullException">file</exception>
  71. public void SerializeToFile<T>(T obj, string file)
  72. {
  73. if (string.IsNullOrEmpty(file))
  74. {
  75. throw new ArgumentNullException("file");
  76. }
  77. using (Stream stream = File.Open(file, FileMode.Create))
  78. {
  79. SerializeToStream(obj, stream);
  80. }
  81. }
  82. /// <summary>
  83. /// Deserializes from file.
  84. /// </summary>
  85. /// <typeparam name="T"></typeparam>
  86. /// <param name="file">The file.</param>
  87. /// <returns>``0.</returns>
  88. /// <exception cref="System.ArgumentNullException">file</exception>
  89. public T DeserializeFromFile<T>(string file)
  90. where T : class
  91. {
  92. if (string.IsNullOrEmpty(file))
  93. {
  94. throw new ArgumentNullException("file");
  95. }
  96. using (Stream stream = File.OpenRead(file))
  97. {
  98. return DeserializeFromStream<T>(stream);
  99. }
  100. }
  101. /// <summary>
  102. /// Serializes to bytes.
  103. /// </summary>
  104. /// <typeparam name="T"></typeparam>
  105. /// <param name="obj">The obj.</param>
  106. /// <returns>System.Byte[][].</returns>
  107. /// <exception cref="System.ArgumentNullException">obj</exception>
  108. public byte[] SerializeToBytes<T>(T obj)
  109. where T : class
  110. {
  111. if (obj == null)
  112. {
  113. throw new ArgumentNullException("obj");
  114. }
  115. using (var stream = new MemoryStream())
  116. {
  117. SerializeToStream(obj, stream);
  118. return stream.ToArray();
  119. }
  120. }
  121. /// <summary>
  122. /// Creates the specified assemblies.
  123. /// </summary>
  124. /// <returns>DynamicProtobufSerializer.</returns>
  125. /// <exception cref="System.ArgumentNullException">assemblies</exception>
  126. public static ProtobufSerializer Create(IEnumerable<Type> types)
  127. {
  128. if (types == null)
  129. {
  130. throw new ArgumentNullException("types");
  131. }
  132. var model = TypeModel.Create();
  133. var attributeType = typeof(ProtoContractAttribute);
  134. // Find all ProtoContracts in the current assembly
  135. foreach (var type in types.Where(t => Attribute.IsDefined(t, attributeType)))
  136. {
  137. model.Add(type, true);
  138. }
  139. return new ProtobufSerializer { TypeModel = model.Compile() };
  140. }
  141. }
  142. }