Class JSONRPC2Message

  • All Implemented Interfaces:
    net.minidev.json.JSONAware
    Direct Known Subclasses:
    JSONRPC2Notification, JSONRPC2Request, JSONRPC2Response

    public abstract class JSONRPC2Message
    extends Object
    implements net.minidev.json.JSONAware
    The base abstract class for JSON-RPC 2.0 requests, notifications and responses. Provides common methods for parsing (from JSON string) and serialisation (to JSON string) of the three message types.

    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
     
    Author:
    Vladimir Dzhuvinov
    • Method Detail

      • appendNonStdMember

        public void appendNonStdMember​(String name,
                                       Object value)
        Appends a non-standard member 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 members, such as the request processing time, to a JSON-RPC 2.0 message.

        Parameters:
        name - The member name. Must not conflict with the existing "method", "id", "params", "result", "error" and "jsonrpc" members reserved by the JSON-RPC 2.0 protocol, else an IllegalArgumentException will be thrown. Must not be null either.
        value - The member value. Must be of type String, boolean, number, List, Map or null, else an IllegalArgumentException will be thrown.
      • appendNonStdAttribute

        @Deprecated
        public void appendNonStdAttribute​(String name,
                                          Object value)
        Deprecated.
        Appends a non-standard member 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 members, such as the request processing time, to a JSON-RPC 2.0 message.

        Parameters:
        name - The member name. Must not conflict with the existing "method", "id", "params", "result", "error" and "jsonrpc" members reserved by the JSON-RPC 2.0 protocol, else an IllegalArgumentException will be thrown. Must not be null either.
        value - The member value. Must be of type String, boolean, number, List, Map or null, else an IllegalArgumentException will be thrown.
      • getNonStdMember

        public Object getNonStdMember​(String name)
        Gets a non-standard JSON-RPC 2.0 message member.
        Parameters:
        name - The name of the non-standard members to retrieve. Must not be null.
        Returns:
        The value of the non-standard members (may also be null), null if not found.
      • getNonStdAttribute

        @Deprecated
        public Object getNonStdAttribute​(String name)
        Deprecated.
        Gets a non-standard JSON-RPC 2.0 message member.
        Parameters:
        name - The name of the non-standard members to retrieve. Must not be null.
        Returns:
        The value of the non-standard members (may also be null), null if not found.
      • getNonStdMembers

        public Map<String,​ObjectgetNonStdMembers()
        Gets the non-standard JSON-RPC 2.0 message members.
        Returns:
        The non-standard members as a map, empty map if none.
      • toJSONObject

        public abstract net.minidev.json.JSONObject toJSONObject()
        Returns a JSON object representing this JSON-RPC 2.0 message.
        Returns:
        The JSON object.
      • toJSONString

        public String toJSONString()
        Returns a JSON string representation of this JSON-RPC 2.0 message.
        Specified by:
        toJSONString in interface net.minidev.json.JSONAware
        Returns:
        The JSON object string representing this JSON-RPC 2.0 message.
        See Also:
        toString()
      • toString

        public String toString()
        Serialises this JSON-RPC 2.0 message to a JSON object string.
        Overrides:
        toString in class Object
        Returns:
        The JSON object string representing this JSON-RPC 2.0 message.