diff options
| author | Bond-009 <bond.009@outlook.com> | 2020-12-23 18:09:39 +0100 |
|---|---|---|
| committer | Joshua M. Boniface <joshua@boniface.me> | 2020-12-30 18:55:51 -0500 |
| commit | 71188ad27a427d96ccc761c5fb46647388af7b22 (patch) | |
| tree | c4a6371f6a02b89e48525f8579d5a28d774d1266 | |
| parent | 0d9e8b4f000f29d1d69b1e972f24cfafe232b758 (diff) | |
Merge pull request #4855 from crobibero/cache-json-serializer
Initialize JsonSerializerOptions statically
(cherry picked from commit f42208673facf26e475c9fad7a1e78b59358f3fa)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
| -rw-r--r-- | MediaBrowser.Common/Json/JsonDefaults.cs | 78 |
1 files changed, 46 insertions, 32 deletions
diff --git a/MediaBrowser.Common/Json/JsonDefaults.cs b/MediaBrowser.Common/Json/JsonDefaults.cs index 128cc9d5d..1ec2a87c5 100644 --- a/MediaBrowser.Common/Json/JsonDefaults.cs +++ b/MediaBrowser.Common/Json/JsonDefaults.cs @@ -20,55 +20,69 @@ namespace MediaBrowser.Common.Json public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\""; /// <summary> + /// When changing these options, update + /// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs + /// -> AddJellyfinApi + /// -> AddJsonOptions. + /// </summary> + 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 + }; + + /// <summary> /// Gets the default <see cref="JsonSerializerOptions" /> options. /// </summary> /// <remarks> - /// 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. /// </remarks> /// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns> 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; /// <summary> /// Gets camelCase json options. /// </summary> + /// <remarks> + /// The return value must not be modified. + /// If the defaults must be modified the author must use the copy constructor. + /// </remarks> /// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns> public static JsonSerializerOptions GetCamelCaseOptions() - { - var options = GetOptions(); - options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - return options; - } + => _camelCaseJsonSerializerOptions; /// <summary> /// Gets PascalCase json options. /// </summary> + /// <remarks> + /// The return value must not be modified. + /// If the defaults must be modified the author must use the copy constructor. + /// </remarks> /// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns> public static JsonSerializerOptions GetPascalCaseOptions() - { - var options = GetOptions(); - options.PropertyNamingPolicy = null; - return options; - } + => _pascalCaseJsonSerializerOptions; } } |
