@@ -71,6 +71,8 @@ private protected JsonRpcMessage()
7171 [ EditorBrowsable ( EditorBrowsableState . Never ) ]
7272 public sealed class Converter : JsonConverter < JsonRpcMessage >
7373 {
74+ private const string JsonRpcVersion = "2.0" ;
75+
7476 /// <summary>
7577 /// Private struct to hold parsed JSON-RPC message data during deserialization.
7678 /// </summary>
@@ -102,7 +104,10 @@ private struct Union
102104 /// </summary>
103105 public static Union Parse ( ref Utf8JsonReader reader , JsonSerializerOptions options )
104106 {
105- var union = new Union ( ) ;
107+ var union = new Union
108+ {
109+ JsonRpc = string . Empty // Initialize to avoid null reference warnings
110+ } ;
106111
107112 if ( reader . TokenType != JsonTokenType . StartObject )
108113 {
@@ -142,7 +147,7 @@ public static Union Parse(ref Utf8JsonReader reader, JsonSerializerOptions optio
142147
143148 case "method" :
144149 union . Method = reader . GetString ( ) ;
145- union . HasMethod = true ;
150+ union . HasMethod = union . Method is not null ;
146151 break ;
147152
148153 case "params" :
@@ -151,7 +156,7 @@ public static Union Parse(ref Utf8JsonReader reader, JsonSerializerOptions optio
151156
152157 case "error" :
153158 union . Error = JsonSerializer . Deserialize ( ref reader , options . GetTypeInfo < JsonRpcErrorDetail > ( ) ) ;
154- union . HasError = true ;
159+ union . HasError = union . Error is not null ;
155160 break ;
156161
157162 case "result" :
@@ -176,7 +181,7 @@ public static Union Parse(ref Utf8JsonReader reader, JsonSerializerOptions optio
176181 var union = Union . Parse ( ref reader , options ) ;
177182
178183 // All JSON-RPC messages must have a jsonrpc property with value "2.0"
179- if ( union . JsonRpc != "2.0" )
184+ if ( union . JsonRpc != JsonRpcVersion )
180185 {
181186 throw new JsonException ( "Invalid or missing jsonrpc version" ) ;
182187 }
@@ -187,11 +192,16 @@ public static Union Parse(ref Utf8JsonReader reader, JsonSerializerOptions optio
187192 // Messages with an error property are error responses
188193 if ( union . HasError )
189194 {
195+ if ( union . Error is null )
196+ {
197+ throw new JsonException ( "Error property cannot be null" ) ;
198+ }
199+
190200 return new JsonRpcError
191201 {
192202 JsonRpc = union . JsonRpc ,
193203 Id = union . Id ,
194- Error = union . Error !
204+ Error = union . Error
195205 } ;
196206 }
197207
@@ -212,22 +222,32 @@ public static Union Parse(ref Utf8JsonReader reader, JsonSerializerOptions optio
212222 // Messages with a method but no id are notifications
213223 if ( union . HasMethod && ! union . HasId )
214224 {
225+ if ( union . Method is null )
226+ {
227+ throw new JsonException ( "Method property cannot be null" ) ;
228+ }
229+
215230 return new JsonRpcNotification
216231 {
217232 JsonRpc = union . JsonRpc ,
218- Method = union . Method ! ,
233+ Method = union . Method ,
219234 Params = union . Params
220235 } ;
221236 }
222237
223238 // Messages with both method and id are requests
224239 if ( union . HasMethod && union . HasId )
225240 {
241+ if ( union . Method is null )
242+ {
243+ throw new JsonException ( "Method property cannot be null" ) ;
244+ }
245+
226246 return new JsonRpcRequest
227247 {
228248 JsonRpc = union . JsonRpc ,
229249 Id = union . Id ,
230- Method = union . Method ! ,
250+ Method = union . Method ,
231251 Params = union . Params
232252 } ;
233253 }
0 commit comments