using System.Text.Json;
using System.Text.Json.Serialization;
using MediaBrowser.Common.Json.Converters;
namespace MediaBrowser.Common.Json
{
///
/// Helper class for having compatible JSON throughout the codebase.
///
public static class JsonDefaults
{
///
/// Pascal case json profile media type.
///
public const string PascalCaseMediaType = "application/json; profile=\"PascalCase\"";
///
/// Camel case json profile media type.
///
public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\"";
///
/// When changing these options, update
/// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
/// -> AddJellyfinApi
/// -> AddJsonOptions.
///
private static readonly JsonSerializerOptions _jsonSerializerOptions = new ()
{
ReadCommentHandling = JsonCommentHandling.Disallow,
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
Converters =
{
new JsonGuidConverter(),
new JsonNullableGuidConverter(),
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
};
///
/// Gets the default options.
///
///
/// The return value must not be modified.
/// If the defaults must be modified the author must use the copy constructor.
///
/// The default options.
public static JsonSerializerOptions GetOptions()
=> _jsonSerializerOptions;
///
/// Gets camelCase json options.
///
///
/// The return value must not be modified.
/// If the defaults must be modified the author must use the copy constructor.
///
/// The camelCase options.
public static JsonSerializerOptions GetCamelCaseOptions()
=> _camelCaseJsonSerializerOptions;
///
/// Gets PascalCase json options.
///
///
/// The return value must not be modified.
/// If the defaults must be modified the author must use the copy constructor.
///
/// The PascalCase options.
public static JsonSerializerOptions GetPascalCaseOptions()
=> _pascalCaseJsonSerializerOptions;
}
}