diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index 57c817fc2..7c736affb 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -83,11 +83,12 @@ - + + diff --git a/MediaBrowser.Common/Net/Handlers/BaseJsonHandler.cs b/MediaBrowser.Common/Net/Handlers/BaseJsonHandler.cs deleted file mode 100644 index 650853537..000000000 --- a/MediaBrowser.Common/Net/Handlers/BaseJsonHandler.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.IO; -using System.Threading.Tasks; -using MediaBrowser.Common.Serialization; - -namespace MediaBrowser.Common.Net.Handlers -{ - public abstract class BaseJsonHandler : BaseHandler - { - public override Task GetContentType() - { - return Task.FromResult(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 GetObjectToSerialize(); - - protected override Task PrepareResponse() - { - return EnsureObjectToSerialize(); - } - - protected async override Task WriteResponseToOutputStream(Stream stream) - { - await EnsureObjectToSerialize(); - - JsonSerializer.SerializeToStream(_ObjectToSerialize, stream); - } - } -} diff --git a/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs b/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs new file mode 100644 index 000000000..4229dce61 --- /dev/null +++ b/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs @@ -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 : 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 GetContentType() + { + switch (SerializationFormat) + { + case Handlers.SerializationFormat.Jsv: + return Task.FromResult("text/plain"); + default: + return Task.FromResult(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 GetObjectToSerialize(); + + protected override Task PrepareResponse() + { + return EnsureObjectToSerialize(); + } + + protected async override Task WriteResponseToOutputStream(Stream stream) + { + await EnsureObjectToSerialize(); + + switch (SerializationFormat) + { + case Handlers.SerializationFormat.Jsv: + JsvSerializer.SerializeToStream(_ObjectToSerialize, stream); + break; + default: + JsonSerializer.SerializeToStream(_ObjectToSerialize, stream); + break; + } + } + } + + public enum SerializationFormat + { + Json, + Jsv + } + +} diff --git a/MediaBrowser.Common/Serialization/JsvSerializer.cs b/MediaBrowser.Common/Serialization/JsvSerializer.cs new file mode 100644 index 000000000..c643d41b0 --- /dev/null +++ b/MediaBrowser.Common/Serialization/JsvSerializer.cs @@ -0,0 +1,22 @@ +using System.IO; + +namespace MediaBrowser.Common.Serialization +{ + /// + /// 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 + /// + public static class JsvSerializer + { + public static void SerializeToStream(T obj, Stream stream) + { + ServiceStack.Text.TypeSerializer.SerializeToStream(obj, stream); + } + + public static T DeserializeFromStream(Stream stream) + { + return ServiceStack.Text.TypeSerializer.DeserializeFromStream(stream); + } + } +}