Advertisement
Guest User

JsonHandler.java

a guest
Apr 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. public class JsonHandler {
  2.    
  3.     private JsonObject jsonRequest;
  4.     private JsonObject jsonResponse;
  5.     private JsonArray jsonBatch;
  6.  
  7.     private static JsonValue jsonRpc;
  8.     private static JsonValue method;
  9.     private static JsonValue id;
  10.     private static JsonValue result;
  11.     private static JsonValue error;
  12.    
  13.     private static JsonArray params;
  14.    
  15.     public JsonHandler(String request) {
  16.        
  17.         if (isBatch(request) == true) {
  18.            
  19.             parseBatchRequest(request);
  20.        
  21.         } else {
  22.            
  23.             jsonRequest = request;
  24.        
  25.         }
  26.    
  27.     }
  28.    
  29.     private void parseJsonRequest(JsonObject obj) {
  30.        
  31.         jsonRequest = obj
  32.        
  33.         jsonRpc = jsonRequest.get('jsonrpc').asString();
  34.         method = jsonRequest.get('method').asString();
  35.         id = jsonRequest.get('id').asInt();
  36.         params = jsonRequest.get('params').asArray();
  37.        
  38.     }
  39.    
  40.     private bool isBatch(String batchRequest) {
  41.        
  42.         JsonArray batchRequest = Json.parse(batchRequest).asArray.isArray;
  43.        
  44.         if (batchRequest == true) {
  45.            
  46.             return true;
  47.            
  48.         }
  49.        
  50.         return false;
  51.        
  52.     }
  53.    
  54.     private void parseBatchRequest(String batchRequest) {
  55.        
  56.         batchRequest = batchRequest.replaceAll('\\r\\s\\t', '');
  57.        
  58.         batchRequest = Json.parse(batchRequest).asArray;
  59.        
  60.         for (JsonObject obj: batchRequest) {
  61.            
  62.             parseJsonRequest(batchRequest[obj]);
  63.            
  64.         }
  65.    
  66.     }
  67.    
  68.     public String getJsonRpc() {
  69.        
  70.         return getJsonRpc;
  71.    
  72.     }
  73.    
  74.     public String getMethod() {
  75.        
  76.         return method;
  77.    
  78.     }
  79.    
  80.     public String[] getParams() {
  81.        
  82.         return params;
  83.    
  84.     }
  85.    
  86.     public int getId() {
  87.        
  88.         return id;
  89.    
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement