Class JSONRPC2Request
java.lang.Object
com.thetransactioncompany.jsonrpc2.JSONRPC2Message
com.thetransactioncompany.jsonrpc2.JSONRPC2Request
Represents a JSON-RPC 2.0 request.
A request carries four pieces of data:
methodThe name of the remote method to call.paramsThe required method parameters (if any), which can be packed into a JSON array or object.idAn identifier which is echoed back to the client with the response.jsonrpcA string indicating the JSON-RPC protocol version set to "2.0".
Here is a sample JSON-RPC 2.0 request string:
{
"method" : "makePayment",
"params" : { "recipient" : "Penny Adams", "amount":175.05 },
"id" : "0001",
"jsonrpc" : "2.0"
}
This class provides two methods to obtain a request object:
- Pass a JSON-RPC 2.0 request string to the static
parse(java.lang.String)method, or - Invoke one of the constructors with the appropriate arguments.
Example 1: Parsing a request string:
String jsonString = "{\"method\":\"makePayment\"," +
"\"params\":{\"recipient\":\"Penny Adams\",\"amount\":175.05}," +
"\"id\":\"0001\","+
"\"jsonrpc\":\"2.0\"}";
JSONRPC2Request req = null;
try {
req = JSONRPC2Request.parse(jsonString);
} catch (JSONRPC2ParseException e) {
// handle exception
}
Example 2: Recreating the above request:
String method = "makePayment";
Map<String,Object> params = new HashMap<String,Object>();
params.put("recipient", "Penny Adams");
params.put("amount", 175.05);
String id = "0001";
JSONRPC2Request req = new JSONRPC2Request(method, params, id);
System.out.println(req);
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
- Author:
- Vladimir Dzhuvinov
-
Constructor Summary
ConstructorsConstructorDescriptionJSONRPC2Request(String method, Object id) Constructs a new JSON-RPC 2.0 request with no parameters.JSONRPC2Request(String method, List<Object> positionalParams, Object id) Constructs a new JSON-RPC 2.0 request with positional (JSON array) parameters.Constructs a new JSON-RPC 2.0 request with named (JSON object) parameters. -
Method Summary
Modifier and TypeMethodDescriptiongetID()Gets the request identifier.Gets the name of the requested method.Gets the named parameters.Deprecated.Gets the positional (JSON array) parameters.static JSONRPC2RequestParses a JSON-RPC 2.0 request string.static JSONRPC2RequestParses a JSON-RPC 2.0 request string.static JSONRPC2RequestParses a JSON-RPC 2.0 request string.static JSONRPC2Requestparse(String jsonString, boolean preserveOrder, boolean ignoreVersion, boolean parseNonStdAttributes) Parses a JSON-RPC 2.0 request string.voidSets the request identifier (ID).voidSets the name of the requested method.voidsetNamedParams(Map<String, Object> namedParams) Sets the named (JSON object) request parameters.voidDeprecated.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 com.thetransactioncompany.jsonrpc2.JSONRPC2Message
appendNonStdAttribute, getNonStdAttribute, getNonStdAttributes, toJSONString, toString
-
Constructor Details
-
JSONRPC2Request
Constructs a new JSON-RPC 2.0 request with no parameters.- Parameters:
method- The name of the requested method. Must not benull.id- The request identifier echoed back to the caller. The value must map to a JSON scalar (nulland fractions, however, should be avoided).
-
JSONRPC2Request
Constructs a new JSON-RPC 2.0 request with positional (JSON array) parameters.- Parameters:
method- The name of the requested method. Must not benull.positionalParams- The positional (JSON array) parameters,nullif none.id- The request identifier echoed back to the caller. The value must map to a JSON scalar (nulland fractions, however, should be avoided).
-
JSONRPC2Request
Constructs a new JSON-RPC 2.0 request with named (JSON object) parameters.- Parameters:
method- The name of the requested method.namedParams- The named (JSON object) parameters,nullif none.id- The request identifier echoed back to the caller. The value must map to a JSON scalar (nulland fractions, however, should be avoided).
-
-
Method Details
-
parse
Parses a JSON-RPC 2.0 request string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 request string, UTF-8 encoded. Must not benull.- Returns:
- The corresponding JSON-RPC 2.0 request object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
-
parse
public static JSONRPC2Request parse(String jsonString, boolean preserveOrder) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 request string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 request 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 request object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
-
parse
public static JSONRPC2Request parse(String jsonString, boolean preserveOrder, boolean ignoreVersion) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 request string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 request 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 request object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
-
parse
public static JSONRPC2Request parse(String jsonString, boolean preserveOrder, boolean ignoreVersion, boolean parseNonStdAttributes) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 request string. This method is thread-safe.- Parameters:
jsonString- The JSON-RPC 2.0 request 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 request object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
-
getMethod
Gets the name of the requested method.- Returns:
- The method name.
-
setMethod
Sets the name of the requested method.- Parameters:
method- The method name. Must not benull.
-
getParamsType
- Returns:
- The parameters type.
-
getParams
Deprecated.Gets the request parameters.This method was deprecated in version 1.30. Use
getPositionalParams()orgetNamedParams()instead.- Returns:
- The parameters as
List<Object>for positional (JSON array),Map<String,Object>for named (JSON object), ornullif none.
-
getPositionalParams
Gets the positional (JSON array) parameters.- Returns:
- The positional (JSON array) parameters,
nullif none or named. - Since:
- 1.30
-
getNamedParams
Gets the named parameters.- Returns:
- The named (JSON object) parameters,
nullif none or positional. - Since:
- 1.30
-
setParams
Deprecated.Sets the request parameters.This method was deprecated in version 1.30. Use
setPositionalParams(java.util.List<java.lang.Object>)orsetNamedParams(java.util.Map<java.lang.String, java.lang.Object>)instead.- Parameters:
params- The parameters. For positional (JSON array) pass aList<Object>. For named (JSON object) pass aMap<String,Object>. If there are no parameters passnull.
-
setPositionalParams
Sets the positional (JSON array) request parameters.- Parameters:
positionalParams- The positional (JSON array) request parameters,nullif none.- Since:
- 1.30
-
setNamedParams
Sets the named (JSON object) request parameters.- Parameters:
namedParams- The named (JSON object) request parameters,nullif none.- Since:
- 1.30
-
getID
Gets the request identifier.- Returns:
- The request identifier (
Number,Boolean,String) ornull.
-
setID
Sets the request identifier (ID).- Parameters:
id- The request identifier echoed back to the caller. The value must map to a JSON scalar (nulland fractions, however, should be avoided).
-
toJSONObject
Description copied from class:JSONRPC2MessageReturns a JSON object representing this JSON-RPC 2.0 message.- Specified by:
toJSONObjectin classJSONRPC2Message- Returns:
- The JSON object.
-