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