11using System . ComponentModel ;
2+ using System . Diagnostics ;
23using System . Text . Json ;
34using System . Text . Json . Serialization ;
45
@@ -8,7 +9,7 @@ namespace ModelContextProtocol.Protocol.Types;
89/// Represents the content of a resource.
910/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">See the schema for details</see>
1011/// </summary>
11- [ JsonConverter ( typeof ( ResourceContentsConverter ) ) ]
12+ [ JsonConverter ( typeof ( Converter ) ) ]
1213public abstract class ResourceContents
1314{
1415 internal ResourceContents ( )
@@ -26,104 +27,105 @@ internal ResourceContents()
2627 /// </summary>
2728 [ JsonPropertyName ( "mimeType" ) ]
2829 public string ? MimeType { get ; set ; }
29- }
3030
31- /// <summary>
32- /// Converter for <see cref="ResourceContents"/>.
33- /// </summary>
34- [ EditorBrowsable ( EditorBrowsableState . Never ) ]
35- public class ResourceContentsConverter : JsonConverter < ResourceContents >
36- {
37- /// <inheritdoc/>
38- public override ResourceContents ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
39- {
40- if ( reader . TokenType == JsonTokenType . Null )
41- {
42- return null ;
43- }
4431
45- if ( reader . TokenType != JsonTokenType . StartObject )
32+ /// <summary>
33+ /// Converter for <see cref="ResourceContents"/>.
34+ /// </summary>
35+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
36+ public class Converter : JsonConverter < ResourceContents >
37+ {
38+ /// <inheritdoc/>
39+ public override ResourceContents ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
4640 {
47- throw new JsonException ( ) ;
48- }
49-
50- string ? uri = null ;
51- string ? mimeType = null ;
52- string ? blob = null ;
53- string ? text = null ;
41+ if ( reader . TokenType == JsonTokenType . Null )
42+ {
43+ return null ;
44+ }
5445
55- while ( reader . Read ( ) && reader . TokenType != JsonTokenType . EndObject )
56- {
57- if ( reader . TokenType != JsonTokenType . PropertyName )
46+ if ( reader . TokenType != JsonTokenType . StartObject )
5847 {
59- continue ;
48+ throw new JsonException ( ) ;
6049 }
6150
62- string propertyName = reader . GetString ( ) ;
51+ string ? uri = null ;
52+ string ? mimeType = null ;
53+ string ? blob = null ;
54+ string ? text = null ;
6355
64- switch ( propertyName )
56+ while ( reader . Read ( ) && reader . TokenType != JsonTokenType . EndObject )
6557 {
66- case "uri" :
67- uri = reader . GetString ( ) ;
68- break ;
69- case "mimeType" :
70- mimeType = reader . GetString ( ) ;
71- break ;
72- case "blob" :
73- blob = reader . GetString ( ) ;
74- break ;
75- case "text" :
76- text = reader . GetString ( ) ;
77- break ;
78- default :
79- break ;
58+ if ( reader . TokenType != JsonTokenType . PropertyName )
59+ {
60+ continue ;
61+ }
62+
63+ string ? propertyName = reader . GetString ( ) ;
64+
65+ switch ( propertyName )
66+ {
67+ case "uri" :
68+ uri = reader . GetString ( ) ;
69+ break ;
70+ case "mimeType" :
71+ mimeType = reader . GetString ( ) ;
72+ break ;
73+ case "blob" :
74+ blob = reader . GetString ( ) ;
75+ break ;
76+ case "text" :
77+ text = reader . GetString ( ) ;
78+ break ;
79+ default :
80+ break ;
81+ }
8082 }
81- }
8283
83- if ( blob is not null )
84- {
85- return new BlobResourceContents
84+ if ( blob is not null )
8685 {
87- Uri = uri ?? string . Empty ,
88- MimeType = mimeType ,
89- Blob = blob
90- } ;
91- }
86+ return new BlobResourceContents
87+ {
88+ Uri = uri ?? string . Empty ,
89+ MimeType = mimeType ,
90+ Blob = blob
91+ } ;
92+ }
9293
93- if ( text is not null )
94- {
95- return new TextResourceContents
94+ if ( text is not null )
9695 {
97- Uri = uri ?? string . Empty ,
98- MimeType = mimeType ,
99- Text = text
100- } ;
101- }
102-
103- return null ;
104- }
96+ return new TextResourceContents
97+ {
98+ Uri = uri ?? string . Empty ,
99+ MimeType = mimeType ,
100+ Text = text
101+ } ;
102+ }
105103
106- /// <inheritdoc/>
107- public override void Write ( Utf8JsonWriter writer , ResourceContents value , JsonSerializerOptions options )
108- {
109- if ( value is null )
110- {
111- writer . WriteNullValue ( ) ;
112- return ;
104+ return null ;
113105 }
114106
115- writer . WriteStartObject ( ) ;
116- writer . WriteString ( "uri" , value . Uri ) ;
117- writer . WriteString ( "mimeType" , value . MimeType ) ;
118- Debug . Assert ( value is BlobResourceContents or TextResourceContents ) ;
119- if ( value is BlobResourceContents blobResource )
107+ /// <inheritdoc/>
108+ public override void Write ( Utf8JsonWriter writer , ResourceContents value , JsonSerializerOptions options )
120109 {
121- writer . WriteString ( "blob" , blobResource . Blob ) ;
122- }
123- else if ( value is TextResourceContents textResource )
124- {
125- writer . WriteString ( "text" , textResource . Text ) ;
110+ if ( value is null )
111+ {
112+ writer . WriteNullValue ( ) ;
113+ return ;
114+ }
115+
116+ writer . WriteStartObject ( ) ;
117+ writer . WriteString ( "uri" , value . Uri ) ;
118+ writer . WriteString ( "mimeType" , value . MimeType ) ;
119+ Debug . Assert ( value is BlobResourceContents or TextResourceContents ) ;
120+ if ( value is BlobResourceContents blobResource )
121+ {
122+ writer . WriteString ( "blob" , blobResource . Blob ) ;
123+ }
124+ else if ( value is TextResourceContents textResource )
125+ {
126+ writer . WriteString ( "text" , textResource . Text ) ;
127+ }
128+ writer . WriteEndObject ( ) ;
126129 }
127- writer . WriteEndObject ( ) ;
128130 }
129- }
131+ }
0 commit comments