Merge pull request #4855 from crobibero/cache-json-serializer

Initialize JsonSerializerOptions statically
This commit is contained in:
Bond-009 2020-12-23 18:09:39 +01:00 committed by GitHub
commit f42208673f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,55 +20,69 @@ namespace MediaBrowser.Common.Json
public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\""; public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\"";
/// <summary> /// <summary>
/// Gets the default <see cref="JsonSerializerOptions" /> options.
/// </summary>
/// <remarks>
/// When changing these options, update /// When changing these options, update
/// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs /// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
/// -> AddJellyfinApi /// -> AddJellyfinApi
/// -> AddJsonOptions. /// -> AddJsonOptions.
/// </remarks> /// </summary>
/// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns> private static readonly JsonSerializerOptions _jsonSerializerOptions = new ()
public static JsonSerializerOptions GetOptions()
{
var options = new JsonSerializerOptions
{ {
ReadCommentHandling = JsonCommentHandling.Disallow, ReadCommentHandling = JsonCommentHandling.Disallow,
WriteIndented = false, WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.AllowReadingFromString NumberHandling = JsonNumberHandling.AllowReadingFromString,
Converters =
{
new JsonGuidConverter(),
new JsonVersionConverter(),
new JsonStringEnumConverter(),
new JsonNullableStructConverterFactory(),
new JsonBoolNumberConverter(),
new JsonDateTimeConverter()
}
}; };
options.Converters.Add(new JsonGuidConverter()); private static readonly JsonSerializerOptions _pascalCaseJsonSerializerOptions = new (_jsonSerializerOptions)
options.Converters.Add(new JsonVersionConverter()); {
options.Converters.Add(new JsonStringEnumConverter()); PropertyNamingPolicy = null
options.Converters.Add(new JsonNullableStructConverterFactory()); };
options.Converters.Add(new JsonBoolNumberConverter());
options.Converters.Add(new JsonDateTimeConverter());
return options; private static readonly JsonSerializerOptions _camelCaseJsonSerializerOptions = new (_jsonSerializerOptions)
} {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
/// <summary>
/// Gets the default <see cref="JsonSerializerOptions" /> 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 default <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetOptions()
=> _jsonSerializerOptions;
/// <summary> /// <summary>
/// Gets camelCase json options. /// Gets camelCase json options.
/// </summary> /// </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> /// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetCamelCaseOptions() public static JsonSerializerOptions GetCamelCaseOptions()
{ => _camelCaseJsonSerializerOptions;
var options = GetOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
return options;
}
/// <summary> /// <summary>
/// Gets PascalCase json options. /// Gets PascalCase json options.
/// </summary> /// </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> /// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetPascalCaseOptions() public static JsonSerializerOptions GetPascalCaseOptions()
{ => _pascalCaseJsonSerializerOptions;
var options = GetOptions();
options.PropertyNamingPolicy = null;
return options;
}
} }
} }