wabbajack/Wabbajack.DTOs/JsonConverters/HashRelativePathConverter.cs

33 lines
1.0 KiB
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Paths;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.DTOs.JsonConverters;
public class HashRelativePathConverter : JsonConverter<HashRelativePath>
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public override HashRelativePath Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var parts = JsonSerializer.Deserialize<string[]>(ref reader)!;
var hash = Hash.FromBase64(parts[0]);
return new HashRelativePath(hash,
(parts[1..] ?? Array.Empty<string>()).Select(r => (RelativePath) r).ToArray());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public override void Write(Utf8JsonWriter writer, HashRelativePath value, JsonSerializerOptions options)
{
writer.WriteStartArray();
Span<byte> temp = stackalloc byte[12];
value.Hash.ToBase64(temp);
writer.WriteStringValue(temp);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
foreach (var part in value.Parts)
writer.WriteStringValue(part.ToString());
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
writer.WriteEndArray();
2021-09-27 12:42:46 +00:00
}
}