From af8acf7128c66b369cd8cad0aaebc4c3dd963c41 Mon Sep 17 00:00:00 2001 From: crobibero Date: Wed, 23 Dec 2020 09:01:24 -0700 Subject: [PATCH] Initialize JsonSerializerOptions statically --- MediaBrowser.Common/Json/JsonDefaults.cs | 78 ++++++++++++++---------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs index 128cc9d5d0..1ec2a87c53 100644 --- a/MediaBrowser.Common/Json/JsonDefaults.cs +++ b/MediaBrowser.Common/Json/JsonDefaults.cs @@ -19,56 +19,70 @@ namespace MediaBrowser.Common.Json /// public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\""; + /// + /// When changing these options, update + /// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs + /// -> AddJellyfinApi + /// -> AddJsonOptions. + /// + private static readonly JsonSerializerOptions _jsonSerializerOptions = new () + { + ReadCommentHandling = JsonCommentHandling.Disallow, + WriteIndented = false, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + NumberHandling = JsonNumberHandling.AllowReadingFromString, + Converters = + { + new JsonGuidConverter(), + new JsonVersionConverter(), + new JsonStringEnumConverter(), + new JsonNullableStructConverterFactory(), + new JsonBoolNumberConverter(), + new JsonDateTimeConverter() + } + }; + + private static readonly JsonSerializerOptions _pascalCaseJsonSerializerOptions = new (_jsonSerializerOptions) + { + PropertyNamingPolicy = null + }; + + private static readonly JsonSerializerOptions _camelCaseJsonSerializerOptions = new (_jsonSerializerOptions) + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + /// /// Gets the default options. /// /// - /// When changing these options, update - /// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs - /// -> AddJellyfinApi - /// -> AddJsonOptions. + /// The return value must not be modified. + /// If the defaults must be modified the author must use the copy constructor. /// /// The default options. public static JsonSerializerOptions GetOptions() - { - var options = new JsonSerializerOptions - { - ReadCommentHandling = JsonCommentHandling.Disallow, - WriteIndented = false, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - NumberHandling = JsonNumberHandling.AllowReadingFromString - }; - - options.Converters.Add(new JsonGuidConverter()); - options.Converters.Add(new JsonVersionConverter()); - options.Converters.Add(new JsonStringEnumConverter()); - options.Converters.Add(new JsonNullableStructConverterFactory()); - options.Converters.Add(new JsonBoolNumberConverter()); - options.Converters.Add(new JsonDateTimeConverter()); - - return options; - } + => _jsonSerializerOptions; /// /// Gets camelCase json options. /// + /// + /// The return value must not be modified. + /// If the defaults must be modified the author must use the copy constructor. + /// /// The camelCase options. public static JsonSerializerOptions GetCamelCaseOptions() - { - var options = GetOptions(); - options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - return options; - } + => _camelCaseJsonSerializerOptions; /// /// Gets PascalCase json options. /// + /// + /// The return value must not be modified. + /// If the defaults must be modified the author must use the copy constructor. + /// /// The PascalCase options. public static JsonSerializerOptions GetPascalCaseOptions() - { - var options = GetOptions(); - options.PropertyNamingPolicy = null; - return options; - } + => _pascalCaseJsonSerializerOptions; } }