refactor: Centralize JSON serializer settings

json-serializing-nullable-fields-issue
Robert Dailey 8 months ago
parent 8f267483a2
commit 016bcb6624

@ -2,10 +2,10 @@ using System.IO.Abstractions;
using System.Reflection;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Recyclarr.Common.Extensions;
using Recyclarr.TrashLib.Config.Services;
using Recyclarr.TrashLib.Interfaces;
using Recyclarr.TrashLib.Json;
namespace Recyclarr.Cli.Cache;
@ -19,14 +19,7 @@ public partial class ServiceCache : IServiceCache
{
_storagePath = storagePath;
_log = log;
_jsonSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
_jsonSettings = GlobalJsonSerializerSettings.Recyclarr;
}
public T? Load<T>(IServiceConfiguration config) where T : class

@ -14,7 +14,7 @@ public sealed class ErrorResponseParser
{
_log = log;
_streamFactory = () => new JsonTextReader(new StringReader(responseBody));
_serializer = ServiceJsonSerializerFactory.Create();
_serializer = JsonSerializer.Create(GlobalJsonSerializerSettings.Services);
}
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]

@ -30,7 +30,7 @@ public class FlurlClientFactory : IFlurlClientFactory
{
var settings = new ClientFlurlHttpSettings
{
JsonSerializer = new NewtonsoftJsonSerializer(ServiceJsonSerializerFactory.Settings)
JsonSerializer = new NewtonsoftJsonSerializer(GlobalJsonSerializerSettings.Services)
};
FlurlLogging.SetupLogging(settings, _log);

@ -0,0 +1,38 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Recyclarr.TrashLib.Json;
public static class GlobalJsonSerializerSettings
{
/// <summary>
/// JSON settings used for starr service API payloads.
/// </summary>
public static JsonSerializerSettings Services { get; } = new()
{
// This makes sure that null properties, such as maxSize and preferredSize in Radarr
// Quality Definitions, do not get written out to JSON request bodies.
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new ServiceContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};
/// <summary>
/// JSON settings used by cache and other Recyclarr-owned JSON files.
/// </summary>
public static JsonSerializerSettings Recyclarr { get; } = new()
{
Formatting = Formatting.Indented,
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
/// <summary>
/// JSON settings used by Trash Guides JSON files.
/// </summary>
public static JsonSerializerSettings Guide => Recyclarr;
}

@ -1,23 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Recyclarr.TrashLib.Json;
public static class ServiceJsonSerializerFactory
{
public static JsonSerializerSettings Settings { get; } = new()
{
// This makes sure that null properties, such as maxSize and preferredSize in Radarr
// Quality Definitions, do not get written out to JSON request bodies.
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new ServiceContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
}
};
public static JsonSerializer Create()
{
return JsonSerializer.Create(Settings);
}
}

@ -1,3 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Recyclarr.Cli.Pipelines.CustomFormat.Guide;
using Recyclarr.Common.Extensions;
@ -123,7 +124,7 @@ public class CustomFormatParserTest
IncludeCustomFormatWhenRenaming = false
};
var json = JObject.FromObject(cf, ServiceJsonSerializerFactory.Create());
var json = JObject.FromObject(cf, JsonSerializer.Create(GlobalJsonSerializerSettings.Services));
json.Children<JProperty>().Should().NotContain(x => x.Name.ContainsIgnoreCase("trash"));
}

@ -11,7 +11,7 @@ public class FieldsArrayJsonConverterTest
[Test]
public void Read_multiple_as_array()
{
var serializer = new NewtonsoftJsonSerializer(ServiceJsonSerializerFactory.Settings);
var serializer = new NewtonsoftJsonSerializer(GlobalJsonSerializerSettings.Services);
const string json =
"""
@ -58,7 +58,7 @@ public class FieldsArrayJsonConverterTest
[Test]
public void Read_single_as_array()
{
var serializer = new NewtonsoftJsonSerializer(ServiceJsonSerializerFactory.Settings);
var serializer = new NewtonsoftJsonSerializer(GlobalJsonSerializerSettings.Services);
const string json =
"""
@ -89,7 +89,7 @@ public class FieldsArrayJsonConverterTest
[Test]
public void Read_throws_on_unsupported_token_type()
{
var serializer = new NewtonsoftJsonSerializer(ServiceJsonSerializerFactory.Settings);
var serializer = new NewtonsoftJsonSerializer(GlobalJsonSerializerSettings.Services);
const string json =
"""

Loading…
Cancel
Save