Questionable/Questionable.Model/Questing/Converter/ExcelRefConverter.cs

31 lines
997 B
C#
Raw Permalink Normal View History

2024-06-12 16:03:48 +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-06-12 16:03:48 +00:00
2024-06-14 09:37:33 +00:00
public sealed class ExcelRefConverter : JsonConverter<ExcelRef>
2024-06-12 16:03:48 +00:00
{
public override ExcelRef? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
2024-07-16 08:54:47 +00:00
return reader.TokenType switch
{
JsonTokenType.String => ExcelRef.FromKey(reader.GetString()!),
JsonTokenType.Number => ExcelRef.FromRowId(reader.GetUInt32()),
_ => null
};
2024-06-12 16:03:48 +00:00
}
public override void Write(Utf8JsonWriter writer, ExcelRef? value, JsonSerializerOptions options)
{
if (value == null)
writer.WriteNullValue();
else if (value.Type == ExcelRef.EType.Key)
writer.WriteStringValue(value.AsKey());
else if (value.Type == ExcelRef.EType.RowId)
writer.WriteNumberValue(value.AsRowId());
else
throw new JsonException();
}
}