From 273376ad97f2b295e880dc5a178324e20a7bb1a5 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 28 Aug 2024 20:59:30 +0200 Subject: [PATCH] Defined a JSON converter for the EncryptedText type --- app/MindWork AI Studio/Tools/EncryptedText.cs | 2 ++ .../Tools/EncryptedTextJsonConverter.cs | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 app/MindWork AI Studio/Tools/EncryptedTextJsonConverter.cs diff --git a/app/MindWork AI Studio/Tools/EncryptedText.cs b/app/MindWork AI Studio/Tools/EncryptedText.cs index 2676980e..1c1c2ab1 100644 --- a/app/MindWork AI Studio/Tools/EncryptedText.cs +++ b/app/MindWork AI Studio/Tools/EncryptedText.cs @@ -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) diff --git a/app/MindWork AI Studio/Tools/EncryptedTextJsonConverter.cs b/app/MindWork AI Studio/Tools/EncryptedTextJsonConverter.cs new file mode 100644 index 00000000..43ddc681 --- /dev/null +++ b/app/MindWork AI Studio/Tools/EncryptedTextJsonConverter.cs @@ -0,0 +1,27 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace AIStudio.Tools; + +public sealed class EncryptedTextJsonConverter : JsonConverter +{ + #region Overrides of JsonConverter + + 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 +} \ No newline at end of file