1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Api/QualityType/RequestExtensions.cs
kay.one c184595935 added Nancy pipelines for error handling,
cleaned up container registrations.
2013-02-15 16:52:04 -08:00

31 lines
1005 B
C#

using System;
using System.IO;
using System.Linq;
using Nancy;
using Nancy.Responses;
using Newtonsoft.Json;
namespace NzbDrone.Api.QualityType
{
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, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
});
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
{
ISerializer serializer = new DefaultJsonSerializer();
var jsonResponse = new JsonResponse<TModel>(model, serializer) { StatusCode = statusCode };
return jsonResponse;
}
}
}