Radarr/NzbDrone.Api/Extensions/RequestExtensions.cs

31 lines
952 B
C#
Raw Normal View History

using System;
using System.IO;
using Nancy;
using Nancy.Responses;
2013-05-12 15:18:17 +00:00
using NzbDrone.Common.Serializer;
2013-02-23 20:35:26 +00:00
namespace NzbDrone.Api.Extensions
{
public static class JsonExtensions
{
2013-05-13 02:52:55 +00:00
private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer();
public static T FromJson<T>(this Stream body) where T : class, new()
{
return FromJson<T>(body, typeof(T));
}
public static T FromJson<T>(this Stream body, Type type)
{
var reader = new StreamReader(body, true);
body.Position = 0;
var value = reader.ReadToEnd();
2013-05-13 02:52:55 +00:00
return (T)Json.Deserialize(value, type);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
}
}
}