List of usage examples for org.json.simple.parser ParseException ERROR_UNEXPECTED_TOKEN
int ERROR_UNEXPECTED_TOKEN
To view the source code for org.json.simple.parser ParseException ERROR_UNEXPECTED_TOKEN.
Click Source Link
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//from w w w . ja v a 2 s .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.jitsi.impl.reservation.rest.json.AbstractJsonHandler.java
/** * Utility method for checking if given <tt>primitive</tt> is a * <tt>String</tt>./* w w w. jav a 2 s. c o m*/ * * @param primitive the object to check * * @throws ParseException if given <tt>primitive</tt> is not instance of * <tt>String</tt> */ protected void assertString(Object primitive) throws ParseException { if (!(primitive instanceof String)) { throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN, primitive); } }
From source file:org.jitsi.impl.reservation.rest.json.AbstractJsonHandler.java
/** * Utility method for checking if given <tt>primitive</tt> is a * <tt>Number</tt>.// w w w. ja v a 2s . c o m * * @param primitive the object to check * * @throws ParseException if given <tt>primitive</tt> is not instance of * <tt>Number</tt> */ protected void assertNumber(Object primitive) throws ParseException { if (!(primitive instanceof Number)) { throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN, primitive); } }
From source file:org.jitsi.impl.reservation.rest.json.ConferenceJsonHandler.java
/** * {@inheritDoc}//ww w .ja va 2 s. c om */ @Override public boolean primitive(Object primitive) throws ParseException, IOException { if (CONF_ID_KEY.equals(currentKey)) { assertNumber(primitive); editedInstance.setId((Number) primitive); } else if (CONF_NAME_KEY.equals(currentKey)) { assertString(primitive); if (checkImmutableString(editedInstance.getName(), (String) primitive, CONF_NAME_KEY)) { editedInstance.setName((String) primitive); } } else if (CONF_OWNER_KEY.equals(currentKey)) { if (checkImmutableString(editedInstance.getOwner(), (String) primitive, CONF_OWNER_KEY)) { editedInstance.setOwner((String) primitive); } } else if (CONF_URL_KEY.equals(currentKey)) { assertString(primitive); editedInstance.setUrl((String) primitive); } else if (CONF_PIN_KEY.equals(currentKey)) { assertString(primitive); editedInstance.setPin((String) primitive); } else if (CONF_START_TIME_KEY.equals(currentKey)) { assertString(primitive); try { editedInstance.setStartTime(DATE_FORMAT.parse((String) primitive)); } catch (java.text.ParseException e) { logger.error(e, e); throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN, primitive); } } else if (CONF_DURATION_KEY.equals(currentKey)) { assertNumber(primitive); editedInstance.setDuration((Long) primitive); } else if (CONF_SIP_ID_KEY.equals(currentKey)) { if (primitive instanceof Number) { editedInstance.setSipId((Number) primitive); } else { throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN, primitive); } } return true; }
From source file:project.cs.netinfutilities.metadata.MetadataParser.java
/** * Returns a map that represents the meta-data key value pairs * contained in the specified meta-data. * * @param metadata The JSON object corresponding to the meta-data. * @return The map with all meta-data values * @throws JSONException Thrown, if no meta-data could be extracted at all *//*from w w w . ja v a 2 s. c o m*/ public static Map<String, Object> toMap(JSONObject metadata) throws ParseException { Map<String, Object> map = new LinkedHashMap<String, Object>(); metadata = (JSONObject) metadata.get("meta"); if (metadata == null) { throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN, "\"meta\" tag not present in JSON Object"); } // We iterate through the metadata by getting a set of keys from the metadata and // we walk through that set. @SuppressWarnings("unchecked") Set<String> keys = (Set<String>) metadata.keySet(); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()) { String key = iterator.next(); Object value; value = metadata.get(key); if (value instanceof JSONArray) { List<String> list = extractList((JSONArray) value); map.put(key, list); } else { map.put(key, value); } } if (map.size() == 0) { throw new ParseException(ParseException.ERROR_UNEXPECTED_EXCEPTION, "No meta-data could be extracted."); } return map; }