List of usage examples for org.json.simple.parser ParseException getErrorType
public int getErrorType()
From source file:com.tobedevoured.json.SimpleStream.java
/** * Process the buffer into json entities. When allow for multiple attempts for malformed json, which * can be caused by a json fragment in the stream. * * @param allowedMalformedAttempts int/* w w w . j ava2s . c o m*/ * @return List * @throws StreamException parser error */ public List processBuffer(int allowedMalformedAttempts) throws StreamException { JSONParser parser = new JSONParser(); JsonStreamHandler streamHandler = new JsonStreamHandler(); List entities = new ArrayList(); int pos = 0; // Check if the buffer is crammed to the brim boolean bufferOverflow = buffer.length() > bufferSize; while (pos < buffer.length() - 1) { String fragment = buffer.substring(pos); try { parser.parse(fragment, streamHandler); } catch (ParseException e) { if (ParseException.ERROR_UNEXPECTED_CHAR == e.getErrorType()) { // Check if should wait for more streaming json before declaring it malformed if (malformedFragmentAttempts < allowedMalformedAttempts) { malformedFragmentAttempts++; } else { throw new StreamException(e); } } else if (ParseException.ERROR_UNEXPECTED_TOKEN != e.getErrorType()) { throw new StreamException(e); } logger.debug("detected json fragment, buffering for the rest: {}", fragment); return entities; } // increment the position in the buffer int currentPos = pos; pos = pos + parser.getPosition() + 1; // Create entity from buffer Object val = null; try { val = parser.parse(buffer.substring(currentPos, pos).trim()); buffer.delete(currentPos, pos); pos = 0; } catch (ParseException e) { throw new StreamException(e); } // valid entity created, reset the malformed counter malformedFragmentAttempts = 0; if (callback != null) { logger.info("Executing callback with {}", val); try { callback.apply(val); } catch (Exception e) { logger.error("Failed to execute callback", e); throw new StreamException(e); } } entities.add(val); } // Compact the buffer after an overflow if (buffer.length() < bufferSize && bufferOverflow) { buffer.setLength(bufferSize); } return entities; }
From source file:org.ScripterRon.JavaBitcoin.RpcHandler.java
/** * Handle a JSON-RPC request/*from ww w . ja va 2s. c o m*/ * * @param exchange HTTP exchange * @throws IOException I/O exception * @return The response in JSON format */ private String processRequest(HttpExchange exchange) throws IOException { String method = ""; Object params = null; Object id = null; Object result = null; int errorCode = 0; String errorMessage = ""; // // Parse the request // try (InputStreamReader in = new InputStreamReader(exchange.getRequestBody(), "UTF-8")) { JSONParser parser = new JSONParser(); Object object = parser.parse(in); if (object == null || !(object instanceof JSONObject)) { errorCode = RPC_INVALID_REQUEST; errorMessage = "The request must be a JSON structured object"; } else { JSONObject request = (JSONObject) object; object = request.get("method"); if (object == null || !(object instanceof String)) { errorCode = RPC_INVALID_REQUEST; errorMessage = "The request must include the 'method' field"; } else { method = (String) object; params = request.get("params"); id = request.get("id"); } } } catch (ParseException exc) { errorCode = RPC_INVALID_REQUEST; errorMessage = String.format("Parse exception: Position %d, Code %d", exc.getPosition(), exc.getErrorType()); log.error(errorMessage); } catch (Throwable exc) { errorCode = RPC_INTERNAL_ERROR; errorMessage = "Unable to parse request"; log.error(errorMessage, exc); } // // Process the request // if (errorCode == 0) { try { switch (method.toLowerCase()) { case "getinfo": result = getInfo(); break; case "getlog": result = getLog(); break; case "getpeerinfo": result = getPeerInfo(); break; case "getblock": result = getBlock(params); break; case "getblockhash": result = getBlockHash(params); break; case "getstacktraces": result = getStackTraces(); break; default: errorCode = RPC_METHOD_NOT_FOUND; errorMessage = String.format("Method '%s' is not recognized", method); } } catch (BlockStoreException exc) { errorCode = RPC_DATABASE_ERROR; errorMessage = "Unable to access database"; } catch (RequestException exc) { errorCode = exc.getCode(); errorMessage = exc.getMessage(); } catch (IllegalArgumentException exc) { errorCode = RPC_INVALID_PARAMETER; errorMessage = exc.getMessage(); } } // // Return the response // JSONObject response = new JSONObject(); if (errorCode != 0) { JSONObject error = new JSONObject(); error.put("code", errorCode); error.put("message", errorMessage); response.put("error", error); } else { response.put("result", result); } if (id != null) response.put("id", id); return response.toJSONString(); }