Questionable/Questionable.Model/Questing/Converter/QuestWorkConfigConverter.cs

64 lines
2.0 KiB
C#
Raw Permalink Normal View History

2024-07-20 17:09:51 +00:00
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
2024-08-02 16:30:21 +00:00
namespace Questionable.Model.Questing.Converter;
2024-07-20 17:09:51 +00:00
public sealed class QuestWorkConfigConverter : JsonConverter<QuestWorkValue>
{
public override QuestWorkValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
return new QuestWorkValue(reader.GetByte());
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException();
byte? high = null, low = null;
EQuestWorkMode mode = EQuestWorkMode.Bitwise;
2024-07-20 17:09:51 +00:00
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonTokenType.PropertyName:
string? propertyName = reader.GetString();
if (propertyName == null || !reader.Read())
throw new JsonException();
switch (propertyName)
{
case nameof(QuestWorkValue.High):
high = reader.GetByte();
break;
case nameof(QuestWorkValue.Low):
low = reader.GetByte();
break;
case nameof(QuestWorkValue.Mode):
mode = new QuestWorkModeConverter().Read(ref reader, typeof(EQuestWorkMode), options);
break;
2024-07-20 17:09:51 +00:00
default:
throw new JsonException();
}
break;
case JsonTokenType.EndObject:
return new QuestWorkValue(high, low, mode);
2024-07-20 17:09:51 +00:00
default:
throw new JsonException();
}
}
throw new JsonException();
}
public override void Write(Utf8JsonWriter writer, QuestWorkValue value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}