Questionable/Questionable.Model/Questing/Converter/AethernetShortcutConverter.cs

50 lines
1.8 KiB
C#
Raw Normal View History

2024-05-26 13:43:33 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
2024-08-02 16:30:21 +00:00
using Questionable.Model.Common;
2024-05-26 13:43:33 +00:00
2024-08-02 16:30:21 +00:00
namespace Questionable.Model.Questing.Converter;
2024-05-26 13:43:33 +00:00
2024-06-14 09:37:33 +00:00
public sealed class AethernetShortcutConverter : JsonConverter<AethernetShortcut>
2024-05-26 13:43:33 +00:00
{
private static readonly Dictionary<EAetheryteLocation, string> EnumToString = AethernetShardConverter.Values;
2024-05-26 13:43:33 +00:00
private static readonly Dictionary<string, EAetheryteLocation> StringToEnum =
EnumToString.ToDictionary(x => x.Value, x => x.Key);
2024-05-26 19:45:26 +00:00
public override AethernetShortcut Read(ref Utf8JsonReader reader, Type typeToConvert,
2024-05-26 13:43:33 +00:00
JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
if (!reader.Read() || reader.TokenType != JsonTokenType.String)
throw new JsonException();
string from = reader.GetString() ?? throw new JsonException();
if (!reader.Read() || reader.TokenType != JsonTokenType.String)
throw new JsonException();
string to = reader.GetString() ?? throw new JsonException();
if (!reader.Read() || reader.TokenType != JsonTokenType.EndArray)
throw new JsonException();
return new AethernetShortcut
{
From = StringToEnum.TryGetValue(from, out var fromEnum) ? fromEnum : throw new JsonException(),
To = StringToEnum.TryGetValue(to, out var toEnum) ? toEnum : throw new JsonException()
};
}
public override void Write(Utf8JsonWriter writer, AethernetShortcut value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteStringValue(EnumToString[value.From]);
writer.WriteStringValue(EnumToString[value.To]);
writer.WriteEndArray();
}
}