com.thetransactioncompany.jsonrpc2
Class JSONRPC2Message

java.lang.Object
  extended by com.thetransactioncompany.jsonrpc2.JSONRPC2Message
Direct Known Subclasses:
JSONRPC2Notification, JSONRPC2Request, JSONRPC2Response

public abstract class JSONRPC2Message
extends java.lang.Object

The base abstract class for JSON-RPC 2.0 requests, notifications and responses. Provides generic methods for parsing (from JSON string) and serialisation (to JSON string) of the three message types.

Example showing 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 error
 }

 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.simple 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.

Version:
1.10 (2010-03-15)
Author:
Vladimir Dzhuvinov

Constructor Summary
JSONRPC2Message()
           
 
Method Summary
static JSONRPC2Message parse(java.lang.String jsonString)
          Provides common parsing of JSON-RPC 2.0 requests, notifications and responses.
abstract  org.json.simple.JSONObject toJSON()
          Gets a JSON object representing the message.
 java.lang.String toString()
          Serialises the message to a JSON string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JSONRPC2Message

public JSONRPC2Message()
Method Detail

parse

public static JSONRPC2Message parse(java.lang.String jsonString)
                             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.

If you are certain about the message type use the dedicated JSONRPC2Request.parse(java.lang.String), JSONRPC2Notification.parse(java.lang.String) and JSONRPC2Response.parse(java.lang.String) methods. They are more efficient and would provide you with better parse error reporting.

Parameters:
jsonString - A JSON string representing a JSON-RPC 2.0 request, notification or response.
Returns:
An instance of JSONRPC2Request, JSONRPC2Notification or JSONRPC2Response.
Throws:
JSONRPC2ParseException - With detailed message if the parsing failed.

toJSON

public abstract org.json.simple.JSONObject toJSON()
Gets a JSON object representing the message.

Returns:
A JSON object.

toString

public java.lang.String toString()
Serialises the message to a JSON string.

Overrides:
toString in class java.lang.Object
Returns:
A JSON-RPC 2.0 encoded string.


Copyright © 2009-2010 Vladimir Dzhuvinov. All Rights Reserved.