jellyfin/MediaBrowser.Common/Json/JsonDefaults.cs

77 lines
2.9 KiB
C#
Raw Normal View History

2019-09-25 17:43:20 +02: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
{
/// <summary>
/// Pascal case json profile media type.
/// </summary>
public const string PascalCaseMediaType = "application/json; profile=\"PascalCase\"";
/// <summary>
/// Camel case json profile media type.
/// </summary>
public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\"";
2019-09-25 17:43:20 +02: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 21:11:41 +02:00
/// -> AddJsonOptions.
/// </remarks>
2019-09-25 17:43:20 +02:00
/// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetOptions()
{
2020-07-14 20:20:24 +02:00
var options = new JsonSerializerOptions
2019-09-25 17:43:20 +02:00
{
2020-07-14 20:20:24 +02:00
ReadCommentHandling = JsonCommentHandling.Disallow,
2020-08-23 15:48:12 +02:00
WriteIndented = false,
2020-08-26 16:22:48 +02:00
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.AllowReadingFromString
2020-07-14 20:20:24 +02:00
};
2019-09-25 17:43:20 +02:00
2020-09-01 17:19:22 +02:00
// Get built-in converters for fallback converting.
var baseNullableInt32Converter = (JsonConverter<int?>)options.GetConverter(typeof(int?));
var baseNullableInt64Converter = (JsonConverter<long?>)options.GetConverter(typeof(long?));
2020-07-14 20:20:24 +02:00
options.Converters.Add(new JsonGuidConverter());
options.Converters.Add(new JsonStringEnumConverter());
2020-09-01 17:42:59 +02:00
options.Converters.Add(new JsonNullableStructConverter<int>(baseNullableInt32Converter));
options.Converters.Add(new JsonNullableStructConverter<long>(baseNullableInt64Converter));
2020-07-14 20:20:24 +02:00
return options;
2019-09-25 17:43:20 +02:00
}
2020-06-13 21:11:41 +02:00
/// <summary>
2020-06-13 21:11:41 +02:00
/// Gets camelCase json options.
/// </summary>
2020-06-13 21:11:41 +02:00
/// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetCamelCaseOptions()
{
2020-06-13 21:11:41 +02:00
var options = GetOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
return options;
}
/// <summary>
/// Gets PascalCase json options.
/// </summary>
2020-06-13 21:11:41 +02:00
/// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
public static JsonSerializerOptions GetPascalCaseOptions()
{
2020-06-13 21:11:41 +02:00
var options = GetOptions();
options.PropertyNamingPolicy = null;
return options;
}
2019-09-25 17:43:20 +02:00
}
}