From c6a0306a34e72fca424545bd33772e91aab92ed7 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 14 Jul 2020 20:20:24 +0200 Subject: [PATCH] Move field to the controller --- .../Controllers/ConfigurationController.cs | 4 ++- Jellyfin.Api/Controllers/PluginsController.cs | 5 ++-- MediaBrowser.Common/Json/JsonDefaults.cs | 30 +++++++------------ 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Jellyfin.Api/Controllers/ConfigurationController.cs b/Jellyfin.Api/Controllers/ConfigurationController.cs index d3c29969b..7d262ed59 100644 --- a/Jellyfin.Api/Controllers/ConfigurationController.cs +++ b/Jellyfin.Api/Controllers/ConfigurationController.cs @@ -23,6 +23,8 @@ namespace Jellyfin.Api.Controllers private readonly IServerConfigurationManager _configurationManager; private readonly IMediaEncoder _mediaEncoder; + private readonly JsonSerializerOptions _serializerOptions = JsonDefaults.GetOptions(); + /// /// Initializes a new instance of the class. /// @@ -88,7 +90,7 @@ namespace Jellyfin.Api.Controllers public async Task UpdateNamedConfiguration([FromRoute] string? key) { var configurationType = _configurationManager.GetConfigurationType(key); - var configuration = await JsonSerializer.DeserializeAsync(Request.Body, configurationType, JsonDefaults.GetOptions()).ConfigureAwait(false); + var configuration = await JsonSerializer.DeserializeAsync(Request.Body, configurationType, _serializerOptions).ConfigureAwait(false); _configurationManager.SaveConfiguration(key, configuration); return NoContent(); } diff --git a/Jellyfin.Api/Controllers/PluginsController.cs b/Jellyfin.Api/Controllers/PluginsController.cs index 9b5529c37..770d74838 100644 --- a/Jellyfin.Api/Controllers/PluginsController.cs +++ b/Jellyfin.Api/Controllers/PluginsController.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text.Json; using System.Threading.Tasks; @@ -27,6 +26,8 @@ namespace Jellyfin.Api.Controllers private readonly IApplicationHost _appHost; private readonly IInstallationManager _installationManager; + private readonly JsonSerializerOptions _serializerOptions = JsonDefaults.GetOptions(); + /// /// Initializes a new instance of the class. /// @@ -119,7 +120,7 @@ namespace Jellyfin.Api.Controllers return NotFound(); } - var configuration = (BasePluginConfiguration)await JsonSerializer.DeserializeAsync(Request.Body, plugin.ConfigurationType, JsonDefaults.GetOptions()) + var configuration = (BasePluginConfiguration)await JsonSerializer.DeserializeAsync(Request.Body, plugin.ConfigurationType, _serializerOptions) .ConfigureAwait(false); plugin.UpdateConfiguration(configuration); diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs index c8217f9ab..36ab6d900 100644 --- a/MediaBrowser.Common/Json/JsonDefaults.cs +++ b/MediaBrowser.Common/Json/JsonDefaults.cs @@ -9,8 +9,6 @@ namespace MediaBrowser.Common.Json /// public static class JsonDefaults { - private static JsonSerializerOptions _defaultOptions; - /// /// Gets the default options. /// @@ -23,26 +21,20 @@ namespace MediaBrowser.Common.Json /// The default options. public static JsonSerializerOptions GetOptions() { - if (_defaultOptions == null) + var options = new JsonSerializerOptions { - var options = new JsonSerializerOptions - { - ReadCommentHandling = JsonCommentHandling.Disallow, - WriteIndented = false - }; + ReadCommentHandling = JsonCommentHandling.Disallow, + WriteIndented = false + }; - options.Converters.Add(new JsonGuidConverter()); - options.Converters.Add(new JsonInt32Converter()); - options.Converters.Add(new JsonStringEnumConverter()); - options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory()); - options.Converters.Add(new JsonInt64Converter()); - options.Converters.Add(new JsonDoubleConverter()); + options.Converters.Add(new JsonGuidConverter()); + options.Converters.Add(new JsonInt32Converter()); + options.Converters.Add(new JsonStringEnumConverter()); + options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory()); + options.Converters.Add(new JsonInt64Converter()); + options.Converters.Add(new JsonDoubleConverter()); - _defaultOptions = options; - return _defaultOptions; - } - - return _defaultOptions; + return options; } ///