Advertisement
Guest User

JsonHandler.java

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