001package com.thetransactioncompany.jsonrpc2.examples;
002
003
004import com.thetransactioncompany.jsonrpc2.*;
005import com.thetransactioncompany.jsonrpc2.util.*;
006
007import java.util.*;
008
009
010/** 
011 * This example demonstrates the use of the utility classes 
012 * {@link com.thetransactioncompany.jsonrpc2.util.PositionalParamsRetriever} 
013 * and {@link com.thetransactioncompany.jsonrpc2.util.NamedParamsRetriever} to 
014 * extract parameters from incoming requests and notifications with proper type 
015 * and optional value checking.
016 *
017 * @author Vladimir Dzhuvinov
018 */
019public class Example3 {
020
021
022        public static void main(String[] args) {
023        
024                // Request string with 3 positional parameters
025                //    1. parameter "amount" - number, mandatory
026                //    2. parameter "recipient" - string, mandatory
027                //    3. parameter "confirmation" - boolean, optional
028                String json = "{" +
029                              "\"method\":\"makePayment\"," +
030                              "\"params\":{\"amount\":175.05,\"recipient\":\"Penny Adams\"}," +
031                              "\"id\":0," +
032                              "\"jsonrpc\":\"2.0\"" +
033                              "}";
034        
035                System.out.println("Parsing request JSON string: \n\n" + json + "\n\n");
036                
037                
038                // Parse the request
039                JSONRPC2Request request;
040                
041                try {
042                        request = JSONRPC2Request.parse(json);
043                        
044                } catch (JSONRPC2ParseException e) {
045                        System.err.println(e);
046                        return;
047                }
048                
049                String method = request.getMethod();
050                JSONRPC2ParamsType paramsType = request.getParamsType();
051                
052                System.out.println("Received request for method \"" + method + "\" " +
053                                   "with params of type " + paramsType);
054                
055                // Create named parameters instance from params Map
056                Map<String,Object> params = request.getNamedParams();
057                NamedParamsRetriever np = new NamedParamsRetriever(params);
058                
059                // Extract parameters
060                try {
061                        // Extract mandatory "recipient" parameter
062                        String recipient = np.getString("recipient");
063                        System.out.println("\tparam \"recipient\": " + recipient);
064                        
065                        // Extract mandatory "amount" parameter
066                        double amount = np.getDouble("amount");
067                        System.out.println("\tparam \"amount\": " + amount);
068                        
069                        // Extract optional "confirmation" parameters, defaults to false
070                        // if undefined
071                        boolean confirmation = np.getOptBoolean("confirmation", false);
072                        System.out.println("\tparam \"confirmation\" [optional]: " + confirmation);
073                
074                } catch (JSONRPC2Error e) {
075                        // If a mandatory parameter is missing or there is a type mismatch
076                        // you get an exception
077                        System.err.println(e);
078                }
079        }
080}