Switched from self created static constant json options to a source generator options

This commit is contained in:
2026-02-19 08:49:23 +01:00
parent d7b725f541
commit 0bd138df63
20 changed files with 46 additions and 84 deletions

View File

@@ -9,21 +9,9 @@ namespace Moonlight.Api.Http.Services.ContainerHelper;
[JsonSerializable(typeof(ProblemDetails))] [JsonSerializable(typeof(ProblemDetails))]
[JsonSerializable(typeof(RebuildEventDto))] [JsonSerializable(typeof(RebuildEventDto))]
[JsonSerializable(typeof(RequestRebuildDto))] [JsonSerializable(typeof(RequestRebuildDto))]
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
public partial class SerializationContext : JsonSerializerContext public partial class SerializationContext : JsonSerializerContext
{ {
private static JsonSerializerOptions? InternalTunedOptions;
public static JsonSerializerOptions TunedOptions
{
get
{
if (InternalTunedOptions != null)
return InternalTunedOptions;
InternalTunedOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
InternalTunedOptions.TypeInfoResolverChain.Add(Default);
return InternalTunedOptions;
}
}
} }

View File

@@ -1,5 +1,4 @@
using System.Net.Http.Headers; using System.Net.Http.Json;
using System.Net.Http.Json;
using System.Text.Json; using System.Text.Json;
using Moonlight.Api.Http.Services.ContainerHelper; using Moonlight.Api.Http.Services.ContainerHelper;
using Moonlight.Api.Http.Services.ContainerHelper.Requests; using Moonlight.Api.Http.Services.ContainerHelper.Requests;
@@ -42,7 +41,7 @@ public class ContainerHelperService
request.Content = JsonContent.Create( request.Content = JsonContent.Create(
new RequestRebuildDto(noBuildCache), new RequestRebuildDto(noBuildCache),
null, null,
SerializationContext.TunedOptions SerializationContext.Default.Options
); );
var response = await client.SendAsync( var response = await client.SendAsync(
@@ -77,7 +76,7 @@ public class ContainerHelperService
continue; continue;
var data = line.Trim("data: "); var data = line.Trim("data: ");
var deserializedData = JsonSerializer.Deserialize<RebuildEventDto>(data, SerializationContext.TunedOptions); var deserializedData = JsonSerializer.Deserialize<RebuildEventDto>(data, SerializationContext.Default.Options);
yield return deserializedData; yield return deserializedData;
@@ -100,14 +99,14 @@ public class ContainerHelperService
var response = await client.PostAsJsonAsync( var response = await client.PostAsJsonAsync(
"api/configuration/version", "api/configuration/version",
new SetVersionDto(version), new SetVersionDto(version),
SerializationContext.TunedOptions SerializationContext.Default.Options
); );
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
return; return;
var problemDetails = var problemDetails =
await response.Content.ReadFromJsonAsync<ProblemDetails>(SerializationContext.TunedOptions); await response.Content.ReadFromJsonAsync<ProblemDetails>(SerializationContext.Default.Options);
if (problemDetails == null) if (problemDetails == null)
throw new HttpRequestException($"Failed to set version: {response.ReasonPhrase}"); throw new HttpRequestException($"Failed to set version: {response.ReasonPhrase}");

View File

@@ -1,28 +0,0 @@
using System.Text.Json;
using Moonlight.Shared.Http;
namespace Moonlight.Frontend;
public static class Constants
{
public static JsonSerializerOptions SerializerOptions
{
get
{
if (InternalOptions != null)
return InternalOptions;
InternalOptions = new()
{
PropertyNameCaseInsensitive = true
};
// Add source generated options from shared project
InternalOptions.TypeInfoResolverChain.Add(SerializationContext.Default);
return InternalOptions;
}
}
private static JsonSerializerOptions? InternalOptions;
}

View File

@@ -3,6 +3,8 @@ using System.Net.Http.Json;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic;
using Moonlight.Shared.Http;
using Moonlight.Shared.Http.Responses.Admin.Auth; using Moonlight.Shared.Http.Responses.Admin.Auth;
namespace Moonlight.Frontend.Services; namespace Moonlight.Frontend.Services;
@@ -23,7 +25,7 @@ public class RemoteAuthProvider : AuthenticationStateProvider
try try
{ {
var claimResponses = await HttpClient.GetFromJsonAsync<ClaimDto[]>( var claimResponses = await HttpClient.GetFromJsonAsync<ClaimDto[]>(
"api/auth/claims", Constants.SerializerOptions "api/auth/claims", SerializationContext.Default.Options
); );
var claims = claimResponses!.Select(claim => new Claim(claim.Type, claim.Value)); var claims = claimResponses!.Select(claim => new Claim(claim.Type, claim.Value));

View File

@@ -1,5 +1,6 @@
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Frontend.UI.Admin.Components @using Moonlight.Frontend.UI.Admin.Components
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.ApiKeys @using Moonlight.Shared.Http.Requests.Admin.ApiKeys
@using Moonlight.Shared.Http.Responses @using Moonlight.Shared.Http.Responses
@using ShadcnBlazor.Dialogs @using ShadcnBlazor.Dialogs
@@ -77,7 +78,7 @@
var response = await HttpClient.PostAsJsonAsync( var response = await HttpClient.PostAsJsonAsync(
"/api/admin/apiKeys", "/api/admin/apiKeys",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -1,5 +1,6 @@
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Frontend.UI.Admin.Components @using Moonlight.Frontend.UI.Admin.Components
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.Roles @using Moonlight.Shared.Http.Requests.Admin.Roles
@using ShadcnBlazor.Dialogs @using ShadcnBlazor.Dialogs
@using ShadcnBlazor.Extras.Forms @using ShadcnBlazor.Extras.Forms
@@ -76,7 +77,7 @@
var response = await HttpClient.PostAsJsonAsync( var response = await HttpClient.PostAsJsonAsync(
"api/admin/roles", "api/admin/roles",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -1,4 +1,5 @@
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.Users @using Moonlight.Shared.Http.Requests.Admin.Users
@using Moonlight.Shared.Http.Responses @using Moonlight.Shared.Http.Responses
@using ShadcnBlazor.Dialogs @using ShadcnBlazor.Dialogs
@@ -65,7 +66,7 @@
var response = await HttpClient.PostAsJsonAsync( var response = await HttpClient.PostAsJsonAsync(
"/api/admin/users", "/api/admin/users",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -1,6 +1,7 @@
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Frontend.Mappers @using Moonlight.Frontend.Mappers
@using Moonlight.Frontend.UI.Admin.Components @using Moonlight.Frontend.UI.Admin.Components
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.ApiKeys @using Moonlight.Shared.Http.Requests.Admin.ApiKeys
@using Moonlight.Shared.Http.Responses.Admin.ApiKeys @using Moonlight.Shared.Http.Responses.Admin.ApiKeys
@using ShadcnBlazor.Dialogs @using ShadcnBlazor.Dialogs
@@ -74,7 +75,7 @@
var response = await HttpClient.PatchAsJsonAsync( var response = await HttpClient.PatchAsJsonAsync(
$"/api/admin/apiKeys/{Key.Id}", $"/api/admin/apiKeys/{Key.Id}",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -124,7 +124,7 @@ else
await HttpClient.PostAsJsonAsync("api/admin/ch/version", new SetVersionDto() await HttpClient.PostAsJsonAsync("api/admin/ch/version", new SetVersionDto()
{ {
Version = Version Version = Version
}, SerializationContext.TunedOptions); }, SerializationContext.Default.Options);
// Starting rebuild task // Starting rebuild task
CurrentStep = 2; CurrentStep = 2;
@@ -136,7 +136,7 @@ else
request.Content = JsonContent.Create( request.Content = JsonContent.Create(
new RequestRebuildDto(NoBuildCache), new RequestRebuildDto(NoBuildCache),
null, null,
SerializationContext.TunedOptions SerializationContext.Default.Options
); );
var response = await HttpClient.SendAsync( var response = await HttpClient.SendAsync(
@@ -160,7 +160,7 @@ else
continue; continue;
var data = line.Trim("data: "); var data = line.Trim("data: ");
var deserializedData = JsonSerializer.Deserialize<RebuildEventDto>(data, Constants.SerializerOptions); var deserializedData = JsonSerializer.Deserialize<RebuildEventDto>(data, SerializationContext.Default.Options);
switch (deserializedData.Type) switch (deserializedData.Type)
{ {

View File

@@ -1,6 +1,7 @@
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Frontend.Mappers @using Moonlight.Frontend.Mappers
@using Moonlight.Frontend.UI.Admin.Components @using Moonlight.Frontend.UI.Admin.Components
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.Roles @using Moonlight.Shared.Http.Requests.Admin.Roles
@using Moonlight.Shared.Http.Responses.Admin @using Moonlight.Shared.Http.Responses.Admin
@using ShadcnBlazor.Dialogs @using ShadcnBlazor.Dialogs
@@ -75,7 +76,7 @@
var response = await HttpClient.PatchAsJsonAsync( var response = await HttpClient.PatchAsJsonAsync(
$"api/admin/roles/{Role.Id}", $"api/admin/roles/{Role.Id}",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -1,6 +1,7 @@
@page "/admin" @page "/admin"
@using LucideBlazor @using LucideBlazor
@using Moonlight.Frontend.UI.Admin.Modals @using Moonlight.Frontend.UI.Admin.Modals
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Responses.Admin @using Moonlight.Shared.Http.Responses.Admin
@using ShadcnBlazor.Buttons @using ShadcnBlazor.Buttons
@using ShadcnBlazor.Cards @using ShadcnBlazor.Cards
@@ -155,7 +156,7 @@
if(!firstRender) if(!firstRender)
return; return;
InfoResponse = await HttpClient.GetFromJsonAsync<SystemInfoDto>("api/admin/system/info", Constants.SerializerOptions); InfoResponse = await HttpClient.GetFromJsonAsync<SystemInfoDto>("api/admin/system/info", SerializationContext.Default.Options);
IsInfoLoading = false; IsInfoLoading = false;
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);

View File

@@ -3,6 +3,7 @@
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@using Moonlight.Frontend.UI.Admin.Modals @using Moonlight.Frontend.UI.Admin.Modals
@using Moonlight.Shared @using Moonlight.Shared
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests @using Moonlight.Shared.Http.Requests
@using Moonlight.Shared.Http.Responses @using Moonlight.Shared.Http.Responses
@using Moonlight.Shared.Http.Responses.Admin.ApiKeys @using Moonlight.Shared.Http.Responses.Admin.ApiKeys
@@ -126,7 +127,7 @@
var response = await HttpClient.GetFromJsonAsync<PagedData<ApiKeyDto>>( var response = await HttpClient.GetFromJsonAsync<PagedData<ApiKeyDto>>(
$"api/admin/apiKeys{query}&filterOptions={filterOptions}", $"api/admin/apiKeys{query}&filterOptions={filterOptions}",
Constants.SerializerOptions SerializationContext.Default.Options
); );
return new DataGridResponse<ApiKeyDto>(response!.Data, response.TotalLength); return new DataGridResponse<ApiKeyDto>(response!.Data, response.TotalLength);

View File

@@ -5,6 +5,7 @@
@using LucideBlazor @using LucideBlazor
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Frontend.Services @using Moonlight.Frontend.Services
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.Themes @using Moonlight.Shared.Http.Requests.Admin.Themes
@using ShadcnBlazor.Buttons @using ShadcnBlazor.Buttons
@using ShadcnBlazor.Cards @using ShadcnBlazor.Cards
@@ -122,7 +123,7 @@
var response = await HttpClient.PostAsJsonAsync( var response = await HttpClient.PostAsJsonAsync(
"/api/admin/themes", "/api/admin/themes",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -2,6 +2,7 @@
@using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@using Moonlight.Shared @using Moonlight.Shared
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests @using Moonlight.Shared.Http.Requests
@using Moonlight.Shared.Http.Responses @using Moonlight.Shared.Http.Responses
@using Moonlight.Shared.Http.Responses.Admin.Themes @using Moonlight.Shared.Http.Responses.Admin.Themes
@@ -136,7 +137,7 @@
var response = await HttpClient.GetFromJsonAsync<PagedData<ThemeDto>>( var response = await HttpClient.GetFromJsonAsync<PagedData<ThemeDto>>(
$"api/admin/themes{query}&filterOptions={filterOptions}", $"api/admin/themes{query}&filterOptions={filterOptions}",
Constants.SerializerOptions SerializationContext.Default.Options
); );
return new DataGridResponse<ThemeDto>(response!.Data, response.TotalLength); return new DataGridResponse<ThemeDto>(response!.Data, response.TotalLength);
@@ -182,7 +183,7 @@
var importedTheme = await response var importedTheme = await response
.Content .Content
.ReadFromJsonAsync<ThemeDto>(Constants.SerializerOptions); .ReadFromJsonAsync<ThemeDto>(SerializationContext.Default.Options);
if (importedTheme == null) if (importedTheme == null)
continue; continue;

View File

@@ -6,6 +6,7 @@
@using Moonlight.Frontend.Helpers @using Moonlight.Frontend.Helpers
@using Moonlight.Frontend.Mappers @using Moonlight.Frontend.Mappers
@using Moonlight.Frontend.Services @using Moonlight.Frontend.Services
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests.Admin.Themes @using Moonlight.Shared.Http.Requests.Admin.Themes
@using Moonlight.Shared.Http.Responses.Admin.Themes @using Moonlight.Shared.Http.Responses.Admin.Themes
@using ShadcnBlazor.Buttons @using ShadcnBlazor.Buttons
@@ -136,7 +137,7 @@
var response = await HttpClient.PatchAsJsonAsync( var response = await HttpClient.PatchAsJsonAsync(
$"/api/admin/themes/{Theme.Id}", $"/api/admin/themes/{Theme.Id}",
Request, Request,
Constants.SerializerOptions SerializationContext.Default.Options
); );
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -3,6 +3,7 @@
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@using Moonlight.Frontend.UI.Admin.Modals @using Moonlight.Frontend.UI.Admin.Modals
@using Moonlight.Shared @using Moonlight.Shared
@using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Requests @using Moonlight.Shared.Http.Requests
@using Moonlight.Shared.Http.Responses @using Moonlight.Shared.Http.Responses
@using Moonlight.Shared.Http.Responses.Admin @using Moonlight.Shared.Http.Responses.Admin
@@ -123,7 +124,7 @@
var response = await HttpClient.GetFromJsonAsync<PagedData<RoleDto>>( var response = await HttpClient.GetFromJsonAsync<PagedData<RoleDto>>(
$"api/admin/roles{query}&filterOptions={filterOptions}", $"api/admin/roles{query}&filterOptions={filterOptions}",
Constants.SerializerOptions SerializationContext.Default.Options
); );
return new DataGridResponse<RoleDto>(response!.Data, response.TotalLength); return new DataGridResponse<RoleDto>(response!.Data, response.TotalLength);

View File

@@ -3,6 +3,7 @@
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@using Moonlight.Frontend.UI.Admin.Modals @using Moonlight.Frontend.UI.Admin.Modals
@using Moonlight.Shared @using Moonlight.Shared
@using Moonlight.Shared.Http
@using ShadcnBlazor.Buttons @using ShadcnBlazor.Buttons
@using ShadcnBlazor.DataGrids @using ShadcnBlazor.DataGrids
@using ShadcnBlazor.Dropdowns @using ShadcnBlazor.Dropdowns
@@ -121,7 +122,7 @@
var response = await HttpClient.GetFromJsonAsync<PagedData<UserDto>>( var response = await HttpClient.GetFromJsonAsync<PagedData<UserDto>>(
$"api/admin/users{query}&filterOptions={filterOptions}", $"api/admin/users{query}&filterOptions={filterOptions}",
Constants.SerializerOptions SerializationContext.Default.Options
); );
return new DataGridResponse<UserDto>(response!.Data, response.TotalLength); return new DataGridResponse<UserDto>(response!.Data, response.TotalLength);

View File

@@ -1,4 +1,5 @@
@using Moonlight.Shared.Http.Responses.Admin.Auth @using Moonlight.Shared.Http
@using Moonlight.Shared.Http.Responses.Admin.Auth
@using ShadcnBlazor.Cards @using ShadcnBlazor.Cards
@using ShadcnBlazor.Spinners @using ShadcnBlazor.Spinners
@using ShadcnBlazor.Buttons @using ShadcnBlazor.Buttons
@@ -48,7 +49,7 @@
return; return;
var schemes = await HttpClient.GetFromJsonAsync<SchemeDto[]>( var schemes = await HttpClient.GetFromJsonAsync<SchemeDto[]>(
"api/auth", Constants.SerializerOptions "api/auth", SerializationContext.Default.Options
); );
if (schemes == null) if (schemes == null)

View File

@@ -58,21 +58,9 @@ namespace Moonlight.Shared.Http;
//Misc //Misc
[JsonSerializable(typeof(VersionDto))] [JsonSerializable(typeof(VersionDto))]
[JsonSerializable(typeof(ProblemDetails))] [JsonSerializable(typeof(ProblemDetails))]
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
public partial class SerializationContext : JsonSerializerContext public partial class SerializationContext : JsonSerializerContext
{ {
private static JsonSerializerOptions? InternalTunedOptions;
public static JsonSerializerOptions TunedOptions
{
get
{
if (InternalTunedOptions != null)
return InternalTunedOptions;
InternalTunedOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
InternalTunedOptions.TypeInfoResolverChain.Add(Default);
return InternalTunedOptions;
}
}
} }