44using System . Threading ;
55using System . Threading . Tasks ;
66using Newtonsoft . Json . Linq ;
7+ using OmniSharp . Extensions . JsonRpc ;
78using OmniSharp . Extensions . LanguageServer . Client . Handlers ;
89
910namespace OmniSharp . Extensions . LanguageServer . Client . Dispatcher
@@ -21,10 +22,22 @@ public class LspDispatcher
2122 /// <summary>
2223 /// Create a new <see cref="LspDispatcher"/>.
2324 /// </summary>
24- public LspDispatcher ( )
25+ /// <param name="serializer">
26+ /// The JSON serialiser for notification / request / response payloads.
27+ /// </param>
28+ public LspDispatcher ( ISerializer serializer )
2529 {
30+ if ( serializer == null )
31+ throw new ArgumentNullException ( nameof ( serializer ) ) ;
32+
33+ Serializer = serializer ;
2634 }
2735
36+ /// <summary>
37+ /// The JSON serialiser to use for notification / request / response payloads.
38+ /// </summary>
39+ public ISerializer Serializer { get ; set ; }
40+
2841 /// <summary>
2942 /// Register a handler invoker.
3043 /// </summary>
@@ -92,7 +105,9 @@ public async Task<bool> TryHandleNotification(string method, JObject notificatio
92105
93106 if ( _handlers . TryGetValue ( method , out IHandler handler ) && handler is IInvokeNotificationHandler notificationHandler )
94107 {
95- await notificationHandler . Invoke ( notification ) ;
108+ object notificationPayload = DeserializePayload ( notificationHandler . PayloadType , notification ) ;
109+
110+ await notificationHandler . Invoke ( notificationPayload ) ;
96111
97112 return true ;
98113 }
@@ -121,9 +136,36 @@ public Task<object> TryHandleRequest(string method, JObject request, Cancellatio
121136 throw new ArgumentException ( $ "Argument cannot be null, empty, or entirely composed of whitespace: { nameof ( method ) } .", nameof ( method ) ) ;
122137
123138 if ( _handlers . TryGetValue ( method , out IHandler handler ) && handler is IInvokeRequestHandler requestHandler )
124- return requestHandler . Invoke ( request , cancellationToken ) ;
139+ {
140+ object requestPayload = DeserializePayload ( requestHandler . PayloadType , request ) ;
141+
142+ return requestHandler . Invoke ( requestPayload , cancellationToken ) ;
143+ }
125144
126145 return null ;
127146 }
147+
148+ /// <summary>
149+ /// Deserialise a notification / request payload from JSON.
150+ /// </summary>
151+ /// <param name="payloadType">
152+ /// The payload's CLR type.
153+ /// </param>
154+ /// <param name="payload">
155+ /// JSON representing the payload.
156+ /// </param>
157+ /// <returns>
158+ /// The deserialised payload (if one is present and expected).
159+ /// </returns>
160+ object DeserializePayload ( Type payloadType , JObject payload )
161+ {
162+ if ( payloadType == null )
163+ throw new ArgumentNullException ( nameof ( payloadType ) ) ;
164+
165+ if ( payloadType == null || payload == null )
166+ return null ;
167+
168+ return payload . ToObject ( payloadType , Serializer . JsonSerializer ) ;
169+ }
128170 }
129171}
0 commit comments