0
0
mirror of https://github.com/sp-tarkov/loot-dump-processor.git synced 2025-02-13 09:50:44 -05:00
bluextx 465ad95cb5 Refactored code to use System.Text.Json and improved code style
The changes include:
- Replaced Newtonsoft.Json with System.Text.Json for serialization
- Removed redundant JsonProperty attributes and simplified model classes
- Converted static fields to constants where appropriate
- Improved code formatting and readability
- Simplified method bodies and expressions
- Removed unused imports and cleaned up namespaces
2025-01-11 10:52:23 +03:00

57 lines
1.9 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
using LootDumpProcessor.Storage;
namespace LootDumpProcessor.Serializers.Json.Converters;
public class NetJsonKeyConverter : JsonConverter<IKey?>
{
public override IKey? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Dictionary<string, string> values = new();
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");
AbstractKey key = Enum.Parse<KeyType>(type) switch
{
KeyType.Subdivisioned => new SubdivisionedUniqueKey(serializedKey.Split("|")),
KeyType.Unique => new FlatUniqueKey(serializedKey.Split("|")),
_ => throw new Exception("Unknown key type used!")
};
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();
}
}
}