wabbajack/Wabbajack.DTOs/JsonConverters/HashJsonConverter.cs

21 lines
637 B
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Wabbajack.Hashing.xxHash64;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.DTOs.JsonConverters;
public class HashJsonConverter : JsonConverter<Hash>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public override Hash Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
return Hash.FromBase64(reader.ValueSpan);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override void Write(Utf8JsonWriter writer, Hash value, JsonSerializerOptions options)
{
Span<byte> data = stackalloc byte[8];
BitConverter.TryWriteBytes(data, (ulong) value);
writer.WriteBase64StringValue(data);
2021-09-27 12:42:46 +00:00
}
}