jellyfin/MediaBrowser.Common.Implementations/Serialization/ProtobufSerializer.cs

159 lines
5.0 KiB
C#
Raw Normal View History

2013-02-24 14:53:54 -07:00
using MediaBrowser.Model.Serialization;
using ProtoBuf;
2013-02-20 18:33:05 -07:00
using ProtoBuf.Meta;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-02-24 14:53:54 -07:00
namespace MediaBrowser.Common.Implementations.Serialization
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Creates a compiled protobuf serializer based on a set of assemblies
/// </summary>
2013-02-24 14:53:54 -07:00
public class ProtobufSerializer : IProtobufSerializer
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the type model.
/// </summary>
/// <value>The type model.</value>
2013-02-24 14:53:54 -07:00
private TypeModel TypeModel { get; set; }
2013-02-20 18:33:05 -07:00
/// <summary>
/// Serializes to stream.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="stream">The stream.</param>
/// <exception cref="System.ArgumentNullException">obj</exception>
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);
}
/// <summary>
/// Deserializes from stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="type">The type.</param>
/// <returns>System.Object.</returns>
/// <exception cref="System.ArgumentNullException">stream</exception>
public object DeserializeFromStream(Stream stream, Type type)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
return TypeModel.Deserialize(stream, null, type);
}
/// <summary>
/// Deserializes from stream.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <returns>``0.</returns>
public T DeserializeFromStream<T>(Stream stream)
where T : class
{
return DeserializeFromStream(stream, typeof(T)) as T;
}
/// <summary>
/// Serializes to file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj">The obj.</param>
/// <param name="file">The file.</param>
/// <exception cref="System.ArgumentNullException">file</exception>
public void SerializeToFile<T>(T obj, string file)
{
if (string.IsNullOrEmpty(file))
{
throw new ArgumentNullException("file");
}
using (Stream stream = File.Open(file, FileMode.Create))
{
SerializeToStream(obj, stream);
}
}
/// <summary>
/// Deserializes from file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="file">The file.</param>
/// <returns>``0.</returns>
/// <exception cref="System.ArgumentNullException">file</exception>
public T DeserializeFromFile<T>(string file)
where T : class
{
if (string.IsNullOrEmpty(file))
{
throw new ArgumentNullException("file");
}
using (Stream stream = File.OpenRead(file))
{
return DeserializeFromStream<T>(stream);
}
}
/// <summary>
/// Serializes to bytes.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj">The obj.</param>
/// <returns>System.Byte[][].</returns>
/// <exception cref="System.ArgumentNullException">obj</exception>
public byte[] SerializeToBytes<T>(T obj)
where T : class
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
using (var stream = new MemoryStream())
{
SerializeToStream(obj, stream);
return stream.ToArray();
}
}
/// <summary>
/// Creates the specified assemblies.
/// </summary>
/// <returns>DynamicProtobufSerializer.</returns>
/// <exception cref="System.ArgumentNullException">assemblies</exception>
2013-02-24 14:53:54 -07:00
public static ProtobufSerializer Create(IEnumerable<Type> types)
2013-02-20 18:33:05 -07:00
{
if (types == null)
2013-02-20 18:33:05 -07:00
{
throw new ArgumentNullException("types");
2013-02-20 18:33:05 -07:00
}
var model = TypeModel.Create();
var attributeType = typeof(ProtoContractAttribute);
// Find all ProtoContracts in the current assembly
foreach (var type in types.Where(t => Attribute.IsDefined(t, attributeType)))
2013-02-20 18:33:05 -07:00
{
model.Add(type, true);
}
2013-02-24 14:53:54 -07:00
return new ProtobufSerializer { TypeModel = model.Compile() };
2013-02-20 18:33:05 -07:00
}
}
}