mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 05:23:31 -07:00
added an abstraction layer for json serializer, should work in mono.
This commit is contained in:
parent
213c842050
commit
65ae894410
14 changed files with 94 additions and 98 deletions
|
@ -1,25 +1,26 @@
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using Nancy;
|
||||
using Nancy.Responses;
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Common;
|
||||
|
||||
namespace NzbDrone.Api.Extensions
|
||||
{
|
||||
public static class JsonExtensions
|
||||
{
|
||||
public static T FromJson<T>(this Stream body)
|
||||
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()
|
||||
{
|
||||
var reader = new StreamReader(body, true);
|
||||
body.Position = 0;
|
||||
var value = reader.ReadToEnd();
|
||||
return JsonConvert.DeserializeObject<T>(value, Serializer.Settings);
|
||||
return Serializer.Deserialize<T>(value);
|
||||
}
|
||||
|
||||
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK)
|
||||
{
|
||||
var jsonResponse = new JsonResponse<TModel>(model, new NancyJsonSerializer()) { StatusCode = statusCode };
|
||||
return jsonResponse;
|
||||
return new JsonResponse<TModel>(model, NancySerializer) { StatusCode = statusCode };
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue