2019-09-25 08:43:20 -07: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>
|
|
|
|
/// Gets the default <see cref="JsonSerializerOptions" /> options.
|
|
|
|
/// </summary>
|
2020-05-21 07:44:15 -07:00
|
|
|
/// <remarks>
|
|
|
|
/// When changing these options, update
|
|
|
|
/// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
|
|
|
|
/// -> AddJellyfinApi
|
2020-06-13 12:11:41 -07:00
|
|
|
/// -> AddJsonOptions.
|
2020-05-21 07:44:15 -07:00
|
|
|
/// </remarks>
|
2019-09-25 08:43:20 -07:00
|
|
|
/// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
|
|
|
|
public static JsonSerializerOptions GetOptions()
|
|
|
|
{
|
2020-07-14 11:20:24 -07:00
|
|
|
var options = new JsonSerializerOptions
|
2019-09-25 08:43:20 -07:00
|
|
|
{
|
2020-07-14 11:20:24 -07:00
|
|
|
ReadCommentHandling = JsonCommentHandling.Disallow,
|
|
|
|
WriteIndented = false
|
|
|
|
};
|
2019-09-25 08:43:20 -07:00
|
|
|
|
2020-07-14 11:20:24 -07:00
|
|
|
options.Converters.Add(new JsonGuidConverter());
|
|
|
|
options.Converters.Add(new JsonInt32Converter());
|
2020-08-21 09:23:47 -07:00
|
|
|
options.Converters.Add(new JsonNullableInt32Converter());
|
2020-07-14 11:20:24 -07:00
|
|
|
options.Converters.Add(new JsonStringEnumConverter());
|
|
|
|
options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory());
|
|
|
|
options.Converters.Add(new JsonInt64Converter());
|
2020-08-21 09:23:47 -07:00
|
|
|
options.Converters.Add(new JsonNullableInt64Converter());
|
2020-07-14 11:20:24 -07:00
|
|
|
options.Converters.Add(new JsonDoubleConverter());
|
2020-07-14 03:39:58 -07:00
|
|
|
|
2020-07-14 11:20:24 -07:00
|
|
|
return options;
|
2019-09-25 08:43:20 -07:00
|
|
|
}
|
2020-06-13 12:11:41 -07:00
|
|
|
|
2020-05-21 07:44:15 -07:00
|
|
|
/// <summary>
|
2020-06-13 12:11:41 -07:00
|
|
|
/// Gets camelCase json options.
|
2020-05-21 07:44:15 -07:00
|
|
|
/// </summary>
|
2020-06-13 12:11:41 -07:00
|
|
|
/// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
|
|
|
|
public static JsonSerializerOptions GetCamelCaseOptions()
|
2020-05-21 07:44:15 -07:00
|
|
|
{
|
2020-06-13 12:11:41 -07:00
|
|
|
var options = GetOptions();
|
|
|
|
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
|
|
|
return options;
|
2020-05-21 07:44:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets PascalCase json options.
|
|
|
|
/// </summary>
|
2020-06-13 12:11:41 -07:00
|
|
|
/// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
|
|
|
|
public static JsonSerializerOptions GetPascalCaseOptions()
|
2020-05-21 07:44:15 -07:00
|
|
|
{
|
2020-06-13 12:11:41 -07:00
|
|
|
var options = GetOptions();
|
|
|
|
options.PropertyNamingPolicy = null;
|
|
|
|
return options;
|
2020-05-21 07:44:15 -07:00
|
|
|
}
|
2019-09-25 08:43:20 -07:00
|
|
|
}
|
|
|
|
}
|