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\"";
///
/// Gets the default options.
///
///
/// When changing these options, update
/// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
/// -> AddJellyfinApi
/// -> AddJsonOptions.
///
/// The default options.
public static JsonSerializerOptions GetOptions()
{
var options = new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Disallow,
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
// Get built-in converters for fallback converting.
var baseNullableInt32Converter = (JsonConverter)options.GetConverter(typeof(int?));
var baseNullableInt64Converter = (JsonConverter)options.GetConverter(typeof(long?));
options.Converters.Add(new JsonGuidConverter());
options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new JsonNullableStructConverter(baseNullableInt32Converter));
options.Converters.Add(new JsonNullableStructConverter(baseNullableInt64Converter));
return options;
}
///
/// Gets camelCase json options.
///
/// The camelCase options.
public static JsonSerializerOptions GetCamelCaseOptions()
{
var options = GetOptions();
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
return options;
}
///
/// Gets PascalCase json options.
///
/// The PascalCase options.
public static JsonSerializerOptions GetPascalCaseOptions()
{
var options = GetOptions();
options.PropertyNamingPolicy = null;
return options;
}
}
}