2012-09-19 16:51:37 +00:00
|
|
|
|
using MediaBrowser.Common.Serialization;
|
|
|
|
|
using System;
|
2012-08-28 14:22:25 +00:00
|
|
|
|
using System.IO;
|
2012-08-22 13:19:18 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Net.Handlers
|
|
|
|
|
{
|
2012-09-02 05:30:25 +00:00
|
|
|
|
public abstract class BaseSerializationHandler<T> : BaseHandler
|
2012-09-19 16:51:37 +00:00
|
|
|
|
where T : class
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
|
|
|
|
public SerializationFormat SerializationFormat
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string format = QueryString["dataformat"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(format))
|
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return SerializationFormat.Json;
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (SerializationFormat)Enum.Parse(typeof(SerializationFormat), format, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-19 16:51:37 +00:00
|
|
|
|
|
|
|
|
|
protected string ContentType
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
get
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
switch (SerializationFormat)
|
|
|
|
|
{
|
|
|
|
|
case SerializationFormat.Jsv:
|
|
|
|
|
return "text/plain";
|
|
|
|
|
case SerializationFormat.Protobuf:
|
|
|
|
|
return "application/x-protobuf";
|
|
|
|
|
default:
|
|
|
|
|
return MimeTypes.JsonMimeType;
|
|
|
|
|
}
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
protected override async Task<ResponseInfo> GetResponseInfo()
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ResponseInfo info = new ResponseInfo
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
ContentType = ContentType
|
|
|
|
|
};
|
2012-08-22 13:19:18 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
_objectToSerialize = await GetObjectToSerialize().ConfigureAwait(false);
|
2012-08-22 13:19:18 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
if (_objectToSerialize == null)
|
|
|
|
|
{
|
|
|
|
|
info.StatusCode = 404;
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
2012-09-19 16:51:37 +00:00
|
|
|
|
|
|
|
|
|
return info;
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
private T _objectToSerialize;
|
2012-08-22 13:19:18 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
protected abstract Task<T> GetObjectToSerialize();
|
2012-08-22 13:19:18 +00:00
|
|
|
|
|
2012-09-19 16:51:37 +00:00
|
|
|
|
protected override Task WriteResponseToOutputStream(Stream stream)
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
return Task.Run(() =>
|
2012-08-22 13:19:18 +00:00
|
|
|
|
{
|
2012-09-19 16:51:37 +00:00
|
|
|
|
switch (SerializationFormat)
|
|
|
|
|
{
|
|
|
|
|
case SerializationFormat.Jsv:
|
|
|
|
|
JsvSerializer.SerializeToStream(_objectToSerialize, stream);
|
|
|
|
|
break;
|
|
|
|
|
case SerializationFormat.Protobuf:
|
|
|
|
|
ProtobufSerializer.SerializeToStream(_objectToSerialize, stream);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
JsonSerializer.SerializeToStream(_objectToSerialize, stream);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum SerializationFormat
|
|
|
|
|
{
|
|
|
|
|
Json,
|
2012-08-28 14:22:25 +00:00
|
|
|
|
Jsv,
|
|
|
|
|
Protobuf
|
2012-08-22 13:19:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|