mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-07-28 06:22:56 +00:00
null safety and optimizations
This commit is contained in:
parent
5e74454bc8
commit
04543540f8
@ -5,47 +5,33 @@ 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))
|
||||
{
|
||||
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
||||
var root = jsonDoc.RootElement;
|
||||
var rawText = root.GetRawText();
|
||||
|
||||
if (root.TryGetProperty("Text", out _))
|
||||
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 JsonSerializer.Deserialize<TextMetadata>(root.GetRawText(), options);
|
||||
return (Metadata?)JsonSerializer.Deserialize(rawText, metadataType, options);
|
||||
}
|
||||
else if (root.TryGetProperty("Pdf", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<PdfMetadata>(root.GetRawText(), options);
|
||||
}
|
||||
else if (root.TryGetProperty("Spreadsheet", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<SpreadsheetMetadata>(root.GetRawText(), options);
|
||||
}
|
||||
else if (root.TryGetProperty("Presentation", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<PresentationMetadata>(root.GetRawText(), options);
|
||||
}
|
||||
else if (root.TryGetProperty("Image", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<ImageMetadata>(root.GetRawText(), options);
|
||||
}
|
||||
else if (root.TryGetProperty("Document", out _))
|
||||
{
|
||||
return JsonSerializer.Deserialize<DocumentMetadata>(root.GetRawText(), options);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unbekannter Typ
|
||||
|
||||
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);
|
||||
}
|
@ -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; }
|
||||
|
Loading…
Reference in New Issue
Block a user