Added a precompiled protobuf serializer assembly

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 12 years ago
parent 50f88997ba
commit e6d689bdd3

@ -1,17 +1,26 @@
using System; using Newtonsoft.Json;
using System;
using System.IO; using System.IO;
using Newtonsoft.Json;
using ProtoBuf;
namespace MediaBrowser.ApiInteraction namespace MediaBrowser.ApiInteraction
{ {
public static class DataSerializer public static class DataSerializer
{ {
/// <summary>
/// This is an auto-generated Protobuf Serialization assembly for best performance.
/// It is created during the Model project's post-build event.
/// This means that this class can currently only handle types within the Model project.
/// If we need to, we can always add a param indicating whether or not the model serializer should be used.
/// </summary>
private static ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format) public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
where T : class
{ {
if (format == ApiInteraction.SerializationFormats.Protobuf) if (format == ApiInteraction.SerializationFormats.Protobuf)
{ {
return Serializer.Deserialize<T>(stream); //return Serializer.Deserialize<T>(stream);
return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
} }
else if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {
@ -35,7 +44,8 @@ namespace MediaBrowser.ApiInteraction
{ {
if (format == ApiInteraction.SerializationFormats.Protobuf) if (format == ApiInteraction.SerializationFormats.Protobuf)
{ {
throw new NotImplementedException(); //throw new NotImplementedException();
return ProtobufModelSerializer.Deserialize(stream, null, type);
} }
else if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {

@ -59,6 +59,9 @@
<Reference Include="protobuf-net"> <Reference Include="protobuf-net">
<HintPath>..\protobuf-net\Full\portable\protobuf-net.dll</HintPath> <HintPath>..\protobuf-net\Full\portable\protobuf-net.dll</HintPath>
</Reference> </Reference>
<Reference Include="ProtobufModelSerializer">
<HintPath>..\MediaBrowser.Model\bin\ProtobufModelSerializer.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

@ -348,6 +348,7 @@ namespace MediaBrowser.ApiInteraction.Portable
/// Performs a GET request, and deserializes the response stream to an object of Type T /// Performs a GET request, and deserializes the response stream to an object of Type T
/// </summary> /// </summary>
private void GetDataAsync<T>(string url, Action<T> callback) private void GetDataAsync<T>(string url, Action<T> callback)
where T : class
{ {
GetDataAsync<T>(url, callback, SerializationFormat); GetDataAsync<T>(url, callback, SerializationFormat);
} }
@ -356,6 +357,7 @@ namespace MediaBrowser.ApiInteraction.Portable
/// Performs a GET request, and deserializes the response stream to an object of Type T /// Performs a GET request, and deserializes the response stream to an object of Type T
/// </summary> /// </summary>
private void GetDataAsync<T>(string url, Action<T> callback, SerializationFormats serializationFormat) private void GetDataAsync<T>(string url, Action<T> callback, SerializationFormats serializationFormat)
where T : class
{ {
if (url.IndexOf('?') == -1) if (url.IndexOf('?') == -1)
{ {
@ -422,6 +424,7 @@ namespace MediaBrowser.ApiInteraction.Portable
/// Performs a POST request, and deserializes the response stream to an object of Type T /// Performs a POST request, and deserializes the response stream to an object of Type T
/// </summary> /// </summary>
private void PostDataAsync<T>(string url, Dictionary<string, string> formValues, Action<T> callback, SerializationFormats serializationFormat) private void PostDataAsync<T>(string url, Dictionary<string, string> formValues, Action<T> callback, SerializationFormats serializationFormat)
where T : class
{ {
if (url.IndexOf('?') == -1) if (url.IndexOf('?') == -1)
{ {
@ -445,7 +448,7 @@ namespace MediaBrowser.ApiInteraction.Portable
{ {
// Construct the body // Construct the body
string postBody = string.Join("&", formValues.Keys.Select(s => string.Format("{0}={1}", s, formValues[s])).ToArray()); string postBody = string.Join("&", formValues.Keys.Select(s => string.Format("{0}={1}", s, formValues[s])).ToArray());
// Convert the string into a byte array. // Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postBody); byte[] byteArray = Encoding.UTF8.GetBytes(postBody);

@ -51,6 +51,9 @@
<Reference Include="protobuf-net"> <Reference Include="protobuf-net">
<HintPath>..\protobuf-net\Full\portable\protobuf-net.dll</HintPath> <HintPath>..\protobuf-net\Full\portable\protobuf-net.dll</HintPath>
</Reference> </Reference>
<Reference Include="ProtobufModelSerializer">
<HintPath>..\MediaBrowser.Model\bin\ProtobufModelSerializer.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj"> <ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">

@ -350,6 +350,7 @@ namespace MediaBrowser.ApiInteraction
} }
protected T DeserializeFromStream<T>(Stream stream) protected T DeserializeFromStream<T>(Stream stream)
where T : class
{ {
return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat); return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat);
} }

@ -1,17 +1,29 @@
using System; using ServiceStack.Text;
using System;
using System.IO; using System.IO;
using ProtoBuf;
using ServiceStack.Text;
namespace MediaBrowser.ApiInteraction namespace MediaBrowser.ApiInteraction
{ {
public static class DataSerializer public static class DataSerializer
{ {
/// <summary>
/// This is an auto-generated Protobuf Serialization assembly for best performance.
/// It is created during the Model project's post-build event.
/// This means that this class can currently only handle types within the Model project.
/// If we need to, we can always add a param indicating whether or not the model serializer should be used.
/// </summary>
private static ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
/// <summary>
/// Deserializes an object using generics
/// </summary>
public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format) public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
where T : class
{ {
if (format == ApiInteraction.SerializationFormats.Protobuf) if (format == ApiInteraction.SerializationFormats.Protobuf)
{ {
return Serializer.Deserialize<T>(stream); //return Serializer.Deserialize<T>(stream);
return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
} }
else if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {
@ -25,11 +37,15 @@ namespace MediaBrowser.ApiInteraction
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <summary>
/// Deserializes an object
/// </summary>
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type) public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
{ {
if (format == ApiInteraction.SerializationFormats.Protobuf) if (format == ApiInteraction.SerializationFormats.Protobuf)
{ {
throw new NotImplementedException(); //throw new NotImplementedException();
return ProtobufModelSerializer.Deserialize(stream, null, type);
} }
else if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {

@ -33,6 +33,9 @@
<Reference Include="protobuf-net"> <Reference Include="protobuf-net">
<HintPath>..\protobuf-net\Full\net30\protobuf-net.dll</HintPath> <HintPath>..\protobuf-net\Full\net30\protobuf-net.dll</HintPath>
</Reference> </Reference>
<Reference Include="ProtobufModelSerializer">
<HintPath>..\MediaBrowser.Model\bin\ProtobufModelSerializer.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text"> <Reference Include="ServiceStack.Text">
<HintPath>..\packages\ServiceStack.Text.3.9.5\lib\net35\ServiceStack.Text.dll</HintPath> <HintPath>..\packages\ServiceStack.Text.3.9.5\lib\net35\ServiceStack.Text.dll</HintPath>
</Reference> </Reference>

@ -42,6 +42,9 @@
<Reference Include="protobuf-net"> <Reference Include="protobuf-net">
<HintPath>..\protobuf-net\Full\net30\protobuf-net.dll</HintPath> <HintPath>..\protobuf-net\Full\net30\protobuf-net.dll</HintPath>
</Reference> </Reference>
<Reference Include="ProtobufModelSerializer">
<HintPath>..\MediaBrowser.Model\bin\ProtobufModelSerializer.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text, Version=3.9.5.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="ServiceStack.Text, Version=3.9.5.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\ServiceStack.Text.3.9.5\lib\net35\ServiceStack.Text.dll</HintPath> <HintPath>..\packages\ServiceStack.Text.3.9.5\lib\net35\ServiceStack.Text.dll</HintPath>

@ -9,21 +9,33 @@ namespace MediaBrowser.Common.Serialization
/// </summary> /// </summary>
public static class ProtobufSerializer public static class ProtobufSerializer
{ {
/// <summary>
/// This is an auto-generated Protobuf Serialization assembly for best performance.
/// It is created during the Model project's post-build event.
/// This means that this class can currently only handle types within the Model project.
/// If we need to, we can always add a param indicating whether or not the model serializer should be used.
/// </summary>
private static ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
public static void SerializeToStream<T>(T obj, Stream stream) public static void SerializeToStream<T>(T obj, Stream stream)
{ {
ProtoBuf.Serializer.Serialize<T>(stream, obj); //new ProtobufModelSerializer.Serialize(stream, typeof(T));
ProtoBuf.Serializer.Serialize(stream, obj);
} }
public static T DeserializeFromStream<T>(Stream stream) public static T DeserializeFromStream<T>(Stream stream)
where T : class
{ {
return ProtoBuf.Serializer.Deserialize<T>(stream); //return ProtoBuf.Serializer.Deserialize<T>(stream);
return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
} }
public static object DeserializeFromStream(Stream stream, Type type) public static object DeserializeFromStream(Stream stream, Type type)
{ {
throw new NotImplementedException(); //throw new NotImplementedException();
return ProtobufModelSerializer.Deserialize(stream, null, type);
} }
public static void SerializeToFile<T>(T obj, string file) public static void SerializeToFile<T>(T obj, string file)
{ {
using (Stream stream = File.Open(file, FileMode.Create)) using (Stream stream = File.Open(file, FileMode.Create))
@ -33,6 +45,7 @@ namespace MediaBrowser.Common.Serialization
} }
public static T DeserializeFromFile<T>(string file) public static T DeserializeFromFile<T>(string file)
where T : class
{ {
using (Stream stream = File.OpenRead(file)) using (Stream stream = File.OpenRead(file))
{ {

@ -76,6 +76,9 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>"$(ProjectDir)..\protobuf-net\Precompile\precompile.exe" "$(TargetPath)" -o:"$(ProjectDir)bin\ProtobufModelSerializer.dll" -t:ProtobufModelSerializer</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">

Loading…
Cancel
Save