mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-10 01:13:47 +00:00
Defined a JSON converter for the EncryptedText type
This commit is contained in:
2 files changed
+29
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user