Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepClone: Use sourcegenerator to fix trimming issues #32

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Components;
using MudBlazor.State;
using MudBlazor.ThemeManager.Extensions;
using System.Diagnostics.CodeAnalysis;

namespace MudBlazor.ThemeManager;

Expand Down Expand Up @@ -49,7 +48,6 @@ public MudThemeManager()
[Parameter]
public EventCallback<ThemeManagerTheme> ThemeChanged { get; set; }

[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
protected override void OnInitialized()
{
base.OnInitialized();
Expand Down
26 changes: 26 additions & 0 deletions src/MudBlazor.ThemeManager/Extensions/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace MudBlazor.ThemeManager.Extensions;
internal static class Extension
{
private static readonly JsonSerializerOptions JsonOptions = new();
private static readonly PaletteSerializerContext PaletteSerializerContext = new();

[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
public static T? DeepClone<T>(this T source)
Expand All @@ -19,4 +20,29 @@ internal static class Extension

return default;
}

// TODO: Needs this to be done https://github.com/MudBlazor/MudBlazor/pull/9434
//private static readonly ThemeSerializerContext ThemeSerializerContext = new();

//public static MudTheme? DeepClone(this MudTheme source)
//{
// var themeType = typeof(MudTheme);
// var serializeStr = JsonSerializer.Serialize(source, themeType, ThemeSerializerContext);
// var copyObj = (MudTheme?)JsonSerializer.Deserialize(serializeStr, themeType, ThemeSerializerContext);

// return copyObj;
//}

public static PaletteDark? DeepClone(this PaletteDark source) => DeepClonePalette(source);

public static PaletteLight? DeepClone(this PaletteLight source) => DeepClonePalette(source);

private static T? DeepClonePalette<T>(T source) where T : Palette
{
var paletteType = typeof(T);
var serializeStr = JsonSerializer.Serialize(source, paletteType, PaletteSerializerContext);
var copyObj = (T?)JsonSerializer.Deserialize(serializeStr, paletteType, PaletteSerializerContext);

return copyObj;
}
}
10 changes: 10 additions & 0 deletions src/MudBlazor.ThemeManager/Extensions/PaletteSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace MudBlazor.ThemeManager.Extensions;

[JsonSerializable(typeof(Palette))]
[JsonSerializable(typeof(PaletteDark))]
[JsonSerializable(typeof(PaletteLight))]
internal sealed partial class PaletteSerializerContext : JsonSerializerContext
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace MudBlazor.ThemeManager.Extensions;

//[JsonSerializable(typeof(MudTheme))]

//internal sealed partial class ThemeSerializerContext : JsonSerializerContext
//{
//}
Loading