mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-07-28 12:02:57 +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>
|
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)
|
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;
|
return (Metadata?)JsonSerializer.Deserialize(rawText, metadataType, options);
|
||||||
|
|
||||||
if (root.TryGetProperty("Text", out _))
|
|
||||||
{
|
|
||||||
return JsonSerializer.Deserialize<TextMetadata>(root.GetRawText(), 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, Metadata value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, Metadata value, JsonSerializerOptions options) => JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -5,10 +5,10 @@ namespace AIStudio.Settings.DataModel;
|
|||||||
public class SseEvent
|
public class SseEvent
|
||||||
{
|
{
|
||||||
[JsonPropertyName("content")]
|
[JsonPropertyName("content")]
|
||||||
public string Content { get; set; }
|
public string? Content { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("metadata")]
|
[JsonPropertyName("metadata")]
|
||||||
public Metadata Metadata { get; set; }
|
public Metadata? Metadata { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(MetadataJsonConverter))]
|
[JsonConverter(typeof(MetadataJsonConverter))]
|
||||||
@ -17,75 +17,71 @@ public abstract class Metadata;
|
|||||||
public class TextMetadata : Metadata
|
public class TextMetadata : Metadata
|
||||||
{
|
{
|
||||||
[JsonPropertyName("Text")]
|
[JsonPropertyName("Text")]
|
||||||
public TextDetails Text { get; set; }
|
public TextDetails? Text { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TextDetails
|
public class TextDetails
|
||||||
{
|
{
|
||||||
[JsonPropertyName("line_number")]
|
[JsonPropertyName("line_number")]
|
||||||
public int LineNumber { get; set; }
|
public int? LineNumber { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PdfMetadata : Metadata
|
public class PdfMetadata : Metadata
|
||||||
{
|
{
|
||||||
[JsonPropertyName("Pdf")]
|
[JsonPropertyName("Pdf")]
|
||||||
public PdfDetails Pdf { get; set; }
|
public PdfDetails? Pdf { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PdfDetails
|
public class PdfDetails
|
||||||
{
|
{
|
||||||
[JsonPropertyName("page_number")]
|
[JsonPropertyName("page_number")]
|
||||||
public int PageNumber { get; set; }
|
public int? PageNumber { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SpreadsheetMetadata : Metadata
|
public class SpreadsheetMetadata : Metadata
|
||||||
{
|
{
|
||||||
[JsonPropertyName("Spreadsheet")]
|
[JsonPropertyName("Spreadsheet")]
|
||||||
public SpreadsheetDetails Spreadsheet { get; set; }
|
public SpreadsheetDetails? Spreadsheet { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SpreadsheetDetails
|
public class SpreadsheetDetails
|
||||||
{
|
{
|
||||||
[JsonPropertyName("sheet_name")]
|
[JsonPropertyName("sheet_name")]
|
||||||
public string SheetName { get; set; }
|
public string? SheetName { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("row_number")]
|
[JsonPropertyName("row_number")]
|
||||||
public int RowNumber { get; set; }
|
public int? RowNumber { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DocumentMetadata : Metadata {}
|
public class DocumentMetadata : Metadata {}
|
||||||
|
|
||||||
|
public class ImageMetadata: Metadata {}
|
||||||
|
|
||||||
public class PresentationMetadata : Metadata
|
public class PresentationMetadata : Metadata
|
||||||
{
|
{
|
||||||
[JsonPropertyName("Presentation")]
|
[JsonPropertyName("Presentation")]
|
||||||
public PresentationDetails Presentation { get; set; }
|
public PresentationDetails? Presentation { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PresentationDetails
|
public class PresentationDetails
|
||||||
{
|
{
|
||||||
[JsonPropertyName("slide_number")]
|
[JsonPropertyName("slide_number")]
|
||||||
public int SlideNumber { get; set; }
|
public int? SlideNumber { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("image")]
|
[JsonPropertyName("image")]
|
||||||
public ImageData Image { get; set; }
|
public PptxImageData? Image { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ImageMetadata : Metadata
|
public class PptxImageData
|
||||||
{
|
|
||||||
[JsonPropertyName("Image")]
|
|
||||||
public object Image { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ImageData
|
|
||||||
{
|
{
|
||||||
[JsonPropertyName("id")]
|
[JsonPropertyName("id")]
|
||||||
public string Id { get; set; }
|
public string? Id { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("content")]
|
[JsonPropertyName("content")]
|
||||||
public string Content { get; set; }
|
public string? Content { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("segment")]
|
[JsonPropertyName("segment")]
|
||||||
public int Segment { get; set; }
|
public int? Segment { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("is_end")]
|
[JsonPropertyName("is_end")]
|
||||||
public bool IsEnd { get; set; }
|
public bool IsEnd { get; set; }
|
||||||
|
Loading…
Reference in New Issue
Block a user