com.thetransactioncompany.jsonrpc2
Class JSONRPC2Notification

java.lang.Object
  extended by com.thetransactioncompany.jsonrpc2.JSONRPC2Message
      extended by com.thetransactioncompany.jsonrpc2.JSONRPC2Notification

public class JSONRPC2Notification
extends JSONRPC2Message

Represents a JSON-RPC 2.0 notification.

Notifications provide a mean for calling a remote procedure without generating a response. Note that notifications are inherently unreliable as no confirmation is sent back to the caller.

Notifications have the same JSON structure as requests, except that they lack an identifier:

Here is a sample JSON-RPC 2.0 notification string:

 {  
    "method"  : "progressNotify",
    "params"  : ["75%"],
    "jsonrpc" : "2.0"
 }
 

This class provides two methods to obtain a request object:

Example 1: Parsing a notification string:

 String jsonString = "{\"method\":\"progressNotify\",\"params\":[\"75%\"],\"jsonrpc\":\"2.0\"}";
 
 JSONRPC2Notification notification = null;
 
 try {
         notification = JSONRPC2Notification.parse(jsonString);

 } catch (JSONRPC2ParseException e) {
         // handle exception
 }
 

Example 2: Recreating the above request:

 String method = "progressNotify";
 List params = new Vector();
 params.add("75%");

 JSONRPC2Notification notification = new JSONRPC2Notification(method, params);

 System.out.println(notification);
 

The mapping between JSON and Java entities (as defined by the underlying JSON.simple library):

     true|false  <--->  java.lang.Boolean
     number      <--->  java.lang.Number
     string      <--->  java.lang.String
     array       <--->  java.util.List
     object      <--->  java.util.Map
     null        <--->  null
 

The JSON-RPC 2.0 specification and user group forum can be found here.

Version:
1.10 (2010-03-15)
Author:
Vladimir Dzhuvinov

Constructor Summary
JSONRPC2Notification(java.lang.String method)
          Constructs a new JSON-RPC 2.0 notification with no parameters.
JSONRPC2Notification(java.lang.String method, java.util.List params)
          Constructs a new JSON-RPC 2.0 notification with JSON array parameters.
JSONRPC2Notification(java.lang.String method, java.util.Map params)
          Constructs a new JSON-RPC 2.0 notification JSON object parameters.
 
Method Summary
 java.lang.String getMethod()
          Gets the name of the requested method.
 java.lang.Object getParams()
          Gets the notification parameters.
 JSONRPC2ParamsType getParamsType()
          Gets the parameters type (JSONRPC2ParamsType.ARRAY, JSONRPC2ParamsType.OBJECT or JSONRPC2ParamsType.NO_PARAMS).
static JSONRPC2Notification parse(java.lang.String jsonString)
          Parses a JSON-RPC 2.0 notification string.
 void setMethod(java.lang.String method)
          Sets the name of the requested method.
 void setParams(java.lang.Object params)
          Sets the notification parameters.
 org.json.simple.JSONObject toJSON()
          Gets a JSON representation of the JSON-RPC 2.0 notification.
 
Methods inherited from class com.thetransactioncompany.jsonrpc2.JSONRPC2Message
toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JSONRPC2Notification

public JSONRPC2Notification(java.lang.String method)
Constructs a new JSON-RPC 2.0 notification with no parameters.

Parameters:
method - The name of the requested method.

JSONRPC2Notification

public JSONRPC2Notification(java.lang.String method,
                            java.util.List params)
Constructs a new JSON-RPC 2.0 notification with JSON array parameters.

Parameters:
method - The name of the requested method.
params - The notification parameters packed as a JSON array (maps to java.util.List).

JSONRPC2Notification

public JSONRPC2Notification(java.lang.String method,
                            java.util.Map params)
Constructs a new JSON-RPC 2.0 notification JSON object parameters.

Parameters:
method - The name of the requested method.
params - The notification parameters packed as a JSON object (maps to java.util.Map).
Method Detail

parse

public static JSONRPC2Notification parse(java.lang.String jsonString)
                                  throws JSONRPC2ParseException
Parses a JSON-RPC 2.0 notification string.

Parameters:
jsonString - The JSON notification string, UTF-8 encoded.
Returns:
The corresponding notification object.
Throws:
JSONRPC2ParseException - With detailed message if the parsing failed.

getMethod

public java.lang.String getMethod()
Gets the name of the requested method.

Returns:
The method name.

setMethod

public void setMethod(java.lang.String method)
Sets the name of the requested method.

Parameters:
method - The method name.

getParamsType

public JSONRPC2ParamsType getParamsType()
Gets the parameters type (JSONRPC2ParamsType.ARRAY, JSONRPC2ParamsType.OBJECT or JSONRPC2ParamsType.NO_PARAMS).

Returns:
The parameters type.

getParams

public java.lang.Object getParams()
Gets the notification parameters.

Returns:
The parameters as List if JSON array, Map if JSON object, or null if none.

setParams

public void setParams(java.lang.Object params)
Sets the notification parameters.

Parameters:
params - The parameters. For a JSON array type pass a List. For a JSON object pass a Map. If there are no parameters pass null.

toJSON

public org.json.simple.JSONObject toJSON()
Gets a JSON representation of the JSON-RPC 2.0 notification.

Specified by:
toJSON in class JSONRPC2Message
Returns:
A JSON object representing the notification.


Copyright © 2009-2010 Vladimir Dzhuvinov. All Rights Reserved.