Move field to the controller

This commit is contained in:
David 2020-07-14 20:20:24 +02:00
parent 9f567e6471
commit c6a0306a34
3 changed files with 17 additions and 22 deletions

View File

@ -23,6 +23,8 @@ namespace Jellyfin.Api.Controllers
private readonly IServerConfigurationManager _configurationManager; private readonly IServerConfigurationManager _configurationManager;
private readonly IMediaEncoder _mediaEncoder; private readonly IMediaEncoder _mediaEncoder;
private readonly JsonSerializerOptions _serializerOptions = JsonDefaults.GetOptions();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ConfigurationController"/> class. /// Initializes a new instance of the <see cref="ConfigurationController"/> class.
/// </summary> /// </summary>
@ -88,7 +90,7 @@ namespace Jellyfin.Api.Controllers
public async Task<ActionResult> UpdateNamedConfiguration([FromRoute] string? key) public async Task<ActionResult> UpdateNamedConfiguration([FromRoute] string? key)
{ {
var configurationType = _configurationManager.GetConfigurationType(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); _configurationManager.SaveConfiguration(key, configuration);
return NoContent(); return NoContent();
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -27,6 +26,8 @@ namespace Jellyfin.Api.Controllers
private readonly IApplicationHost _appHost; private readonly IApplicationHost _appHost;
private readonly IInstallationManager _installationManager; private readonly IInstallationManager _installationManager;
private readonly JsonSerializerOptions _serializerOptions = JsonDefaults.GetOptions();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PluginsController"/> class. /// Initializes a new instance of the <see cref="PluginsController"/> class.
/// </summary> /// </summary>
@ -119,7 +120,7 @@ namespace Jellyfin.Api.Controllers
return NotFound(); 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); .ConfigureAwait(false);
plugin.UpdateConfiguration(configuration); plugin.UpdateConfiguration(configuration);

View File

@ -9,8 +9,6 @@ namespace MediaBrowser.Common.Json
/// </summary> /// </summary>
public static class JsonDefaults public static class JsonDefaults
{ {
private static JsonSerializerOptions _defaultOptions;
/// <summary> /// <summary>
/// Gets the default <see cref="JsonSerializerOptions" /> options. /// Gets the default <see cref="JsonSerializerOptions" /> options.
/// </summary> /// </summary>
@ -23,26 +21,20 @@ namespace MediaBrowser.Common.Json
/// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns> /// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetOptions() 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 JsonGuidConverter());
options.Converters.Add(new JsonInt32Converter()); options.Converters.Add(new JsonInt32Converter());
options.Converters.Add(new JsonStringEnumConverter()); options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory()); options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory());
options.Converters.Add(new JsonInt64Converter()); options.Converters.Add(new JsonInt64Converter());
options.Converters.Add(new JsonDoubleConverter()); options.Converters.Add(new JsonDoubleConverter());
_defaultOptions = options; return options;
return _defaultOptions;
}
return _defaultOptions;
} }
/// <summary> /// <summary>