2013-04-27 04:03:34 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
using Nancy;
|
|
|
|
|
using Nancy.Responses;
|
2013-04-17 02:24:49 +02:00
|
|
|
|
using NzbDrone.Common;
|
2013-05-12 17:18:17 +02:00
|
|
|
|
using NzbDrone.Common.Serializer;
|
2013-01-19 02:27:30 +01:00
|
|
|
|
|
2013-02-23 21:35:26 +01:00
|
|
|
|
namespace NzbDrone.Api.Extensions
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
|
|
|
|
public static class JsonExtensions
|
|
|
|
|
{
|
2013-04-17 02:24:49 +02:00
|
|
|
|
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()
|
2013-04-27 04:03:34 +02:00
|
|
|
|
{
|
|
|
|
|
return FromJson<T>(body, typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T FromJson<T>(this Stream body, Type type)
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
|
|
|
|
var reader = new StreamReader(body, true);
|
|
|
|
|
body.Position = 0;
|
|
|
|
|
var value = reader.ReadToEnd();
|
2013-04-27 04:03:34 +02:00
|
|
|
|
return (T)Serializer.Deserialize(value, type);
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 05:46:43 +01:00
|
|
|
|
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
|
2013-01-19 02:27:30 +01:00
|
|
|
|
{
|
2013-04-17 02:24:49 +02:00
|
|
|
|
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
|
2013-01-19 02:27:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|