mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 03:26:07 +02:00
Merge pull request #2932 from crobibero/api-casing
Add optional camelCase formatter
This commit is contained in:
commit
383bc45372
4 changed files with 88 additions and 0 deletions
|
@ -4,6 +4,7 @@ using Jellyfin.Api.Auth.FirstTimeSetupOrElevatedPolicy;
|
|||
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
||||
using Jellyfin.Api.Constants;
|
||||
using Jellyfin.Api.Controllers;
|
||||
using Jellyfin.Server.Formatters;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
@ -66,6 +67,8 @@ namespace Jellyfin.Server.Extensions
|
|||
return serviceCollection.AddMvc(opts =>
|
||||
{
|
||||
opts.UseGeneralRoutePrefix(baseUrl);
|
||||
opts.OutputFormatters.Insert(0, new CamelCaseJsonProfileFormatter());
|
||||
opts.OutputFormatters.Insert(0, new PascalCaseJsonProfileFormatter());
|
||||
})
|
||||
|
||||
// Clear app parts to avoid other assemblies being picked up
|
||||
|
|
21
Jellyfin.Server/Formatters/CamelCaseJsonProfileFormatter.cs
Normal file
21
Jellyfin.Server/Formatters/CamelCaseJsonProfileFormatter.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using Jellyfin.Server.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Jellyfin.Server.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Camel Case Json Profile Formatter.
|
||||
/// </summary>
|
||||
public class CamelCaseJsonProfileFormatter : SystemTextJsonOutputFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CamelCaseJsonProfileFormatter"/> class.
|
||||
/// </summary>
|
||||
public CamelCaseJsonProfileFormatter() : base(JsonOptions.CamelCase)
|
||||
{
|
||||
SupportedMediaTypes.Clear();
|
||||
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json;profile=\"CamelCase\""));
|
||||
}
|
||||
}
|
||||
}
|
23
Jellyfin.Server/Formatters/PascalCaseJsonProfileFormatter.cs
Normal file
23
Jellyfin.Server/Formatters/PascalCaseJsonProfileFormatter.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using Jellyfin.Server.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Jellyfin.Server.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Pascal Case Json Profile Formatter.
|
||||
/// </summary>
|
||||
public class PascalCaseJsonProfileFormatter : SystemTextJsonOutputFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PascalCaseJsonProfileFormatter"/> class.
|
||||
/// </summary>
|
||||
public PascalCaseJsonProfileFormatter() : base(JsonOptions.PascalCase)
|
||||
{
|
||||
SupportedMediaTypes.Clear();
|
||||
// Add application/json for default formatter
|
||||
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
|
||||
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json;profile=\"PascalCase\""));
|
||||
}
|
||||
}
|
||||
}
|
41
Jellyfin.Server/Models/JsonOptions.cs
Normal file
41
Jellyfin.Server/Models/JsonOptions.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System.Text.Json;
|
||||
|
||||
namespace Jellyfin.Server.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Json Options.
|
||||
/// </summary>
|
||||
public static class JsonOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets CamelCase json options.
|
||||
/// </summary>
|
||||
public static JsonSerializerOptions CamelCase
|
||||
{
|
||||
get
|
||||
{
|
||||
var options = DefaultJsonOptions;
|
||||
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets PascalCase json options.
|
||||
/// </summary>
|
||||
public static JsonSerializerOptions PascalCase
|
||||
{
|
||||
get
|
||||
{
|
||||
var options = DefaultJsonOptions;
|
||||
options.PropertyNamingPolicy = null;
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets base Json Serializer Options.
|
||||
/// </summary>
|
||||
private static JsonSerializerOptions DefaultJsonOptions => new JsonSerializerOptions();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue