2013-01-25 19:03:28 +00:00
|
|
|
|
using System.IO;
|
2013-01-19 01:27:30 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Nancy;
|
|
|
|
|
using Nancy.Responses;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
2013-01-25 19:03:28 +00:00
|
|
|
|
namespace NzbDrone.Api.Extentions
|
2013-01-19 01:27:30 +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();
|
2013-01-25 19:03:28 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(value, Serializer.Settings);
|
2013-01-19 01:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 04:46:43 +00:00
|
|
|
|
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
|
2013-01-19 01:27:30 +00:00
|
|
|
|
{
|
2013-01-25 19:03:28 +00:00
|
|
|
|
var jsonResponse = new JsonResponse<TModel>(model, new NancyJsonSerializer()) { StatusCode = statusCode };
|
2013-01-19 01:27:30 +00:00
|
|
|
|
return jsonResponse;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|