-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSourceConverter.cs
More file actions
59 lines (49 loc) · 1.93 KB
/
SourceConverter.cs
File metadata and controls
59 lines (49 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Text.Json;
using System.Text.Json.Serialization;
using AnthropicClient.Models;
namespace AnthropicClient.Json;
class SourceConverter : JsonConverter<Source>
{
public override Source Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var jsonDocument = JsonDocument.ParseValue(ref reader);
var root = jsonDocument.RootElement;
var type = root.GetProperty("type").GetString();
return type switch
{
SourceType.Text => JsonSerializer.Deserialize<TextSource>(root.GetRawText(), options)!,
SourceType.Content => JsonSerializer.Deserialize<CustomSource>(root.GetRawText(), options)!,
SourceType.Base64 => DeserializeBase64Source(root, options),
_ => throw new JsonException($"Unknown content type: {type}")
};
}
private static Source DeserializeBase64Source(JsonElement root, JsonSerializerOptions options)
{
var mediaType = root.TryGetProperty("media_type", out var mediaTypeElement)
? mediaTypeElement.GetString() ?? throw new JsonException("Missing 'media_type' property")
: throw new JsonException("Missing 'media_type' property");
var isImage = ImageType.IsValidImageType(mediaType);
return isImage
? JsonSerializer.Deserialize<ImageSource>(root.GetRawText(), options)!
: JsonSerializer.Deserialize<DocumentSource>(root.GetRawText(), options)!;
}
public override void Write(Utf8JsonWriter writer, Source value, JsonSerializerOptions options)
{
if (value is TextSource textSource)
{
JsonSerializer.Serialize(writer, textSource, options);
return;
}
if (value is CustomSource customSource)
{
JsonSerializer.Serialize(writer, customSource, options);
return;
}
if (value is Base64Source base64Source)
{
JsonSerializer.Serialize(writer, base64Source, options);
return;
}
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}