continuity/Wonderking/Utils/ByteArrayConverter.cs
Timothy Schenk f5cd6c380e
Some checks failed
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 27s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / sbom-scan (push) Successful in 33s
Build, Package and Push Images / container-build (push) Failing after 1m10s
Build, Package and Push Images / container-sbom-scan (push) Has been skipped
feat: Itemdata pooling & benchmarks for storage options
2023-11-08 19:04:37 +01:00

36 lines
914 B
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace Wonderking.Utils;
public class ByteArrayConverter : JsonConverter<byte[]>
{
public override byte[] Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
var hexData = reader.GetString();
if (hexData != null)
{
return hexData.Split('-').Select(b => Convert.ToByte(b, 16)).ToArray();
}
throw new JsonException("Hex string is null.");
}
public override void Write(
Utf8JsonWriter writer,
byte[]? value,
JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else
{
var hexData = BitConverter.ToString(value).Replace("-", string.Empty);
writer.WriteStringValue(hexData);
}
}
}