qstbak/Questionable.Model/Questionable.Model.Questing.Converter/AethernetShortcutConverter.cs
2025-10-09 07:47:19 +10:00

57 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Questionable.Model.Common;
namespace Questionable.Model.Questing.Converter;
public sealed class AethernetShortcutConverter : JsonConverter<AethernetShortcut>
{
private static readonly Dictionary<EAetheryteLocation, string> EnumToString = AethernetShardConverter.Values;
private static readonly Dictionary<string, EAetheryteLocation> StringToEnum = EnumToString.ToDictionary<KeyValuePair<EAetheryteLocation, string>, string, EAetheryteLocation>((KeyValuePair<EAetheryteLocation, string> x) => x.Value, (KeyValuePair<EAetheryteLocation, string> x) => x.Key);
public override AethernetShortcut Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
{
throw new JsonException();
}
if (!reader.Read() || reader.TokenType != JsonTokenType.String)
{
throw new JsonException();
}
string key = reader.GetString() ?? throw new JsonException();
if (!reader.Read() || reader.TokenType != JsonTokenType.String)
{
throw new JsonException();
}
string key2 = reader.GetString() ?? throw new JsonException();
if (!reader.Read() || reader.TokenType != JsonTokenType.EndArray)
{
throw new JsonException();
}
AethernetShortcut aethernetShortcut = new AethernetShortcut();
if (!StringToEnum.TryGetValue(key, out var value))
{
throw new JsonException();
}
aethernetShortcut.From = value;
if (!StringToEnum.TryGetValue(key2, out var value2))
{
throw new JsonException();
}
aethernetShortcut.To = value2;
return aethernetShortcut;
}
public override void Write(Utf8JsonWriter writer, AethernetShortcut value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteStringValue(EnumToString[value.From]);
writer.WriteStringValue(EnumToString[value.To]);
writer.WriteEndArray();
}
}