using System.Text.Json; using System.Text.Json.Serialization; using AnthropicClient.Models; namespace AnthropicClient.Json; class EventDataConverter : JsonConverter { public override EventData 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 { EventType.Ping => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.Error => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.MessageStart => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.MessageDelta => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.MessageStop => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.ContentBlockStart => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.ContentBlockDelta => JsonSerializer.Deserialize(root.GetRawText(), options)!, EventType.ContentBlockStop => JsonSerializer.Deserialize(root.GetRawText(), options)!, _ => throw new JsonException($"Unknown content type: {type}") }; } public override void Write(Utf8JsonWriter writer, EventData value, JsonSerializerOptions options) { JsonSerializer.Serialize(writer, value, value.GetType(), options); } }