PluralKit/Myriad/Serialization/ShardInfoJsonConverter.cs

26 lines
812 B
C#
Raw Normal View History

2020-12-22 12:15:26 +00:00
using System.Text.Json;
using System.Text.Json.Serialization;
using Myriad.Gateway;
namespace Myriad.Serialization;
public class ShardInfoJsonConverter: JsonConverter<ShardInfo>
2020-12-22 12:15:26 +00:00
{
public override ShardInfo? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2020-12-22 12:15:26 +00:00
{
var arr = JsonSerializer.Deserialize<int[]>(ref reader);
if (arr?.Length != 2)
throw new JsonException("Expected shard info as array of length 2");
2020-12-22 12:15:26 +00:00
return new ShardInfo(arr[0], arr[1]);
}
2020-12-22 12:15:26 +00:00
public override void Write(Utf8JsonWriter writer, ShardInfo value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteNumberValue(value.ShardId);
writer.WriteNumberValue(value.NumShards);
writer.WriteEndArray();
2020-12-22 12:15:26 +00:00
}
}