Defined a JSON converter for the EncryptedText type

This commit is contained in:
Thorsten Sommer 2024-08-28 20:59:30 +02:00
parent a95cfc5ac8
commit 273376ad97
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 29 additions and 0 deletions

View File

@ -1,7 +1,9 @@
using System.Security;
using System.Text.Json.Serialization;
namespace AIStudio.Tools;
[JsonConverter(typeof(EncryptedTextJsonConverter))]
public readonly record struct EncryptedText(string EncryptedData)
{
public EncryptedText() : this(string.Empty)

View File

@ -0,0 +1,27 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AIStudio.Tools;
public sealed class EncryptedTextJsonConverter : JsonConverter<EncryptedText>
{
#region Overrides of JsonConverter<EncryptedText>
public override EncryptedText Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType is JsonTokenType.String)
{
var value = reader.GetString()!;
return new EncryptedText(value);
}
throw new JsonException($"Unexpected token type when parsing EncryptedText. Expected {JsonTokenType.String}, but got {reader.TokenType}.");
}
public override void Write(Utf8JsonWriter writer, EncryptedText value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.EncryptedData);
}
#endregion
}