jellyfin/MediaBrowser.Common/Net/Handlers/JsonHandler.cs
LukePulverenti Luke Pulverenti luke pulverenti 5f5f2838b1 moved the http server to common, added more http handlers
2012-07-12 23:50:50 -04:00

37 lines
932 B
C#

using System.IO;
using System.IO.Compression;
using MediaBrowser.Common.Json;
namespace MediaBrowser.Common.Net.Handlers
{
public abstract class JsonHandler : Response
{
public JsonHandler(RequestContext ctx)
: base(ctx)
{
Headers["Content-Encoding"] = "gzip";
WriteStream = s =>
{
WriteReponse(s);
s.Close();
};
}
public override string ContentType
{
get { return "application/json"; }
}
protected abstract object ObjectToSerialize { get; }
private void WriteReponse(Stream stream)
{
using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
{
JsonSerializer.Serialize(ObjectToSerialize, gzipStream);
}
}
}
}