From 767cdc1f6f6a63ce997fc9476911e2c361f9d402 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Wed, 20 Feb 2013 20:33:05 -0500 Subject: Pushing missing changes --- .../Serialization/DynamicProtobufSerializer.cs | 160 +++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs (limited to 'MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs') diff --git a/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs b/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs new file mode 100644 index 000000000..f83b31322 --- /dev/null +++ b/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs @@ -0,0 +1,160 @@ +using MediaBrowser.Common.Mef; +using ProtoBuf; +using ProtoBuf.Meta; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace MediaBrowser.Common.Serialization +{ + /// + /// Creates a compiled protobuf serializer based on a set of assemblies + /// + public class DynamicProtobufSerializer + { + /// + /// Gets or sets the type model. + /// + /// The type model. + public TypeModel TypeModel { get; set; } + + /// + /// Serializes to stream. + /// + /// The obj. + /// The stream. + /// obj + public void SerializeToStream(object obj, Stream stream) + { + if (obj == null) + { + throw new ArgumentNullException("obj"); + } + if (stream == null) + { + throw new ArgumentNullException("stream"); + } + + TypeModel.Serialize(stream, obj); + } + + /// + /// Deserializes from stream. + /// + /// The stream. + /// The type. + /// System.Object. + /// stream + public object DeserializeFromStream(Stream stream, Type type) + { + if (stream == null) + { + throw new ArgumentNullException("stream"); + } + + return TypeModel.Deserialize(stream, null, type); + } + + /// + /// Deserializes from stream. + /// + /// + /// The stream. + /// ``0. + public T DeserializeFromStream(Stream stream) + where T : class + { + return DeserializeFromStream(stream, typeof(T)) as T; + } + + /// + /// Serializes to file. + /// + /// + /// The obj. + /// The file. + /// file + public void SerializeToFile(T obj, string file) + { + if (string.IsNullOrEmpty(file)) + { + throw new ArgumentNullException("file"); + } + + using (Stream stream = File.Open(file, FileMode.Create)) + { + SerializeToStream(obj, stream); + } + } + + /// + /// Deserializes from file. + /// + /// + /// The file. + /// ``0. + /// file + public T DeserializeFromFile(string file) + where T : class + { + if (string.IsNullOrEmpty(file)) + { + throw new ArgumentNullException("file"); + } + + using (Stream stream = File.OpenRead(file)) + { + return DeserializeFromStream(stream); + } + } + + /// + /// Serializes to bytes. + /// + /// + /// The obj. + /// System.Byte[][]. + /// obj + public byte[] SerializeToBytes(T obj) + where T : class + { + if (obj == null) + { + throw new ArgumentNullException("obj"); + } + + using (var stream = new MemoryStream()) + { + SerializeToStream(obj, stream); + return stream.ToArray(); + } + } + + /// + /// Creates the specified assemblies. + /// + /// The assemblies. + /// DynamicProtobufSerializer. + /// assemblies + public static DynamicProtobufSerializer Create(IEnumerable assemblies) + { + if (assemblies == null) + { + throw new ArgumentNullException("assemblies"); + } + + var model = TypeModel.Create(); + var attributeType = typeof(ProtoContractAttribute); + + // Find all ProtoContracts in the current assembly + foreach (var type in assemblies.SelectMany(a => MefUtils.GetTypes(a).Where(t => Attribute.IsDefined(t, attributeType)))) + { + model.Add(type, true); + } + + return new DynamicProtobufSerializer { TypeModel = model.Compile() }; + } + } +} -- cgit v1.2.3