ApiInteraction tweaks

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 12 years ago
parent a588e0461b
commit 99a679d328

@ -45,6 +45,9 @@
<Compile Include="..\MediaBrowser.ApiInteraction\DataSerializer.cs">
<Link>DataSerializer.cs</Link>
</Compile>
<Compile Include="..\MediaBrowser.ApiInteraction\SerializationFormats.cs">
<Link>SerializationFormats.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

@ -45,11 +45,11 @@ namespace MediaBrowser.ApiInteraction
/// <summary>
/// Gets the data format to request from the server
/// </summary>
private SerializationFormat SerializationFormat
private SerializationFormats SerializationFormat
{
get
{
return ApiInteraction.SerializationFormat.Protobuf;
return ApiInteraction.SerializationFormats.Protobuf;
}
}
@ -585,11 +585,11 @@ namespace MediaBrowser.ApiInteraction
string url = ApiUrl + "/ServerConfiguration";
// At the moment this can't be retrieved in protobuf format
SerializationFormat format = SerializationFormat.Jsv;
SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
{
return DeserializeFromStream<ServerConfiguration>(stream, format);
return DataSerializer.DeserializeFromStream<ServerConfiguration>(stream, format);
}
}
@ -601,11 +601,11 @@ namespace MediaBrowser.ApiInteraction
string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
// At the moment this can't be retrieved in protobuf format
SerializationFormat format = SerializationFormat.Jsv;
SerializationFormats format = DataSerializer.CanDeSerializeJsv ? SerializationFormats.Jsv : SerializationFormats.Json;
using (Stream stream = await GetSerializedStreamAsync(url, format).ConfigureAwait(false))
{
return DeserializeFromStream(stream, format, configurationType);
return DataSerializer.DeserializeFromStream(stream, format, configurationType);
}
}
@ -659,7 +659,7 @@ namespace MediaBrowser.ApiInteraction
/// <summary>
/// This is a helper around getting a stream from the server that contains serialized data
/// </summary>
private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormat serializationFormat)
private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
{
if (url.IndexOf('?') == -1)
{
@ -675,35 +675,7 @@ namespace MediaBrowser.ApiInteraction
private T DeserializeFromStream<T>(Stream stream)
{
return DeserializeFromStream<T>(stream, SerializationFormat);
}
private T DeserializeFromStream<T>(Stream stream, SerializationFormat format)
{
if (format == ApiInteraction.SerializationFormat.Protobuf)
{
return DataSerializer.DeserializeProtobufFromStream<T>(stream);
}
if (format == ApiInteraction.SerializationFormat.Jsv)
{
return DataSerializer.DeserializeJsvFromStream<T>(stream);
}
return DataSerializer.DeserializeJsonFromStream<T>(stream);
}
private object DeserializeFromStream(Stream stream, SerializationFormat format, Type type)
{
if (format == ApiInteraction.SerializationFormat.Protobuf)
{
throw new NotImplementedException();
}
if (format == ApiInteraction.SerializationFormat.Jsv)
{
return DataSerializer.DeserializeJsvFromStream(stream, type);
}
return DataSerializer.DeserializeJsonFromStream(stream, type);
return DataSerializer.DeserializeFromStream<T>(stream, SerializationFormat);
}
/// <summary>

@ -7,30 +7,33 @@ namespace MediaBrowser.ApiInteraction
{
public static class DataSerializer
{
public static T DeserializeJsonFromStream<T>(Stream stream)
public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
{
return JsonSerializer.DeserializeFromStream<T>(stream);
}
if (format == ApiInteraction.SerializationFormats.Protobuf)
{
return Serializer.Deserialize<T>(stream);
}
if (format == ApiInteraction.SerializationFormats.Jsv)
{
return TypeSerializer.DeserializeFromStream<T>(stream);
}
public static T DeserializeJsvFromStream<T>(Stream stream)
{
return TypeSerializer.DeserializeFromStream<T>(stream);
return JsonSerializer.DeserializeFromStream<T>(stream);
}
public static object DeserializeJsvFromStream(Stream stream, Type type)
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
{
return TypeSerializer.DeserializeFromStream(type, stream);
}
if (format == ApiInteraction.SerializationFormats.Protobuf)
{
throw new NotImplementedException();
}
if (format == ApiInteraction.SerializationFormats.Jsv)
{
return TypeSerializer.DeserializeFromStream(type, stream);
}
public static object DeserializeJsonFromStream(Stream stream, Type type)
{
return JsonSerializer.DeserializeFromStream(type, stream);
}
public static T DeserializeProtobufFromStream<T>(Stream stream)
{
return Serializer.Deserialize<T>(stream);
}
public static void Configure()
{
@ -38,12 +41,13 @@ namespace MediaBrowser.ApiInteraction
JsConfig.ExcludeTypeInfo = true;
JsConfig.IncludeNullValues = false;
}
}
public enum SerializationFormat
{
Json,
Jsv,
Protobuf
public static bool CanDeSerializeJsv
{
get
{
return true;
}
}
}
}

@ -49,6 +49,7 @@
<Compile Include="ApiClient.cs" />
<Compile Include="DataSerializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerializationFormats.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj">

@ -0,0 +1,10 @@

namespace MediaBrowser.ApiInteraction
{
public enum SerializationFormats
{
Json,
Jsv,
Protobuf
}
}
Loading…
Cancel
Save