|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.thetransactioncompany.jsonrpc2.JSONRPC2Message
com.thetransactioncompany.jsonrpc2.JSONRPC2Request
public class JSONRPC2Request
Represents a JSON-RPC 2.0 request.
A request carries four pieces of data:
method The name of the remote method to call.
params The required method parameters (if any), which can
be packed into a JSON array or object.
id An identifier which is echoed back to the client with
the response.
jsonrpc A 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:
parse(java.lang.String) method, or
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
The JSON-RPC 2.0 specification and user group forum can be found here.
| Constructor Summary | |
|---|---|
JSONRPC2Request(String method,
List<Object> positionalParams,
Object id)
Constructs a new JSON-RPC 2.0 request with positional (JSON array) parameters. |
|
JSONRPC2Request(String method,
Map<String,Object> namedParams,
Object id)
Constructs a new JSON-RPC 2.0 request with named (JSON object) parameters. |
|
JSONRPC2Request(String method,
Object id)
Constructs a new JSON-RPC 2.0 request with no parameters. |
|
| Method Summary | |
|---|---|
Object |
getID()
Gets the request identifier. |
String |
getMethod()
Gets the name of the requested method. |
Map<String,Object> |
getNamedParams()
Gets the named parameters. |
Object |
getParams()
Deprecated. |
JSONRPC2ParamsType |
getParamsType()
Gets the parameters type ( positional,
named or
none). |
List<Object> |
getPositionalParams()
Gets the positional (JSON array) parameters. |
static JSONRPC2Request |
parse(String jsonString)
Parses a JSON-RPC 2.0 request string. |
static JSONRPC2Request |
parse(String jsonString,
boolean preserveOrder)
Parses a JSON-RPC 2.0 request string. |
static JSONRPC2Request |
parse(String jsonString,
boolean preserveOrder,
boolean ignoreVersion)
Parses a JSON-RPC 2.0 request string. |
static JSONRPC2Request |
parse(String jsonString,
boolean preserveOrder,
boolean ignoreVersion,
boolean parseNonStdAttributes)
Parses a JSON-RPC 2.0 request string. |
void |
setID(Object id)
Sets the request identifier (ID). |
void |
setMethod(String method)
Sets the name of the requested method. |
void |
setNamedParams(Map<String,Object> namedParams)
Sets the named (JSON object) request parameters. |
void |
setParams(Object params)
Deprecated. |
void |
setPositionalParams(List<Object> positionalParams)
Sets the positional (JSON array) request parameters. |
net.minidev.json.JSONObject |
toJSONObject()
Returns a JSON object representing this JSON-RPC 2.0 message. |
| Methods inherited from class com.thetransactioncompany.jsonrpc2.JSONRPC2Message |
|---|
appendNonStdAttribute, getNonStdAttribute, getNonStdAttributes, toJSONString, toString |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public JSONRPC2Request(String method,
Object id)
method - The name of the requested method. Must not be
null.id - The request identifier echoed back to the caller.
The value must map to a JSON
scalar (null and fractions, however, should
be avoided).
public JSONRPC2Request(String method,
List<Object> positionalParams,
Object id)
method - The name of the requested method. Must not
be null.positionalParams - The positional (JSON array) parameters,
null if none.id - The request identifier echoed back to the
caller. The value must map
to a JSON scalar (null and
fractions, however, should be avoided).
public JSONRPC2Request(String method,
Map<String,Object> namedParams,
Object id)
method - The name of the requested method.namedParams - The named (JSON object) parameters, null
if none.id - The request identifier echoed back to the caller.
The value must map to a JSON
scalar (null and fractions, however,
should be avoided).| Method Detail |
|---|
public static JSONRPC2Request parse(String jsonString)
throws JSONRPC2ParseException
jsonString - The JSON-RPC 2.0 request string, UTF-8 encoded.
Must not be null.
JSONRPC2ParseException - With detailed message if parsing
failed.
public static JSONRPC2Request parse(String jsonString,
boolean preserveOrder)
throws JSONRPC2ParseException
jsonString - The JSON-RPC 2.0 request string, UTF-8 encoded.
Must not be null.preserveOrder - true to preserve the order of JSON
object members in parameters.
JSONRPC2ParseException - With detailed message if parsing
failed.
public static JSONRPC2Request parse(String jsonString,
boolean preserveOrder,
boolean ignoreVersion)
throws JSONRPC2ParseException
jsonString - The JSON-RPC 2.0 request string, UTF-8 encoded.
Must not be null.preserveOrder - true to preserve the order of JSON
object members in parameters.ignoreVersion - true to skip a check of the
"jsonrpc":"2.0" version attribute in the
JSON-RPC 2.0 message.
JSONRPC2ParseException - With detailed message if parsing
failed.
public static JSONRPC2Request parse(String jsonString,
boolean preserveOrder,
boolean ignoreVersion,
boolean parseNonStdAttributes)
throws JSONRPC2ParseException
jsonString - The JSON-RPC 2.0 request string, UTF-8
encoded. Must not be null.preserveOrder - true to preserve the order of
JSON object members in parameters.ignoreVersion - true to skip a check of the
"jsonrpc":"2.0" version
attribute in the JSON-RPC 2.0 message.parseNonStdAttributes - true to parse non-standard
attributes found in the JSON-RPC 2.0
message.
JSONRPC2ParseException - With detailed message if parsing
failed.public String getMethod()
public void setMethod(String method)
method - The method name. Must not be null.public JSONRPC2ParamsType getParamsType()
positional,
named or
none).
@Deprecated public Object getParams()
This method was deprecated in version 1.30. Use
getPositionalParams() or getNamedParams() instead.
List<Object> for positional (JSON
array), Map<String,Object> for named (JSON object),
or null if none.public List<Object> getPositionalParams()
null if none
or named.public Map<String,Object> getNamedParams()
null if none or
positional.@Deprecated public void setParams(Object params)
This method was deprecated in version 1.30. Use
setPositionalParams(java.util.List or setNamedParams(java.util.Map instead.
params - The parameters. For positional (JSON array) pass a
List<Object>. For named (JSON object) pass a
Map<String,Object>. If there are no
parameters pass null.public void setPositionalParams(List<Object> positionalParams)
positionalParams - The positional (JSON array) request
parameters, null if none.public void setNamedParams(Map<String,Object> namedParams)
namedParams - The named (JSON object) request parameters,
null if none.public Object getID()
Number, Boolean,
String) or null.public void setID(Object id)
id - The request identifier echoed back to the caller.
The value must map to a JSON
scalar (null and fractions, however, should
be avoided).public net.minidev.json.JSONObject toJSONObject()
JSONRPC2Message
toJSONObject in class JSONRPC2Message
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||