♻️ Refactor the embeddable to dictionary

This commit is contained in:
2025-08-06 14:32:18 +08:00
parent 681934a0dc
commit ebbe14f293
2 changed files with 5 additions and 12 deletions

View File

@@ -14,7 +14,7 @@ namespace DysonNetwork.Shared.Proto;
public abstract class GrpcTypeHelper public abstract class GrpcTypeHelper
{ {
private static readonly JsonSerializerOptions? SerializerOptions = new JsonSerializerOptions() public static readonly JsonSerializerOptions? SerializerOptions = new JsonSerializerOptions()
{ {
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DefaultIgnoreCondition = JsonIgnoreCondition.Never, DefaultIgnoreCondition = JsonIgnoreCondition.Never,

View File

@@ -1,5 +1,7 @@
using System.Reflection; using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using DysonNetwork.Shared.Proto;
namespace DysonNetwork.Sphere.WebReader; namespace DysonNetwork.Sphere.WebReader;
@@ -29,16 +31,7 @@ public abstract class EmbeddableBase
public Dictionary<string, object> ToDictionary() public Dictionary<string, object> ToDictionary()
{ {
var dict = new Dictionary<string, object>(); var jsonRaw = JsonSerializer.Serialize(this, GrpcTypeHelper.SerializerOptions);
foreach (var prop in GetType().GetProperties()) return JsonSerializer.Deserialize<Dictionary<string, object>>(jsonRaw, GrpcTypeHelper.SerializerOptions) ?? [];
{
if (prop.GetCustomAttribute<JsonIgnoreAttribute>() is not null)
continue;
var value = prop.GetValue(this);
if (value is null) continue;
dict[prop.Name] = value;
}
return dict;
} }
} }