♻️ Refactor the embeddable to dictionary
This commit is contained in:
@@ -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,
|
||||||
|
@@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user