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 { private static readonly Dictionary EnumToString = AethernetShardConverter.Values; private static readonly Dictionary StringToEnum = EnumToString.ToDictionary, string, EAetheryteLocation>((KeyValuePair x) => x.Value, (KeyValuePair 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(); } }