- Direct Known Subclasses:
JSONRPC2Notification, JSONRPC2Request, JSONRPC2Response
Example parsing and serialisation back to JSON:
String jsonString = "{\"method\":\"progressNotify\",\"params\":[\"75%\"],\"jsonrpc\":\"2.0\"}";
JSONRPC2Message message = null;
// parse
try {
message = JSONRPC2Message.parse(jsonString);
} catch (JSONRPC2ParseException e) {
// handle parse exception
}
if (message instanceof JSONRPC2Request)
System.out.println("The message is a request");
else if (message instanceof JSONRPC2Notification)
System.out.println("The message is a notification");
else if (message instanceof JSONRPC2Response)
System.out.println("The message is a response");
// serialise back to JSON string
System.out.println(message);
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 TypeFieldDescriptionMap of non-standard JSON-RPC 2.0 message attributes,nullif none.Constructor Summary
ConstructorsConstructorDescriptionConstructor for subclasses; a message holds only the fields its own subclass sets.Method Summary
Modifier 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.static JSONRPC2MessageProvides common parsing of JSON-RPC 2.0 requests, notifications and responses.static JSONRPC2MessageProvides common parsing of JSON-RPC 2.0 requests, notifications and responses.abstract JsonObjectReturns a JSON object representing this JSON-RPC 2.0 message.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.
Field Details
nonStdAttributes
Constructor Details
JSONRPC2Message
public JSONRPC2Message()Constructor for subclasses; a message holds only the fields its own subclass sets.
Method Details
parse
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 JSON string represents.Batched requests / notifications are not supported.
This method is thread-safe.
If you are certain about the message type use the dedicated
JSONRPC2Request.parse(String),JSONRPC2Notification.parse(String)orJSONRPC2Response.parse(String)methods. They are more efficient and provide a more detailed parse error reporting.The member order of parsed JSON objects will not be preserved (for efficiency reasons) and the JSON-RPC 2.0 version field must be set to "2.0". To change this behaviour check the optional
parse(String,boolean,boolean)method.- 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 parsing failed.
parse
public static JSONRPC2Message parse(String jsonString, boolean preserveOrder, boolean ignoreVersion) throws JSONRPC2ParseException 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.Batched requests / notifications are not supported.
This method is thread-safe.
If you are certain about the message type use the dedicated
JSONRPC2Request.parse(String),JSONRPC2Notification.parse(String)orJSONRPC2Response.parse(String)methods. They are more efficient and provide a 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.preserveOrder- Iftruethe member order of JSON objects in parameters and results must be preserved.ignoreVersion- Iftruethe"jsonrpc":"2.0"version field in the JSON-RPC 2.0 message will not be checked.- Returns:
- An instance of
JSONRPC2Request,JSONRPC2NotificationorJSONRPC2Response. - Throws:
JSONRPC2ParseException- With detailed message if parsing failed.
appendNonStdAttribute
Appends a non-standard attribute to this JSON-RPC 2.0 message. This is done by adding a new member (key / value pair) to the top level JSON object representing the message.You may use this method to add meta and debugging attributes, such as the request processing time, to a JSON-RPC 2.0 message.
- Parameters:
name- The attribute name. Must not conflict with the existing "method", "id", "params", "result", "error" and "jsonrpc" attributes reserved by the JSON-RPC 2.0 protocol, else anIllegalArgumentExceptionwill be thrown. Must not benulleither.value- The attribute value. Must be of type String, boolean, number, List, Map or null, else anIllegalArgumentExceptionwill be thrown.
getNonStdAttribute
getNonStdAttributes
toJSONObject
Returns a JSON object representing this JSON-RPC 2.0 message.- Returns:
- The JSON object.
toJSONString
Returns a JSON string representation of this JSON-RPC 2.0 message.- Returns:
- The JSON object string representing this JSON-RPC 2.0 message.
- See Also:
toString