1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00
Radarr/NzbDrone.Api/Extensions/RequestExtensions.cs

33 lines
1.0 KiB
C#
Raw Normal View History

using System;
using System.IO;
using Nancy;
using Nancy.Responses;
using NzbDrone.Common;
2013-05-12 17:18:17 +02:00
using NzbDrone.Common.Serializer;
2013-02-23 21:35:26 +01:00
namespace NzbDrone.Api.Extensions
{
public static class JsonExtensions
{
private static readonly JsonSerializer Serializer = new JsonSerializer();
private static readonly NancyJsonSerializer NancySerializer = new NancyJsonSerializer(Serializer);
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();
return (T)Serializer.Deserialize(value, type);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
}
}
}