aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2013-02-20 17:10:49 -0800
committerLuke <luke.pulverenti@gmail.com>2013-02-20 17:10:49 -0800
commit845554722efaed872948a9e0f7202e3ef52f1b6e (patch)
tree534ab2d11fe4824ed303bb01e885b330631b657e /MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs
parent2f142263f080f7b012a0ec2095d350ccf75b49b7 (diff)
parent14bbb6804718ef78a8118fe750896ba679e3b6cb (diff)
Merge pull request #1 from MediaBrowser/Repo
Repo
Diffstat (limited to 'MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs')
-rw-r--r--MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs b/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs
new file mode 100644
index 000000000..53b3ee817
--- /dev/null
+++ b/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs
@@ -0,0 +1,90 @@
+using MediaBrowser.Common.Serialization;
+using System;
+using System.IO;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Common.Net.Handlers
+{
+ public abstract class BaseSerializationHandler<T> : BaseHandler
+ where T : class
+ {
+ public SerializationFormat SerializationFormat
+ {
+ get
+ {
+ string format = QueryString["dataformat"];
+
+ if (string.IsNullOrEmpty(format))
+ {
+ return SerializationFormat.Json;
+ }
+
+ return (SerializationFormat)Enum.Parse(typeof(SerializationFormat), format, true);
+ }
+ }
+
+ protected string ContentType
+ {
+ get
+ {
+ switch (SerializationFormat)
+ {
+ case SerializationFormat.Jsv:
+ return "text/plain";
+ case SerializationFormat.Protobuf:
+ return "application/x-protobuf";
+ default:
+ return MimeTypes.JsonMimeType;
+ }
+ }
+ }
+
+ protected override async Task<ResponseInfo> GetResponseInfo()
+ {
+ ResponseInfo info = new ResponseInfo
+ {
+ ContentType = ContentType
+ };
+
+ _objectToSerialize = await GetObjectToSerialize().ConfigureAwait(false);
+
+ if (_objectToSerialize == null)
+ {
+ info.StatusCode = 404;
+ }
+
+ return info;
+ }
+
+ private T _objectToSerialize;
+
+ protected abstract Task<T> GetObjectToSerialize();
+
+ protected override Task WriteResponseToOutputStream(Stream stream)
+ {
+ return Task.Run(() =>
+ {
+ switch (SerializationFormat)
+ {
+ case SerializationFormat.Jsv:
+ JsvSerializer.SerializeToStream(_objectToSerialize, stream);
+ break;
+ case SerializationFormat.Protobuf:
+ ProtobufSerializer.SerializeToStream(_objectToSerialize, stream);
+ break;
+ default:
+ JsonSerializer.SerializeToStream(_objectToSerialize, stream);
+ break;
+ }
+ });
+ }
+ }
+
+ public enum SerializationFormat
+ {
+ Json,
+ Jsv,
+ Protobuf
+ }
+
+}