java.lang.Object
com.thetransactioncompany.jsonrpc2.JSONRPC2Message
com.thetransactioncompany.jsonrpc2.JSONRPC2Notification
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:
methodThe name of the remote method to call.paramsThe required method parameters (if any), which can be packed into a JSON array or object.jsonrpcA string indicating the JSON-RPC protocol version set to "2.0".
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:
- Pass a JSON-RPC 2.0 notification string to the static
parse(String)method, or - Invoke one of the constructors with the appropriate arguments.
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<Object> params = new Vector<Object>();
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 Smart library):
true|false <---> java.lang.Boolean
number <---> java.lang.Number
string <---> java.lang.String
array <---> java.util.List
object <---> java.util.Map
null <---> null
Constructor Summary
ConstructorsConstructorDescriptionJSONRPC2Notification(String method) Constructs a new JSON-RPC 2.0 notification with no parameters.JSONRPC2Notification(String method, List<Object> positionalParams) Constructs a new JSON-RPC 2.0 notification with positional (JSON array) parameters.JSONRPC2Notification(String method, Map<String, Object> namedParams) Constructs a new JSON-RPC 2.0 notification with named (JSON object) parameters.Method Summary
Modifier and TypeMethodDescriptionGets the name of the requested method.Gets the named parameters.Gets the positional (JSON array) parameters.static JSONRPC2NotificationParses a JSON-RPC 2.0 notification string.static JSONRPC2NotificationParses a JSON-RPC 2.0 notification string.static JSONRPC2NotificationParses a JSON-RPC 2.0 notification string.static JSONRPC2Notificationparse(String jsonString, boolean preserveOrder, boolean ignoreVersion, boolean parseNonStdAttributes) Parses a JSON-RPC 2.0 notification string.voidSets the name of the requested method.voidsetNamedParams(Map<String, Object> namedParams) Sets the named (JSON object) request parameters.voidsetPositionalParams(List<Object> positionalParams) Sets the positional (JSON array) request parameters.Returns a JSON object representing this JSON-RPC 2.0 message.Methods inherited from class JSONRPC2Message
appendNonStdAttribute, getNonStdAttribute, getNonStdAttributes, toJSONString, toStringModifier and TypeMethodDescriptionvoidappendNonStdAttribute(String name, Object value) Appends a non-standard attribute to this JSON-RPC 2.0 message.getNonStdAttribute(String name) Retrieves a non-standard JSON-RPC 2.0 message attribute.Retrieves the non-standard JSON-RPC 2.0 message attributes.Returns a JSON string representation of this JSON-RPC 2.0 message.toString()Serialises this JSON-RPC 2.0 message to a JSON object string.
Constructor Details
JSONRPC2Notification
Constructs a new JSON-RPC 2.0 notification with no parameters.- Parameters:
method- The name of the requested method. Must not benull.
JSONRPC2Notification
JSONRPC2Notification
Method Details
parse
Parses a JSON-RPC 2.0 notification string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 notification string, UTF-8 encoded. Must not benull.- Returns:
- The corresponding JSON-RPC 2.0 notification object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
parse
public static JSONRPC2Notification parse(String jsonString, boolean preserveOrder) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 notification string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 notification string, UTF-8 encoded. Must not benull.preserveOrder-trueto preserve the order of JSON object members in parameters.- Returns:
- The corresponding JSON-RPC 2.0 notification object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
parse
public static JSONRPC2Notification parse(String jsonString, boolean preserveOrder, boolean ignoreVersion) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 notification string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 notification string, UTF-8 encoded. Must not benull.preserveOrder-trueto preserve the order of JSON object members in parameters.ignoreVersion-trueto skip a check of the"jsonrpc":"2.0"version attribute in the JSON-RPC 2.0 message.- Returns:
- The corresponding JSON-RPC 2.0 notification object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
parse
public static JSONRPC2Notification parse(String jsonString, boolean preserveOrder, boolean ignoreVersion, boolean parseNonStdAttributes) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 notification string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 notification string, UTF-8 encoded. Must not benull.preserveOrder-trueto preserve the order of JSON object members in parameters.ignoreVersion-trueto skip a check of the"jsonrpc":"2.0"version attribute in the JSON-RPC 2.0 message.parseNonStdAttributes-trueto parse non-standard attributes found in the JSON-RPC 2.0 message.- Returns:
- The corresponding JSON-RPC 2.0 notification object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
getMethod
setMethod
Sets the name of the requested method.- Parameters:
method- The method name. Must not benull.
getParamsType
- Returns:
- The parameters type.
getPositionalParams
getNamedParams
setPositionalParams
setNamedParams
toJSONObject
Description copied from class:JSONRPC2MessageReturns a JSON object representing this JSON-RPC 2.0 message.- Specified by:
toJSONObjectin classJSONRPC2Message- Returns:
- The JSON object.