Немного работы
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2eModel;
|
||||
using Pf2eModel.Model;
|
||||
using Pf2eModel.Model.Base;
|
||||
using Pf2eModel.Model.Common.I18n;
|
||||
using Pf2eModel.Model.Structure;
|
||||
|
||||
namespace Pf2eViewer.Converters;
|
||||
|
||||
internal class EntityConverter : JsonConverter
|
||||
{
|
||||
private readonly Pf2eDbContext _context = new();
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(Entity) || objectType.IsAssignableTo(typeof(IEntity));
|
||||
}
|
||||
|
||||
private Dictionary<string, object?> DictionarySerialization(object source)
|
||||
{
|
||||
Dictionary<string, object?> dictionary = [];
|
||||
|
||||
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
|
||||
{
|
||||
if (property.Attributes.OfType<JsonIgnoreAttribute>().Any())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
object? v = property.GetValue(source);
|
||||
dictionary.Add(property.Name, v);
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
Dictionary<string, object?> dictionary = DictionarySerialization(value!);
|
||||
|
||||
if (value.GetType() != typeof(Entity))
|
||||
{
|
||||
dictionary.Remove("Entity");
|
||||
}
|
||||
|
||||
switch (value)
|
||||
{
|
||||
case Entity entity:
|
||||
ProcessEntity(dictionary, entity);
|
||||
break;
|
||||
|
||||
case Trait trait:
|
||||
ProcessTrait(dictionary, trait);
|
||||
break;
|
||||
}
|
||||
|
||||
serializer.Serialize(writer, dictionary);
|
||||
|
||||
/*
|
||||
writer.WriteStartObject();
|
||||
|
||||
foreach (KeyValuePair<string, object> keyValuePair in dictionary)
|
||||
{
|
||||
writer.WritePropertyName(keyValuePair.Key, true);
|
||||
|
||||
serializer.Serialize(writer, keyValuePair.Value);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
*/
|
||||
}
|
||||
|
||||
private void ProcessTrait(Dictionary<string, object?> dictionary, Trait trait)
|
||||
{
|
||||
LocaleString name =
|
||||
trait.Entity?.Name
|
||||
?? _context.Entities.Where(e => e.Id == trait.Id).Select(e => e.Name).FirstOrDefault()!;
|
||||
|
||||
dictionary.Remove("Entity");
|
||||
dictionary.Remove("Entities");
|
||||
dictionary.Add("Name", name.ToString());
|
||||
}
|
||||
|
||||
private void ProcessEntity(Dictionary<string, object?> dictionary, Entity entity)
|
||||
{
|
||||
dictionary.Remove("Description");
|
||||
}
|
||||
|
||||
private void ProcessSubclass(Dictionary<string, object?> dictionary, Subclass entity)
|
||||
{
|
||||
dictionary.Remove("Group");
|
||||
}
|
||||
|
||||
public override bool CanRead => false;
|
||||
|
||||
public override object? ReadJson(
|
||||
JsonReader reader,
|
||||
Type objectType,
|
||||
object? existingValue,
|
||||
JsonSerializer serializer
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException(
|
||||
"Unnecessary because CanRead is false. The type will skip the converter."
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user