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
|
|
|
|
{
|
2020-09-03 18:15:24 +00:00
|
|
|
/// <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\"";
|
|
|
|
|
2020-12-23 16:01:24 +00:00
|
|
|
/// <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(),
|
2020-12-29 23:08:16 +00:00
|
|
|
new JsonNullableGuidConverter(),
|
2021-03-05 14:48:45 +00:00
|
|
|
new JsonVersionConverter(),
|
2020-12-23 16:01:24 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-09-25 15:43:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the default <see cref="JsonSerializerOptions" /> options.
|
|
|
|
/// </summary>
|
2020-05-21 14:44:15 +00:00
|
|
|
/// <remarks>
|
2020-12-23 16:01:24 +00:00
|
|
|
/// The return value must not be modified.
|
|
|
|
/// If the defaults must be modified the author must use the copy constructor.
|
2020-05-21 14:44:15 +00:00
|
|
|
/// </remarks>
|
2019-09-25 15:43:20 +00:00
|
|
|
/// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
|
|
|
|
public static JsonSerializerOptions GetOptions()
|
2020-12-23 16:01:24 +00:00
|
|
|
=> _jsonSerializerOptions;
|
2020-06-13 19:11:41 +00:00
|
|
|
|
2020-05-21 14:44:15 +00:00
|
|
|
/// <summary>
|
2020-06-13 19:11:41 +00:00
|
|
|
/// Gets camelCase json options.
|
2020-05-21 14:44:15 +00:00
|
|
|
/// </summary>
|
2020-12-23 16:01:24 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// The return value must not be modified.
|
|
|
|
/// If the defaults must be modified the author must use the copy constructor.
|
|
|
|
/// </remarks>
|
2020-06-13 19:11:41 +00:00
|
|
|
/// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
|
|
|
|
public static JsonSerializerOptions GetCamelCaseOptions()
|
2020-12-23 16:01:24 +00:00
|
|
|
=> _camelCaseJsonSerializerOptions;
|
2020-05-21 14:44:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets PascalCase json options.
|
|
|
|
/// </summary>
|
2020-12-23 16:01:24 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// The return value must not be modified.
|
|
|
|
/// If the defaults must be modified the author must use the copy constructor.
|
|
|
|
/// </remarks>
|
2020-06-13 19:11:41 +00:00
|
|
|
/// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
|
|
|
|
public static JsonSerializerOptions GetPascalCaseOptions()
|
2020-12-23 16:01:24 +00:00
|
|
|
=> _pascalCaseJsonSerializerOptions;
|
2019-09-25 15:43:20 +00:00
|
|
|
}
|
|
|
|
}
|