DynamicProtobufSerializer.cs 5.1 KB

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