mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 14:03:29 -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,14 +1,19 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Nancy;
|
||||
using Newtonsoft.Json;
|
||||
using NzbDrone.Common;
|
||||
|
||||
namespace NzbDrone.Api.Extensions
|
||||
{
|
||||
public class NancyJsonSerializer : ISerializer
|
||||
{
|
||||
public readonly static NancyJsonSerializer Instance = new NancyJsonSerializer();
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
|
||||
public NancyJsonSerializer(IJsonSerializer jsonSerializer)
|
||||
{
|
||||
_jsonSerializer = jsonSerializer;
|
||||
}
|
||||
|
||||
|
||||
public bool CanSerialize(string contentType)
|
||||
{
|
||||
|
@ -17,9 +22,7 @@ namespace NzbDrone.Api.Extensions
|
|||
|
||||
public void Serialize<TModel>(string contentType, TModel model, Stream outputStream)
|
||||
{
|
||||
var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream));
|
||||
Serializer.Instance.Serialize(jsonTextWriter, model);
|
||||
jsonTextWriter.Flush();
|
||||
_jsonSerializer.Serialize(model, outputStream);
|
||||
}
|
||||
|
||||
public IEnumerable<string> Extensions { get; private set; }
|
||||
|
|
|
@ -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 };
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace NzbDrone.Api.Extensions
|
||||
{
|
||||
public static class Serializer
|
||||
{
|
||||
static Serializer()
|
||||
{
|
||||
Settings = new JsonSerializerSettings
|
||||
{
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Formatting = Formatting.Indented,
|
||||
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
|
||||
};
|
||||
|
||||
Instance = new JsonSerializer
|
||||
{
|
||||
DateTimeZoneHandling = Settings.DateTimeZoneHandling,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Formatting = Formatting.Indented,
|
||||
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
}
|
||||
|
||||
public static JsonSerializerSettings Settings { get; private set; }
|
||||
|
||||
public static JsonSerializer Instance { get; private set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue