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"> <Compile Include="..\MediaBrowser.ApiInteraction\DataSerializer.cs">
<Link>DataSerializer.cs</Link> <Link>DataSerializer.cs</Link>
</Compile> </Compile>
<Compile Include="..\MediaBrowser.ApiInteraction\SerializationFormats.cs">
<Link>SerializationFormats.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -45,11 +45,11 @@ namespace MediaBrowser.ApiInteraction
/// <summary> /// <summary>
/// Gets the data format to request from the server /// Gets the data format to request from the server
/// </summary> /// </summary>
private SerializationFormat SerializationFormat private SerializationFormats SerializationFormat
{ {
get get
{ {
return ApiInteraction.SerializationFormat.Protobuf; return ApiInteraction.SerializationFormats.Protobuf;
} }
} }
@ -585,11 +585,11 @@ namespace MediaBrowser.ApiInteraction
string url = ApiUrl + "/ServerConfiguration"; string url = ApiUrl + "/ServerConfiguration";
// At the moment this can't be retrieved in protobuf format // 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)) 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; string url = ApiUrl + "/PluginConfiguration?assemblyfilename=" + plugin.AssemblyFileName;
// At the moment this can't be retrieved in protobuf format // 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)) 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> /// <summary>
/// This is a helper around getting a stream from the server that contains serialized data /// This is a helper around getting a stream from the server that contains serialized data
/// </summary> /// </summary>
private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormat serializationFormat) private Task<Stream> GetSerializedStreamAsync(string url, SerializationFormats serializationFormat)
{ {
if (url.IndexOf('?') == -1) if (url.IndexOf('?') == -1)
{ {
@ -675,35 +675,7 @@ namespace MediaBrowser.ApiInteraction
private T DeserializeFromStream<T>(Stream stream) private T DeserializeFromStream<T>(Stream stream)
{ {
return DeserializeFromStream<T>(stream, SerializationFormat); return DataSerializer.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);
} }
/// <summary> /// <summary>

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

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

@ -0,0 +1,10 @@

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