From ebbe14f2938d0f3c44dee9c4a28ed02a7b3562df Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 6 Aug 2025 14:32:18 +0800 Subject: [PATCH] :recycle: Refactor the embeddable to dictionary --- DysonNetwork.Shared/Proto/GrpcTypeHelper.cs | 2 +- DysonNetwork.Sphere/WebReader/EmbeddableBase.cs | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/DysonNetwork.Shared/Proto/GrpcTypeHelper.cs b/DysonNetwork.Shared/Proto/GrpcTypeHelper.cs index 3210342..03f2d53 100644 --- a/DysonNetwork.Shared/Proto/GrpcTypeHelper.cs +++ b/DysonNetwork.Shared/Proto/GrpcTypeHelper.cs @@ -14,7 +14,7 @@ namespace DysonNetwork.Shared.Proto; public abstract class GrpcTypeHelper { - private static readonly JsonSerializerOptions? SerializerOptions = new JsonSerializerOptions() + public static readonly JsonSerializerOptions? SerializerOptions = new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, DefaultIgnoreCondition = JsonIgnoreCondition.Never, diff --git a/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs b/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs index d1a499a..6854a2f 100644 --- a/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs +++ b/DysonNetwork.Sphere/WebReader/EmbeddableBase.cs @@ -1,5 +1,7 @@ using System.Reflection; +using System.Text.Json; using System.Text.Json.Serialization; +using DysonNetwork.Shared.Proto; namespace DysonNetwork.Sphere.WebReader; @@ -29,16 +31,7 @@ public abstract class EmbeddableBase public Dictionary ToDictionary() { - var dict = new Dictionary(); - foreach (var prop in GetType().GetProperties()) - { - if (prop.GetCustomAttribute() is not null) - continue; - var value = prop.GetValue(this); - if (value is null) continue; - dict[prop.Name] = value; - } - - return dict; + var jsonRaw = JsonSerializer.Serialize(this, GrpcTypeHelper.SerializerOptions); + return JsonSerializer.Deserialize>(jsonRaw, GrpcTypeHelper.SerializerOptions) ?? []; } } \ No newline at end of file