Parsing of batched requests / notifications is not supported.
This class is not thread-safe. A parser instance should not be used by more than one thread unless properly synchronised. Alternatively, you may use the thread-safe JSONRPC2Message.parse(String) and its sister methods.
Example:
String jsonString = "{\"method\":\"makePayment\"," +
"\"params\":{\"recipient\":\"Penny Adams\",\"amount\":175.05}," +
"\"id\":\"0001\","+
"\"jsonrpc\":\"2.0\"}";
JSONRPC2Request req = null;
JSONRPC2Parser parser = new JSONRPC2Parser();
try {
req = parser.parseJSONRPC2Request(jsonString);
} catch (JSONRPC2ParseException e) {
// handle exception
}
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
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanIftruethe"jsonrpc":"2.0"version attribute in the JSON-RPC 2.0 message must be ignored during parsing.private booleanIftruenon-standard JSON-RPC 2.0 message attributes must be parsed too.private booleanIftruethe order of the parsed JSON object members must be preserved.Constructor Summary
ConstructorsConstructorDescriptionCreates a new JSON-RPC 2.0 message parser.JSONRPC2Parser(boolean preserveOrder) Creates a new JSON-RPC 2.0 message parser.JSONRPC2Parser(boolean preserveOrder, boolean ignoreVersion) Creates a new JSON-RPC 2.0 message parser.JSONRPC2Parser(boolean preserveOrder, boolean ignoreVersion, boolean parseNonStdAttributes) Creates a new JSON-RPC 2.0 message parser.Method Summary
Modifier and TypeMethodDescriptionprivate static voidensureVersion2(Object version, String jsonString) Ensures the specified parameter is aStringobject set to "2.0".booleanReturnstrueif the"jsonrpc":"2.0"version attribute in parsed JSON-RPC 2.0 messages is ignored, elsefalse.voidignoreVersion(boolean ignore) Specifies whether to ignore the"jsonrpc":"2.0"version attribute during parsing of JSON-RPC 2.0 messages.parseJSONObject(String jsonString) Parses a JSON object string.parseJSONRPC2Message(String jsonString) Provides common parsing of JSON-RPC 2.0 requests, notifications and responses.parseJSONRPC2Notification(String jsonString) Parses a JSON-RPC 2.0 notification string.parseJSONRPC2Request(String jsonString) Parses a JSON-RPC 2.0 request string.parseJSONRPC2Response(String jsonString) Parses a JSON-RPC 2.0 response string.voidparseNonStdAttributes(boolean enable) Specifies whether to parse non-standard attributes found in JSON-RPC 2.0 messages.booleanReturnstrueif non-standard attributes in JSON-RPC 2.0 messages are parsed.voidpreserveOrder(boolean preserveOrder) Controls the preservation of JSON object member order in parsed JSON-RPC 2.0 messages.booleanReturnstrueif the order of JSON object members in parsed JSON-RPC 2.0 messages is preserved, elsefalse.
Field Details
preserveOrder
private boolean preserveOrderIftruethe order of the parsed JSON object members must be preserved.ignoreVersion
private boolean ignoreVersionIftruethe"jsonrpc":"2.0"version attribute in the JSON-RPC 2.0 message must be ignored during parsing.parseNonStdAttributes
private boolean parseNonStdAttributesIftruenon-standard JSON-RPC 2.0 message attributes must be parsed too.
Constructor Details
JSONRPC2Parser
public JSONRPC2Parser()Creates a new JSON-RPC 2.0 message parser.The member order of parsed JSON objects in parameters and results will not be preserved; strict checking of the 2.0 JSON-RPC version attribute will be enforced; non-standard message attributes will be ignored. Check the other constructors if you want to specify different behaviour.
JSONRPC2Parser
public JSONRPC2Parser(boolean preserveOrder) Creates a new JSON-RPC 2.0 message parser.Strict checking of the 2.0 JSON-RPC version attribute will be enforced; non-standard message attributes will be ignored. Check the other constructors if you want to specify different behaviour.
- Parameters:
preserveOrder- Iftruethe member order of JSON objects in parameters and results will be preserved.
JSONRPC2Parser
public JSONRPC2Parser(boolean preserveOrder, boolean ignoreVersion) Creates a new JSON-RPC 2.0 message parser.Non-standard message attributes will be ignored. Check the other constructors if you want to specify different behaviour.
- Parameters:
preserveOrder- Iftruethe member order of JSON objects in parameters and results will be preserved.ignoreVersion- Iftruethe"jsonrpc":"2.0"version attribute in the JSON-RPC 2.0 message will not be checked.
JSONRPC2Parser
public JSONRPC2Parser(boolean preserveOrder, boolean ignoreVersion, boolean parseNonStdAttributes) Creates a new JSON-RPC 2.0 message parser.This constructor allows full specification of the available JSON-RPC message parsing properties.
- Parameters:
preserveOrder- Iftruethe member order of JSON objects in parameters and results will be preserved.ignoreVersion- Iftruethe"jsonrpc":"2.0"version attribute in the JSON-RPC 2.0 message will not be checked.parseNonStdAttributes- Iftruenon-standard attributes found in the JSON-RPC 2.0 messages will be parsed too.
Method Details
parseJSONObject
Parses a JSON object string. Provides the initial parsing of JSON-RPC 2.0 messages. The member order of JSON objects will be preserved ifpreserveOrderis set totrue.- Parameters:
jsonString- The JSON string to parse. Must not benull.- Returns:
- The parsed JSON object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
ensureVersion2
Ensures the specified parameter is aStringobject set to "2.0". This method is intended to check the "jsonrpc" attribute during parsing of JSON-RPC messages.- Parameters:
version- The version parameter. Must not benull.jsonString- The original JSON string.- Throws:
JSONRPC2ParseException- If the parameter is not a string that equals "2.0".
parseJSONRPC2Message
Provides common parsing of JSON-RPC 2.0 requests, notifications and responses. Use this method if you don't know which type of JSON-RPC message the input string represents.If a particular message type is expected use the dedicated
parseJSONRPC2Request(String),parseJSONRPC2Notification(String)andparseJSONRPC2Response(String)methods. They are more efficient and would provide you with more detailed parse error reporting.- Parameters:
jsonString- A JSON string representing a JSON-RPC 2.0 request, notification or response, UTF-8 encoded. Must not benull.- Returns:
- An instance of
JSONRPC2Request,JSONRPC2NotificationorJSONRPC2Response. - Throws:
JSONRPC2ParseException- With detailed message if the parsing failed.
parseJSONRPC2Request
Parses a JSON-RPC 2.0 request string.- 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.
parseJSONRPC2Notification
public JSONRPC2Notification parseJSONRPC2Notification(String jsonString) throws JSONRPC2ParseException Parses a JSON-RPC 2.0 notification string.- 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.
parseJSONRPC2Response
Parses a JSON-RPC 2.0 response string.- Parameters:
jsonString- The JSON-RPC 2.0 response string, UTF-8 encoded. Must not benull.- Returns:
- The corresponding JSON-RPC 2.0 response object.
- Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
preserveOrder
public void preserveOrder(boolean preserveOrder) Controls the preservation of JSON object member order in parsed JSON-RPC 2.0 messages.- Parameters:
preserveOrder-trueto preserve the order of JSON object members, elsefalse.
preservesOrder
public boolean preservesOrder()Returnstrueif the order of JSON object members in parsed JSON-RPC 2.0 messages is preserved, elsefalse.- Returns:
trueif order is preserved, elsefalse.
ignoreVersion
public void ignoreVersion(boolean ignore) Specifies whether to ignore the"jsonrpc":"2.0"version attribute during parsing of JSON-RPC 2.0 messages.You may with to disable strict 2.0 version checking if the parsed JSON-RPC 2.0 messages don't include a version attribute or if you wish to achieve limited compatibility with older JSON-RPC protocol versions.
- Parameters:
ignore-trueto skip checks of the"jsonrpc":"2.0"version attribute in parsed JSON-RPC 2.0 messages, elsefalse.
ignoresVersion
public boolean ignoresVersion()Returnstrueif the"jsonrpc":"2.0"version attribute in parsed JSON-RPC 2.0 messages is ignored, elsefalse.- Returns:
trueif the"jsonrpc":"2.0"version attribute in parsed JSON-RPC 2.0 messages is ignored, elsefalse.
parseNonStdAttributes
public void parseNonStdAttributes(boolean enable) Specifies whether to parse non-standard attributes found in JSON-RPC 2.0 messages.- Parameters:
enable-trueto parse non-standard attributes, elsefalse.
parsesNonStdAttributes
public boolean parsesNonStdAttributes()Returnstrueif non-standard attributes in JSON-RPC 2.0 messages are parsed.- Returns:
trueif non-standard attributes are parsed, elsefalse.