using System; using System.IO; using System.Threading.Tasks; using MediaBrowser.Common.Serialization; 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"); case Handlers.SerializationFormat.Protobuf: return Task.FromResult("application/x-protobuf"); 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; case Handlers.SerializationFormat.Protobuf: ProtobufSerializer.SerializeToStream(_ObjectToSerialize, stream); break; default: JsonSerializer.SerializeToStream(_ObjectToSerialize, stream); break; } } public override bool ShouldCompressResponse(string contentType) { return SerializationFormat != Handlers.SerializationFormat.Protobuf; } } public enum SerializationFormat { Json, Jsv, Protobuf } }