Fixed enum serialization

This commit is contained in:
Alex
2025-01-16 15:35:26 +00:00
parent 088248706d
commit 0c08bac439
7 changed files with 137150 additions and 5 deletions
@@ -932,6 +932,7 @@ public class Props
[JsonPropertyName("effects_damage")]
[JsonConverter(typeof(ArrayToObjectFactoryConverter))]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public Dictionary<string, EffectDamageProps>? EffectsDamage { get; set; }
[JsonPropertyName("maximumNumberOfUsage")]
@@ -39,11 +39,9 @@ public class ArrayToObjectFactoryConverter : JsonConverterFactory
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
{
if (value == null)
JsonSerializer.Serialize(writer, null, options);
JsonSerializer.Serialize(writer, new List<object>(), options);
else
{
JsonSerializer.Serialize(writer, value, options);
}
}
}
}
+13 -1
View File
@@ -25,7 +25,19 @@ public class EftEnumConverter<T> : JsonConverter<T>
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, _options);
if (typeof(T).GetFields().Any(f => f.FieldType == typeof(string)))
{
JsonSerializer.Serialize(writer, value as string, _options);
}
else
{
if (typeof(T).GetFields().Any(f => f.FieldType == typeof(int)))
JsonSerializer.Serialize(writer, Convert.ToInt32(value), _options);
else if (typeof(T).GetFields().Any(f => f.FieldType == typeof(byte)))
JsonSerializer.Serialize(writer, Convert.ToByte(value), _options);
else
throw new Exception($"Could not convert enum {value.GetType()} with value {value}");
}
}
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)