mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-07-27 21:42:56 +00:00
Moved into tools namespace & optimized type handling
This commit is contained in:
parent
81e68b58a0
commit
70622492a9
@ -1,37 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AIStudio.Settings.DataModel;
|
||||
|
||||
public class MetadataJsonConverter : JsonConverter<Metadata>
|
||||
{
|
||||
private static readonly Dictionary<string, Type> TYPE_MAP = new()
|
||||
{
|
||||
{ "Text", typeof(TextMetadata) },
|
||||
{ "Pdf", typeof(PdfMetadata) },
|
||||
{ "Spreadsheet", typeof(SpreadsheetMetadata) },
|
||||
{ "Presentation", typeof(PresentationMetadata) },
|
||||
{ "Image", typeof(ImageMetadata) },
|
||||
{ "Document", typeof(DocumentMetadata) }
|
||||
};
|
||||
|
||||
public override Metadata? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
||||
var root = jsonDoc.RootElement;
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
var propertyName = root.EnumerateObject()
|
||||
.Select(p => p.Name)
|
||||
.FirstOrDefault(name => TYPE_MAP.ContainsKey(name));
|
||||
|
||||
if (propertyName != null && TYPE_MAP.TryGetValue(propertyName, out var metadataType))
|
||||
{
|
||||
return (Metadata?)JsonSerializer.Deserialize(rawText, metadataType, options);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Metadata value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
32
app/MindWork AI Studio/Tools/MetadataJsonConverter.cs
Normal file
32
app/MindWork AI Studio/Tools/MetadataJsonConverter.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
public class MetadataJsonConverter : JsonConverter<SseMetadata>
|
||||
{
|
||||
public override SseMetadata? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
||||
var root = jsonDoc.RootElement;
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
var propertyName = root.EnumerateObject()
|
||||
.Select(p => p.Name)
|
||||
.FirstOrDefault();
|
||||
|
||||
return propertyName switch
|
||||
{
|
||||
"Text" => JsonSerializer.Deserialize<TextMetadata?>(rawText, options),
|
||||
"Pdf" => JsonSerializer.Deserialize<PdfMetadata?>(rawText, options),
|
||||
"Spreadsheet" => JsonSerializer.Deserialize<SpreadsheetMetadata?>(rawText, options),
|
||||
"Presentation" => JsonSerializer.Deserialize<PresentationMetadata?>(rawText, options),
|
||||
"Image" => JsonSerializer.Deserialize<ImageMetadata?>(rawText, options),
|
||||
"Document" => JsonSerializer.Deserialize<DocumentMetadata?>(rawText, options),
|
||||
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, SseMetadata value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||
}
|
Loading…
Reference in New Issue
Block a user