New: Use System.Text.Json for Nancy and SignalR

This commit is contained in:
ta264
2021-02-10 21:52:48 +00:00
parent 16b3817202
commit d3e8c7e0c9
40 changed files with 378 additions and 88 deletions

View File

@@ -2,10 +2,11 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Reflection;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Annotations;
namespace Readarr.Http.ClientSchema
@@ -213,9 +214,9 @@ namespace Readarr.Http.ClientSchema
{
return Enumerable.Empty<int>();
}
else if (fieldValue.GetType() == typeof(JArray))
else if (fieldValue is JsonElement e && e.ValueKind == JsonValueKind.Array)
{
return ((JArray)fieldValue).Select(s => s.Value<int>());
return e.EnumerateArray().Select(s => s.GetInt32());
}
else
{
@@ -231,9 +232,9 @@ namespace Readarr.Http.ClientSchema
{
return Enumerable.Empty<string>();
}
else if (fieldValue.GetType() == typeof(JArray))
else if (fieldValue is JsonElement e && e.ValueKind == JsonValueKind.Array)
{
return ((JArray)fieldValue).Select(s => s.Value<string>());
return e.EnumerateArray().Select(s => s.GetString());
}
else
{
@@ -243,7 +244,18 @@ namespace Readarr.Http.ClientSchema
}
else
{
return fieldValue => fieldValue;
return fieldValue =>
{
var element = fieldValue as JsonElement?;
if (element == null || !element.HasValue)
{
return null;
}
var json = element.Value.GetRawText();
return STJson.Deserialize(json, propertyType);
};
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Nancy;
using Nancy.Responses.Negotiation;
using NzbDrone.Common.Serializer;
@@ -8,6 +9,13 @@ namespace Readarr.Http.Extensions
{
public class NancyJsonSerializer : ISerializer
{
protected readonly JsonSerializerOptions _serializerSettings;
public NancyJsonSerializer()
{
_serializerSettings = STJson.GetSerializerSettings();
}
public bool CanSerialize(MediaRange contentType)
{
return contentType == "application/json";
@@ -15,7 +23,7 @@ namespace Readarr.Http.Extensions
public void Serialize<TModel>(MediaRange contentType, TModel model, Stream outputStream)
{
Json.Serialize(model, outputStream);
STJson.Serialize(model, outputStream, _serializerSettings);
}
public IEnumerable<string> Extensions { get; private set; }

View File

@@ -27,10 +27,8 @@ namespace Readarr.Http.Extensions
public static object FromJson(this Stream body, Type type)
{
var reader = new StreamReader(body, true);
body.Position = 0;
var value = reader.ReadToEnd();
return Json.Deserialize(value, type);
return STJson.Deserialize(body, type);
}
public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, NancyContext context, HttpStatusCode statusCode = HttpStatusCode.OK)

View File

@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using FluentValidation;
using FluentValidation.Results;
using Nancy;
using Nancy.Responses.Negotiation;
using Newtonsoft.Json;
using NzbDrone.Core.Datastore;
using Readarr.Http.Extensions;
@@ -248,9 +248,9 @@ namespace Readarr.Http.REST
{
resource = Request.Body.FromJson<TResource>();
}
catch (JsonReaderException ex)
catch (JsonException e)
{
throw new BadRequestException(ex.Message);
throw new BadRequestException(e.Message);
}
if (resource == null)

View File

@@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace Readarr.Http.REST
{
public abstract class RestResource
{
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int Id { get; set; }
[JsonIgnore]

View File

@@ -7,7 +7,6 @@
<PackageReference Include="Nancy" Version="2.0.0" />
<PackageReference Include="Nancy.Authentication.Basic" Version="2.0.0" />
<PackageReference Include="Nancy.Authentication.Forms" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.7.2" />
</ItemGroup>
<ItemGroup>