2024-04-16 18:29:40 +00:00
|
|
|
using System.Text.Json;
|
2023-08-12 19:08:38 +01:00
|
|
|
using System.Text.Json.Serialization;
|
2023-08-13 17:26:49 +01:00
|
|
|
using LootDumpProcessor.Storage;
|
2023-08-12 19:08:38 +01:00
|
|
|
|
2023-08-13 17:26:49 +01:00
|
|
|
namespace LootDumpProcessor.Serializers.Json.Converters;
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
public class NetJsonKeyConverter : JsonConverter<IKey?>
|
|
|
|
{
|
|
|
|
public override IKey? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
{
|
|
|
|
Dictionary<string, string> values = new Dictionary<string, string>();
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
|
|
|
var property = reader.GetString() ?? "";
|
|
|
|
reader.Read();
|
|
|
|
var value = reader.GetString() ?? "";
|
|
|
|
values.Add(property, value);
|
|
|
|
if (values.Count == 2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
if (!values.TryGetValue("type", out var type))
|
|
|
|
{
|
|
|
|
throw new Exception("Key type was missing from json definition");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!values.TryGetValue("serializedKey", out var serializedKey))
|
|
|
|
{
|
|
|
|
throw new Exception("Key serializedKey was missing from json definition");
|
|
|
|
}
|
|
|
|
|
2024-04-16 18:29:40 +00:00
|
|
|
AbstractKey key = Enum.Parse<KeyType>(type) switch
|
2023-08-12 19:08:38 +01:00
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
KeyType.Subdivisioned => new SubdivisionedUniqueKey(serializedKey.Split("|")),
|
|
|
|
KeyType.Unique => new FlatUniqueKey(serializedKey.Split("|")),
|
|
|
|
_ => throw new Exception("Unknown key type used!")
|
|
|
|
};
|
2023-08-12 19:08:38 +01:00
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, IKey? value, JsonSerializerOptions options)
|
|
|
|
{
|
|
|
|
if (value != null)
|
|
|
|
{
|
|
|
|
var actualValue = (AbstractKey)value;
|
|
|
|
writer.WriteStartObject();
|
|
|
|
// writer.WritePropertyName("type");
|
|
|
|
writer.WriteString("type", actualValue.GetKeyType().ToString());
|
|
|
|
// writer.WritePropertyName("serializedKey");
|
|
|
|
writer.WriteString("serializedKey", actualValue.SerializedKey);
|
|
|
|
writer.WriteEndObject();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writer.WriteStartObject();
|
|
|
|
writer.WriteEndObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|