All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thetransactioncompany.jsonrpc2.JSONRPC2Parser Maven / Gradle / Ivy

package com.thetransactioncompany.jsonrpc2;


import net.minidev.json.parser.JSONParser;
import net.minidev.json.writer.JsonReader;

import java.util.*;


/**
 * Parses JSON-RPC 2.0 request, notification and response messages. 
 *
 * 

Parsing of batched requests / notifications is not supported. * *

This class is not thread-safe. A parser instance should not be used by * more than one thread unless properly synchronised. Alternatively, you may * use the thread-safe {@link JSONRPC2Message#parse} and its sister methods. * *

Example: * *

 * String jsonString = "{\"method\":\"makePayment\"," +
 *                      "\"params\":{\"recipient\":\"Penny Adams\",\"amount\":175.05}," +
 *                      "\"id\":\"0001\","+
 *                      "\"jsonrpc\":\"2.0\"}";
 *  
 *  JSONRPC2Request req = null;
 *
 * JSONRPC2Parser parser = new JSONRPC2Parser();
 *  
 *  try {
 *          req = parser.parseJSONRPC2Request(jsonString);
 * 
 *  } catch (JSONRPC2ParseException e) {
 *          // handle exception
 *  }
 *
 * 
* *

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 */ public class JSONRPC2Parser { /** * JSON-RPC 2.0 parse options. */ public enum Option { /** * The order of the parsed JSON object members in parameters * and results must be preserved. */ PRESERVE_ORDER, /** * The {@code "jsonrpc":"2.0"} version member in the JSON-RPC * 2.0 message must be ignored. */ IGNORE_VERSION, /** * Non-standard JSON-RPC 2.0 message members must be parsed. */ PARSE_NON_STD_MEMBERS, /** * Allow a {@code null} "error" member in JSON-RPC 2.0 * responses. */ ALLOW_NULL_ERROR_IN_RESPONSE } /** * Reusable JSON parser. Not thread-safe! */ private final JSONParser parser; /** * The parse options. */ private final Set