Added support for jsv format output from the api
This commit is contained in:
parent
9338571e03
commit
020c20bd7d
|
@ -83,11 +83,12 @@
|
|||
<Compile Include="Net\CollectionExtensions.cs" />
|
||||
<Compile Include="Net\Handlers\BaseEmbeddedResourceHandler.cs" />
|
||||
<Compile Include="Net\Handlers\BaseHandler.cs" />
|
||||
<Compile Include="Net\Handlers\BaseJsonHandler.cs" />
|
||||
<Compile Include="Net\Handlers\BaseSerializationHandler.cs" />
|
||||
<Compile Include="Net\HttpServer.cs" />
|
||||
<Compile Include="Net\Request.cs" />
|
||||
<Compile Include="Plugins\BasePlugin.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Serialization\JsvSerializer.cs" />
|
||||
<Compile Include="Serialization\XmlSerializer.cs" />
|
||||
<Compile Include="UI\BaseApplication.cs" />
|
||||
<Compile Include="UI\Splash.xaml.cs">
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Net.Handlers
|
||||
{
|
||||
public abstract class BaseJsonHandler<T> : BaseHandler
|
||||
{
|
||||
public override Task<string> GetContentType()
|
||||
{
|
||||
return Task.FromResult<string>(MimeTypes.JsonMimeType);
|
||||
}
|
||||
|
||||
private bool _ObjectToSerializeEnsured = false;
|
||||
private T _ObjectToSerialize;
|
||||
|
||||
private async Task EnsureObjectToSerialize()
|
||||
{
|
||||
if (!_ObjectToSerializeEnsured)
|
||||
{
|
||||
_ObjectToSerialize = await GetObjectToSerialize().ConfigureAwait(false);
|
||||
|
||||
if (_ObjectToSerialize == null)
|
||||
{
|
||||
StatusCode = 404;
|
||||
}
|
||||
|
||||
_ObjectToSerializeEnsured = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Task<T> GetObjectToSerialize();
|
||||
|
||||
protected override Task PrepareResponse()
|
||||
{
|
||||
return EnsureObjectToSerialize();
|
||||
}
|
||||
|
||||
protected async override Task WriteResponseToOutputStream(Stream stream)
|
||||
{
|
||||
await EnsureObjectToSerialize();
|
||||
|
||||
JsonSerializer.SerializeToStream<T>(_ObjectToSerialize, stream);
|
||||
}
|
||||
}
|
||||
}
|
83
MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs
Normal file
83
MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs
Normal file
|
@ -0,0 +1,83 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using System;
|
||||
|
||||
namespace MediaBrowser.Common.Net.Handlers
|
||||
{
|
||||
public abstract class BaseJsonHandler<T> : BaseHandler
|
||||
{
|
||||
public SerializationFormat SerializationFormat
|
||||
{
|
||||
get
|
||||
{
|
||||
string format = QueryString["dataformat"];
|
||||
|
||||
if (string.IsNullOrEmpty(format))
|
||||
{
|
||||
return Handlers.SerializationFormat.Json;
|
||||
}
|
||||
|
||||
return (SerializationFormat)Enum.Parse(typeof(SerializationFormat), format, true);
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<string> GetContentType()
|
||||
{
|
||||
switch (SerializationFormat)
|
||||
{
|
||||
case Handlers.SerializationFormat.Jsv:
|
||||
return Task.FromResult<string>("text/plain");
|
||||
default:
|
||||
return Task.FromResult<string>(MimeTypes.JsonMimeType);
|
||||
}
|
||||
}
|
||||
|
||||
private bool _ObjectToSerializeEnsured = false;
|
||||
private T _ObjectToSerialize;
|
||||
|
||||
private async Task EnsureObjectToSerialize()
|
||||
{
|
||||
if (!_ObjectToSerializeEnsured)
|
||||
{
|
||||
_ObjectToSerialize = await GetObjectToSerialize().ConfigureAwait(false);
|
||||
|
||||
if (_ObjectToSerialize == null)
|
||||
{
|
||||
StatusCode = 404;
|
||||
}
|
||||
|
||||
_ObjectToSerializeEnsured = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Task<T> GetObjectToSerialize();
|
||||
|
||||
protected override Task PrepareResponse()
|
||||
{
|
||||
return EnsureObjectToSerialize();
|
||||
}
|
||||
|
||||
protected async override Task WriteResponseToOutputStream(Stream stream)
|
||||
{
|
||||
await EnsureObjectToSerialize();
|
||||
|
||||
switch (SerializationFormat)
|
||||
{
|
||||
case Handlers.SerializationFormat.Jsv:
|
||||
JsvSerializer.SerializeToStream<T>(_ObjectToSerialize, stream);
|
||||
break;
|
||||
default:
|
||||
JsonSerializer.SerializeToStream<T>(_ObjectToSerialize, stream);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum SerializationFormat
|
||||
{
|
||||
Json,
|
||||
Jsv
|
||||
}
|
||||
|
||||
}
|
22
MediaBrowser.Common/Serialization/JsvSerializer.cs
Normal file
22
MediaBrowser.Common/Serialization/JsvSerializer.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System.IO;
|
||||
|
||||
namespace MediaBrowser.Common.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// This adds support for ServiceStack's proprietary JSV output format.
|
||||
/// It's based on Json but the serializer performs faster and output runs about 10% smaller
|
||||
/// http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.100000-times.2010-08-17.html
|
||||
/// </summary>
|
||||
public static class JsvSerializer
|
||||
{
|
||||
public static void SerializeToStream<T>(T obj, Stream stream)
|
||||
{
|
||||
ServiceStack.Text.TypeSerializer.SerializeToStream<T>(obj, stream);
|
||||
}
|
||||
|
||||
public static T DeserializeFromStream<T>(Stream stream)
|
||||
{
|
||||
return ServiceStack.Text.TypeSerializer.DeserializeFromStream<T>(stream);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user