diff --git a/app/MindWork AI Studio/Settings/DataModel/MetadataJsonConverter.cs b/app/MindWork AI Studio/Settings/DataModel/MetadataJsonConverter.cs index f800ef8a..66d1be39 100644 --- a/app/MindWork AI Studio/Settings/DataModel/MetadataJsonConverter.cs +++ b/app/MindWork AI Studio/Settings/DataModel/MetadataJsonConverter.cs @@ -5,47 +5,33 @@ namespace AIStudio.Settings.DataModel; public class MetadataJsonConverter : JsonConverter { + private static readonly Dictionary 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(); - using (var jsonDoc = JsonDocument.ParseValue(ref reader)) + var propertyName = root.EnumerateObject() + .Select(p => p.Name) + .FirstOrDefault(name => TYPE_MAP.ContainsKey(name)); + + if (propertyName != null && TYPE_MAP.TryGetValue(propertyName, out var metadataType)) { - var root = jsonDoc.RootElement; - - if (root.TryGetProperty("Text", out _)) - { - return JsonSerializer.Deserialize(root.GetRawText(), options); - } - else if (root.TryGetProperty("Pdf", out _)) - { - return JsonSerializer.Deserialize(root.GetRawText(), options); - } - else if (root.TryGetProperty("Spreadsheet", out _)) - { - return JsonSerializer.Deserialize(root.GetRawText(), options); - } - else if (root.TryGetProperty("Presentation", out _)) - { - return JsonSerializer.Deserialize(root.GetRawText(), options); - } - else if (root.TryGetProperty("Image", out _)) - { - return JsonSerializer.Deserialize(root.GetRawText(), options); - } - else if (root.TryGetProperty("Document", out _)) - { - return JsonSerializer.Deserialize(root.GetRawText(), options); - } - else - { - // Unbekannter Typ - return null; - } + return (Metadata?)JsonSerializer.Deserialize(rawText, metadataType, options); } + + return null; } - public override void Write(Utf8JsonWriter writer, Metadata value, JsonSerializerOptions options) - { - throw new NotImplementedException(); - } + public override void Write(Utf8JsonWriter writer, Metadata value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, value.GetType(), options); } \ No newline at end of file diff --git a/app/MindWork AI Studio/Settings/DataModel/SseEvent.cs b/app/MindWork AI Studio/Settings/DataModel/SseEvent.cs index 391df2d5..a1aa4295 100644 --- a/app/MindWork AI Studio/Settings/DataModel/SseEvent.cs +++ b/app/MindWork AI Studio/Settings/DataModel/SseEvent.cs @@ -5,10 +5,10 @@ namespace AIStudio.Settings.DataModel; public class SseEvent { [JsonPropertyName("content")] - public string Content { get; set; } + public string? Content { get; set; } [JsonPropertyName("metadata")] - public Metadata Metadata { get; set; } + public Metadata? Metadata { get; set; } } [JsonConverter(typeof(MetadataJsonConverter))] @@ -17,75 +17,71 @@ public abstract class Metadata; public class TextMetadata : Metadata { [JsonPropertyName("Text")] - public TextDetails Text { get; set; } + public TextDetails? Text { get; set; } } public class TextDetails { [JsonPropertyName("line_number")] - public int LineNumber { get; set; } + public int? LineNumber { get; set; } } public class PdfMetadata : Metadata { [JsonPropertyName("Pdf")] - public PdfDetails Pdf { get; set; } + public PdfDetails? Pdf { get; set; } } public class PdfDetails { [JsonPropertyName("page_number")] - public int PageNumber { get; set; } + public int? PageNumber { get; set; } } public class SpreadsheetMetadata : Metadata { [JsonPropertyName("Spreadsheet")] - public SpreadsheetDetails Spreadsheet { get; set; } + public SpreadsheetDetails? Spreadsheet { get; set; } } public class SpreadsheetDetails { [JsonPropertyName("sheet_name")] - public string SheetName { get; set; } + public string? SheetName { get; set; } [JsonPropertyName("row_number")] - public int RowNumber { get; set; } + public int? RowNumber { get; set; } } public class DocumentMetadata : Metadata {} +public class ImageMetadata: Metadata {} + public class PresentationMetadata : Metadata { [JsonPropertyName("Presentation")] - public PresentationDetails Presentation { get; set; } + public PresentationDetails? Presentation { get; set; } } public class PresentationDetails { [JsonPropertyName("slide_number")] - public int SlideNumber { get; set; } + public int? SlideNumber { get; set; } [JsonPropertyName("image")] - public ImageData Image { get; set; } + public PptxImageData? Image { get; set; } } -public class ImageMetadata : Metadata -{ - [JsonPropertyName("Image")] - public object Image { get; set; } -} - -public class ImageData +public class PptxImageData { [JsonPropertyName("id")] - public string Id { get; set; } + public string? Id { get; set; } [JsonPropertyName("content")] - public string Content { get; set; } + public string? Content { get; set; } [JsonPropertyName("segment")] - public int Segment { get; set; } + public int? Segment { get; set; } [JsonPropertyName("is_end")] public bool IsEnd { get; set; }