Skip to content
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
//{
//}