Clean up and add more comments (#2)

This commit is contained in:
Peder Holdgaard Pedersen 2025-01-02 19:02:24 +01:00 • committed by GitHub
commit 9a244f5770
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 349 additions and 653 deletions

View file

@ -1,15 +1,15 @@
// Utils/Json/ContentJsonConverter.cs
namespace McpDotNet.Utils.Json;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using global::McpDotNet.Protocol.Types;
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)
@ -34,6 +34,7 @@ public class ContentJsonConverter : JsonConverter<IContent>
};
}
/// </inheritdoc>
public override void Write(Utf8JsonWriter writer, IContent value, JsonSerializerOptions options)
{
switch (value)

View file

@ -1,15 +1,15 @@
// Utils/Json/JsonRpcMessageConverter.cs (continued)
namespace McpDotNet.Utils.Json;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using global::McpDotNet.Protocol.Messages;
using McpDotNet.Protocol.Messages;
namespace McpDotNet.Utils.Json;
/// <summary>
/// JSON converter for IJsonRpcMessage that handles polymorphic deserialization of different message types.
/// </summary>
public class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
{
/// </inheritdoc>
public override IJsonRpcMessage? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
@ -63,6 +63,7 @@ public class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
throw new JsonException("Invalid JSON-RPC message format");
}
/// </inheritdoc>
public override void Write(Utf8JsonWriter writer, IJsonRpcMessage value, JsonSerializerOptions options)
{
switch (value)

View file

@ -1,8 +1,7 @@
namespace McpDotNet.Utils.Json;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace McpDotNet.Utils.Json;
/// <summary>
/// Extensions for configuring System.Text.Json serialization options for MCP.
/// </summary>

View file

@ -1,14 +1,15 @@
namespace McpDotNet.Utils.Json;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using global::McpDotNet.Protocol.Types;
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)
@ -32,6 +33,7 @@ public class ResourceContentsJsonConverter : JsonConverter<IResourceContents>
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)