@@ -198,6 +198,44 @@ public JSONObject toObject() {
198198 return this .value ;
199199 }
200200
201+ public static RequestMessage createRequest (Object id ,String method ) throws JSONRPCException {
202+ return Message .createRequest (id ,method ,null );
203+ }
204+
205+ /**
206+ *
207+ * @param id
208+ * @param method
209+ * @param params
210+ * @return
211+ * @throws JSONRPCException
212+ */
213+ public static RequestMessage createRequest (Object id ,String method ,Object params ) throws JSONRPCException {
214+ return Message .createRequest (id ,method ,params ,true );
215+ }
216+
217+ /**
218+ *
219+ * @param id
220+ * @param method
221+ * @param params
222+ * @param version2
223+ * @return
224+ * @throws JSONRPCException
225+ */
226+ public static RequestMessage createRequest (Object id ,String method ,Object params ,boolean version2 ) throws JSONRPCException {
227+ if (version2 ){
228+ return Message .createRequestMessageV2 (id ,method ,params );
229+ }
230+ return Message .createRequestMessageV1 (id ,method ,params !=null ?((JSONArray ) params ):new JSONArray ());
231+ }
232+
233+ /**
234+ *
235+ * @param id
236+ * @param method
237+ * @return
238+ */
201239 public static RequestMessage createRequestMessageV1 (Object id , String method ) {
202240 return Message .createRequestMessageV1 (id , method , new JSONArray ());
203241 }
@@ -208,8 +246,8 @@ public static RequestMessage createRequestMessageV1(Object id, String method) {
208246 * @param params
209247 * @return
210248 */
211- public static RequestMessage createRequestMessageV1 (Object id , String method , JSONArray params ) {
212- return new RequestMessage (new JSONObject ().put ("id" , id ).put ("method" , method ).put ("params" , params ));
249+ public static RequestMessage createRequestMessageV1 (Object id , String method , JSONArray params ){
250+ return new RequestMessage (new JSONObject ().put ("id" ,id != null ? id : JSONObject . NULL ).put ("method" ,method != null ? method : JSONObject . NULL ).put ("params" , params != null ? params : JSONObject . NULL ));
213251 }
214252
215253 public static RequestMessage createRequestMessageV2 (Object id , String method ) throws JSONRPCException {
@@ -223,7 +261,7 @@ public static RequestMessage createRequestMessageV2(Object id, String method) th
223261 * @return
224262 */
225263 public static RequestMessage createRequestMessageV2 (Object id , String method , Object params ) throws JSONRPCException {
226- JSONObject obj = new JSONObject ().put ("jsonrpc" , "2.0" ).put ("id" , id ).put ("method" , method );
264+ JSONObject obj = new JSONObject ().put ("jsonrpc" , "2.0" ).put ("id" ,id != null ? id : JSONObject . NULL ).put ("method" ,method != null ? method : JSONObject . NULL );
227265
228266 if (params instanceof JSONObject || params instanceof JSONArray ) {
229267 obj .put ("params" , params );
@@ -234,6 +272,42 @@ public static RequestMessage createRequestMessageV2(Object id, String method, Ob
234272 return new RequestMessage (obj );
235273 }
236274
275+ /**
276+ *
277+ * @param method
278+ * @return
279+ * @throws JSONRPCException
280+ */
281+ public static NotificationMessage createNotification (String method ) throws JSONRPCException {
282+ return Message .createNotification (method ,null );
283+ }
284+
285+ /**
286+ *
287+ * @param method
288+ * @param params
289+ * @return
290+ * @throws JSONRPCException
291+ */
292+ public static NotificationMessage createNotification (String method ,Object params ) throws JSONRPCException {
293+ return Message .createNotification (method ,params ,true );
294+ }
295+
296+ /**
297+ *
298+ * @param method
299+ * @param params
300+ * @param version2
301+ * @return
302+ * @throws JSONRPCException
303+ */
304+ public static NotificationMessage createNotification (String method ,Object params ,boolean version2 ) throws JSONRPCException {
305+ if (version2 ){
306+ return Message .createNotificationMessageV2 (method ,params );
307+ }
308+ return Message .createNotificationMessageV1 (method ,params !=null ?((JSONArray ) params ):new JSONArray ());
309+ }
310+
237311 /**
238312 * @param method
239313 * @return
@@ -248,7 +322,7 @@ public static NotificationMessage createNotificationMessageV1(String method) {
248322 * @return
249323 */
250324 public static NotificationMessage createNotificationMessageV1 (String method , JSONArray params ) {
251- return new NotificationMessage (new JSONObject ().put ("id" , JSONObject .NULL ).put ("method" , method ).put ("params" , params ));
325+ return new NotificationMessage (new JSONObject ().put ("id" ,JSONObject .NULL ).put ("method" ,method != null ? method : JSONObject . NULL ).put ("params" , params != null ? params : JSONObject . NULL ));
252326 }
253327
254328 /**
@@ -265,7 +339,7 @@ public static NotificationMessage createNotificationMessageV2(String method) thr
265339 * @return
266340 */
267341 public static NotificationMessage createNotificationMessageV2 (String method , Object params ) throws JSONRPCException {
268- JSONObject obj = new JSONObject ().put ("jsonrpc" , "2.0" ).put ("method" , method );
342+ JSONObject obj = new JSONObject ().put ("jsonrpc" , "2.0" ).put ("method" , method != null ? method : JSONObject . NULL );
269343
270344 if (params instanceof JSONObject || params instanceof JSONArray ) {
271345 obj .put ("params" , params );
@@ -276,6 +350,38 @@ public static NotificationMessage createNotificationMessageV2(String method, Obj
276350 return new NotificationMessage (obj );
277351 }
278352
353+ public static ResponseMessage createResponse (Object id ,Object result ) throws JSONRPCException {
354+ return Message .createResponse (id ,result ,null );
355+ }
356+
357+ /**
358+ *
359+ * @param id
360+ * @param result
361+ * @param error
362+ * @return
363+ * @throws JSONRPCException
364+ */
365+ public static ResponseMessage createResponse (Object id ,Object result ,Object error ) throws JSONRPCException {
366+ return Message .createResponse (id ,result ,error ,true );
367+ }
368+
369+ /**
370+ *
371+ * @param id
372+ * @param result
373+ * @param error
374+ * @param version2
375+ * @return
376+ * @throws JSONRPCException
377+ */
378+ public static ResponseMessage createResponse (Object id ,Object result ,Object error ,boolean version2 ) throws JSONRPCException {
379+ if (version2 ){
380+ return Message .createResponseMessageV2 (id ,result ,(JSONObject ) error );
381+ }
382+ return Message .createResponseMessageV1 (id ,result ,error );
383+ }
384+
279385 /**
280386 * @param id
281387 * @return
@@ -306,7 +412,7 @@ public static ResponseMessage createResponseMessageV1(Object id, Object result,
306412 if (!(error instanceof JSONObject ) && !(error instanceof String ) && error != null ) {
307413 throw new JSONRPCException ("[V1] The \" error\" property in request MUST be an string, object or null." );
308414 }
309- return new ResponseMessage (new JSONObject ().put ("id" , id ).put ("result" , result ).put ("error" , error ));
415+ return new ResponseMessage (new JSONObject ().put ("id" , id != null ? id : JSONObject . NULL ).put ("result" , result != null ? result : JSONObject . NULL ).put ("error" , error != null ? error : JSONObject . NULL ));
310416 }
311417
312418 /**
@@ -336,7 +442,7 @@ public static ResponseMessage createResponseMessageV2(Object id, Object result,
336442 if (result != null && error != null ) {
337443 throw new JSONRPCException ("[V2] Only one property \" result\" or \" error\" can be non null." );
338444 }
339- JSONObject obj = new JSONObject ().put ("jsonrpc" , "2.0" ).put ("id" , id );
445+ JSONObject obj = new JSONObject ().put ("jsonrpc" , "2.0" ).put ("id" , id != null ? id : JSONObject . NULL );
340446 if (error != null ) {
341447 if (!error .has ("code" )) {
342448 throw new JSONRPCException ("[V2] The error object MUST have a \" code\" property." );
0 commit comments