From 016bcb66248d63913c49e0e99f54859a4c90e458 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 9 Sep 2023 22:39:54 -0500 Subject: [PATCH] refactor: Centralize JSON serializer settings --- src/Recyclarr.Cli/Cache/ServiceCache.cs | 11 +----- .../ErrorHandling/ErrorResponseParser.cs | 2 +- .../Http/FlurlClientFactory.cs | 2 +- .../Json/GlobalJsonSerializerSettings.cs | 38 +++++++++++++++++++ .../Json/ServiceJsonSerializerFactory.cs | 23 ----------- .../Guide/CustomFormatParserTest.cs | 3 +- .../Models/FieldsArrayJsonConverterTest.cs | 6 +-- 7 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 src/Recyclarr.TrashLib/Json/GlobalJsonSerializerSettings.cs delete mode 100644 src/Recyclarr.TrashLib/Json/ServiceJsonSerializerFactory.cs diff --git a/src/Recyclarr.Cli/Cache/ServiceCache.cs b/src/Recyclarr.Cli/Cache/ServiceCache.cs index 8c213fad..0dfdf607 100644 --- a/src/Recyclarr.Cli/Cache/ServiceCache.cs +++ b/src/Recyclarr.Cli/Cache/ServiceCache.cs @@ -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(IServiceConfiguration config) where T : class diff --git a/src/Recyclarr.Cli/Processors/ErrorHandling/ErrorResponseParser.cs b/src/Recyclarr.Cli/Processors/ErrorHandling/ErrorResponseParser.cs index ec1a88a6..a71f843d 100644 --- a/src/Recyclarr.Cli/Processors/ErrorHandling/ErrorResponseParser.cs +++ b/src/Recyclarr.Cli/Processors/ErrorHandling/ErrorResponseParser.cs @@ -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")] diff --git a/src/Recyclarr.TrashLib/Http/FlurlClientFactory.cs b/src/Recyclarr.TrashLib/Http/FlurlClientFactory.cs index 1251c14f..f8d00354 100644 --- a/src/Recyclarr.TrashLib/Http/FlurlClientFactory.cs +++ b/src/Recyclarr.TrashLib/Http/FlurlClientFactory.cs @@ -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); diff --git a/src/Recyclarr.TrashLib/Json/GlobalJsonSerializerSettings.cs b/src/Recyclarr.TrashLib/Json/GlobalJsonSerializerSettings.cs new file mode 100644 index 00000000..d2863419 --- /dev/null +++ b/src/Recyclarr.TrashLib/Json/GlobalJsonSerializerSettings.cs @@ -0,0 +1,38 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + +namespace Recyclarr.TrashLib.Json; + +public static class GlobalJsonSerializerSettings +{ + /// + /// JSON settings used for starr service API payloads. + /// + 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() + } + }; + + /// + /// JSON settings used by cache and other Recyclarr-owned JSON files. + /// + public static JsonSerializerSettings Recyclarr { get; } = new() + { + Formatting = Formatting.Indented, + ContractResolver = new DefaultContractResolver + { + NamingStrategy = new SnakeCaseNamingStrategy() + } + }; + + /// + /// JSON settings used by Trash Guides JSON files. + /// + public static JsonSerializerSettings Guide => Recyclarr; +} diff --git a/src/Recyclarr.TrashLib/Json/ServiceJsonSerializerFactory.cs b/src/Recyclarr.TrashLib/Json/ServiceJsonSerializerFactory.cs deleted file mode 100644 index 30930b9b..00000000 --- a/src/Recyclarr.TrashLib/Json/ServiceJsonSerializerFactory.cs +++ /dev/null @@ -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); - } -} diff --git a/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Guide/CustomFormatParserTest.cs b/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Guide/CustomFormatParserTest.cs index 396283fa..7a97b5e6 100644 --- a/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Guide/CustomFormatParserTest.cs +++ b/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Guide/CustomFormatParserTest.cs @@ -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().Should().NotContain(x => x.Name.ContainsIgnoreCase("trash")); } diff --git a/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Models/FieldsArrayJsonConverterTest.cs b/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Models/FieldsArrayJsonConverterTest.cs index 095ebe81..567e5fdb 100644 --- a/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Models/FieldsArrayJsonConverterTest.cs +++ b/src/tests/Recyclarr.Cli.Tests/Pipelines/CustomFormat/Models/FieldsArrayJsonConverterTest.cs @@ -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 = """