Radarr/NzbDrone.Services.Api/Extensions/RequestExtensions.cs

25 lines
782 B
C#
Raw Normal View History

using System.IO;
2013-01-29 01:07:18 +00:00
using System.Linq;
using Nancy;
using Nancy.Responses;
using Newtonsoft.Json;
2013-01-29 01:07:18 +00:00
namespace NzbDrone.Services.Api.Extensions
2013-01-29 01:07:18 +00:00
{
public static class JsonExtensions
{
public static T FromJson<T>(this Stream body)
{
var reader = new StreamReader(body, true);
body.Position = 0;
var value = reader.ReadToEnd();
return JsonConvert.DeserializeObject<T>(value, Serializer.Settings);
2013-01-29 01:07:18 +00:00
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
var jsonResponse = new JsonResponse<TModel>(model, new NancyJsonSerializer()) { StatusCode = statusCode };
2013-01-29 01:07:18 +00:00
return jsonResponse;
}
}
}