1- using System . Text . Json ;
21using System . Text . Json . Serialization ;
32
43namespace A2A ;
54
65/// <summary>
76/// Base class for A2A events.
87/// </summary>
9- [ JsonConverter ( typeof ( A2AEventConverter ) ) ]
10- public abstract class A2AEvent
8+ /// <param name="kind">The event kind discriminator.</param>
9+ [ JsonPolymorphic ( TypeDiscriminatorPropertyName = "kind" ) ]
10+ [ JsonDerivedType ( typeof ( TaskStatusUpdateEvent ) , "status-update" ) ]
11+ [ JsonDerivedType ( typeof ( TaskArtifactUpdateEvent ) , "artifact-update" ) ]
12+ [ JsonDerivedType ( typeof ( Message ) , "message" ) ]
13+ [ JsonDerivedType ( typeof ( AgentTask ) , "task" ) ]
14+ public abstract class A2AEvent ( string kind )
1115{
1216 /// <summary>
13- /// Event object discriminator.
17+ /// Gets the event kind discriminator used for polymorphic serialization .
1418 /// </summary>
15- [ JsonPropertyName ( "kind" ) ]
16- public abstract string Kind { get ; }
17- }
18-
19- /// <summary>
20- /// JSON converter for A2AEvent.
21- /// </summary>
22- sealed class A2AEventConverter : JsonConverter < A2AEvent >
23- {
24- /// <inheritdoc/>
25- public override A2AEvent ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
26- {
27- if ( reader . TokenType != JsonTokenType . StartObject )
28- {
29- throw new JsonException ( "Expected StartObject token" ) ;
30- }
31-
32- using var document = JsonDocument . ParseValue ( ref reader ) ;
33- if ( ! document . RootElement . TryGetProperty ( "kind" , out var kindProperty ) || kindProperty . ValueKind != JsonValueKind . String )
34- {
35- throw new JsonException ( "Missing or invalid 'kind' discriminator for A2AEvent." ) ;
36- }
37-
38- var kind = kindProperty . GetString ( ) ;
39- A2AEvent ? result = kind switch
40- {
41- "message" => document . RootElement . Deserialize ( A2AJsonUtilities . JsonContext . Default . Message ) ,
42- "task" => document . RootElement . Deserialize ( A2AJsonUtilities . JsonContext . Default . AgentTask ) ,
43- "status-update" => document . RootElement . Deserialize ( A2AJsonUtilities . JsonContext . Default . TaskStatusUpdateEvent ) ,
44- "artifact-update" => document . RootElement . Deserialize ( A2AJsonUtilities . JsonContext . Default . TaskArtifactUpdateEvent ) ,
45- _ => null ,
46- } ;
47-
48- if ( result is null )
49- {
50- throw new JsonException ( $ "Unknown A2AEvent kind '{ kind } '.") ;
51- }
52-
53- return result ;
54- }
55-
56- /// <inheritdoc/>
57- public override void Write ( Utf8JsonWriter writer , A2AEvent value , JsonSerializerOptions options )
58- {
59- if ( value is null )
60- {
61- writer . WriteNullValue ( ) ;
62- return ;
63- }
64-
65- switch ( value )
66- {
67- case Message message :
68- JsonSerializer . Serialize ( writer , message , A2AJsonUtilities . JsonContext . Default . Message ) ;
69- break ;
70- case AgentTask task :
71- JsonSerializer . Serialize ( writer , task , A2AJsonUtilities . JsonContext . Default . AgentTask ) ;
72- break ;
73- case TaskStatusUpdateEvent taskStatusUpdateEvent :
74- JsonSerializer . Serialize ( writer , taskStatusUpdateEvent , A2AJsonUtilities . JsonContext . Default . TaskStatusUpdateEvent ) ;
75- break ;
76- case TaskArtifactUpdateEvent taskArtifactUpdateEvent :
77- JsonSerializer . Serialize ( writer , taskArtifactUpdateEvent , A2AJsonUtilities . JsonContext . Default . TaskArtifactUpdateEvent ) ;
78- break ;
79- default :
80- throw new JsonException ( $ "Unsupported A2AEvent runtime type { value . GetType ( ) . Name } ") ;
81- }
82- }
19+ [ JsonIgnore ]
20+ public string Kind { get ; } = kind ;
8321}
8422
8523/// <summary>
8624/// A2A response objects.
8725/// </summary>
88- [ JsonConverter ( typeof ( A2AResponseConverter ) ) ]
89- public abstract class A2AResponse : A2AEvent ;
90-
91- /// <summary>
92- /// JSON converter for A2AResponse.
93- /// </summary>
94- sealed class A2AResponseConverter : JsonConverter < A2AResponse >
95- {
96- private static readonly A2AEventConverter _eventConverter = new ( ) ;
97-
98- /// <inheritdoc/>
99- public override A2AResponse ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
100- {
101- // Delegate the deserialization to A2AEventConverter.
102- var a2aEvent = _eventConverter . Read ( ref reader , typeof ( A2AEvent ) , options ) ;
103- if ( a2aEvent is A2AResponse a2aResponse )
104- {
105- return a2aResponse ;
106- }
107- throw new JsonException ( "JSON did not represent an A2AResponse instance." ) ;
108- }
109-
110- /// <inheritdoc/>
111- public override void Write ( Utf8JsonWriter writer , A2AResponse value , JsonSerializerOptions options )
112- {
113- JsonSerializer . Serialize ( writer , value , A2AJsonUtilities . JsonContext . Default . A2AEvent ) ;
114- }
115- }
26+ /// <param name="kind">The event kind discriminator.</param>
27+ [ JsonPolymorphic ( TypeDiscriminatorPropertyName = "kind" ) ]
28+ [ JsonDerivedType ( typeof ( Message ) , "message" ) ]
29+ [ JsonDerivedType ( typeof ( AgentTask ) , "task" ) ]
30+ public abstract class A2AResponse ( string kind ) : A2AEvent ( kind ) ;
0 commit comments