jellyfin-server/MediaBrowser.Common/Json/JsonDefaults.cs

71 lines
2.5 KiB
C#
Raw Normal View History

2019-09-25 15:43:20 +00:00
using System.Text.Json;
using System.Text.Json.Serialization;
using MediaBrowser.Common.Json.Converters;
namespace MediaBrowser.Common.Json
{
/// <summary>
/// Helper class for having compatible JSON throughout the codebase.
/// </summary>
public static class JsonDefaults
{
private static JsonSerializerOptions _defaultOptions;
2019-09-25 15:43:20 +00:00
/// <summary>
/// Gets the default <see cref="JsonSerializerOptions" /> options.
/// </summary>
/// <remarks>
/// When changing these options, update
/// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
/// -> AddJellyfinApi
2020-06-13 19:11:41 +00:00
/// -> AddJsonOptions.
/// </remarks>
2019-09-25 15:43:20 +00:00
/// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetOptions()
{
if (_defaultOptions == null)
2019-09-25 15:43:20 +00:00
{
var options = new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Disallow,
WriteIndented = false
};
2019-09-25 15:43:20 +00:00
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());
2019-09-25 15:43:20 +00:00
_defaultOptions = options;
return _defaultOptions;
}
return _defaultOptions;
2019-09-25 15:43:20 +00:00
}
2020-06-13 19:11:41 +00:00
/// <summary>
2020-06-13 19:11:41 +00:00
/// Gets camelCase json options.
/// </summary>
2020-06-13 19:11:41 +00:00
/// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetCamelCaseOptions()
{
2020-06-13 19:11:41 +00:00
var options = GetOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
return options;
}
/// <summary>
/// Gets PascalCase json options.
/// </summary>
2020-06-13 19:11:41 +00:00
/// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetPascalCaseOptions()
{
2020-06-13 19:11:41 +00:00
var options = GetOptions();
options.PropertyNamingPolicy = null;
return options;
}
2019-09-25 15:43:20 +00:00
}
}