Clean up types (#6)

This commit is contained in:
Peder Holdgaard Pedersen 2025-01-05 13:19:24 +01:00 • committed by GitHub
commit 211cd2175b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 154 additions and 307 deletions

View file

@ -1,52 +0,0 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using McpDotNet.Protocol.Types;
namespace McpDotNet.Utils.Json;
/// <summary>
/// JSON converter for IContent that handles polymorphic deserialization based on the "type" property.
/// </summary>
public class ContentJsonConverter : JsonConverter<IContent>
{
/// <inheritdoc/>
public override IContent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException("Expected StartObject token");
}
using var doc = JsonDocument.ParseValue(ref reader);
var root = doc.RootElement;
if (!root.TryGetProperty("type", out var typeProperty))
{
throw new JsonException("Missing 'type' property");
}
var type = typeProperty.GetString();
return type switch
{
"text" => JsonSerializer.Deserialize<TextContent>(root.GetRawText(), options),
"image" => JsonSerializer.Deserialize<ImageContent>(root.GetRawText(), options),
_ => throw new JsonException($"Unknown content type: {type}")
};
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, IContent value, JsonSerializerOptions options)
{
switch (value)
{
case TextContent text:
JsonSerializer.Serialize(writer, text, options);
break;
case ImageContent image:
JsonSerializer.Serialize(writer, image, options);
break;
default:
throw new JsonException($"Unknown content type: {value.GetType()}");
}
}
}

View file

@ -7,7 +7,7 @@ namespace McpDotNet.Utils.Json;
/// <summary>
/// JSON converter for IJsonRpcMessage that handles polymorphic deserialization of different message types.
/// </summary>
public class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
internal class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
{
/// <inheritdoc/>
public override IJsonRpcMessage? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

View file

@ -5,7 +5,7 @@ namespace McpDotNet.Utils.Json;
/// <summary>
/// Extensions for configuring System.Text.Json serialization options for MCP.
/// </summary>
public static class JsonSerializerOptionsExtensions
internal static class JsonSerializerOptionsExtensions
{
/// <summary>
/// Configures JsonSerializerOptions with MCP-specific settings and converters.
@ -16,8 +16,6 @@ public static class JsonSerializerOptionsExtensions
{
// Add custom converters
options.Converters.Add(new JsonRpcMessageConverter());
options.Converters.Add(new ContentJsonConverter());
options.Converters.Add(new ResourceContentsJsonConverter());
options.Converters.Add(new JsonStringEnumConverter());
// Configure general options

View file

@ -1,51 +0,0 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using McpDotNet.Protocol.Types;
namespace McpDotNet.Utils.Json;
/// <summary>
/// JSON converter for IResourceContents that handles polymorphic deserialization based on content properties.
/// </summary>
public class ResourceContentsJsonConverter : JsonConverter<IResourceContents>
{
/// <inheritdoc/>
public override IResourceContents? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException("Expected StartObject token");
}
using var doc = JsonDocument.ParseValue(ref reader);
var root = doc.RootElement;
// Determine the type based on whether it has a 'text' or 'blob' property
if (root.TryGetProperty("text", out _))
{
return JsonSerializer.Deserialize<TextResourceContents>(root.GetRawText(), options);
}
else if (root.TryGetProperty("blob", out _))
{
return JsonSerializer.Deserialize<BlobResourceContents>(root.GetRawText(), options);
}
throw new JsonException("Resource contents must have either 'text' or 'blob' property");
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, IResourceContents value, JsonSerializerOptions options)
{
switch (value)
{
case TextResourceContents text:
JsonSerializer.Serialize(writer, text, options);
break;
case BlobResourceContents blob:
JsonSerializer.Serialize(writer, blob, options);
break;
default:
throw new JsonException($"Unknown resource contents type: {value.GetType()}");
}
}
}