Example usage for org.json.simple.parser ParseException ParseException

List of usage examples for org.json.simple.parser ParseException ParseException

Introduction

In this page you can find the example usage for org.json.simple.parser ParseException ParseException.

Prototype

public ParseException(int errorType, Object unexpectedObject) 

Source Link

Usage

From source file:org.alfresco.repo.remoteconnector.RemoteConnectorServiceImpl.java

public static JSONObject doExecuteJSONRequest(RemoteConnectorRequest request, RemoteConnectorService service)
        throws ParseException, IOException, AuthenticationException {
    // Set as JSON
    request.setContentType(MimetypeMap.MIMETYPE_JSON);

    // Perform the request
    RemoteConnectorResponse response = service.executeRequest(request);

    // Parse this as JSON
    JSONParser parser = new JSONParser();
    String jsonText = response.getResponseBodyAsString();
    Object json = parser.parse(jsonText);

    // Check it's the right type and return
    if (json instanceof JSONObject) {
        return (JSONObject) json;
    } else {// w w w.  j av  a 2 s .  c o m
        throw new ParseException(0, json);
    }
}

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>./*from w w w  . j  a  v  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>.// ww  w  . ja  va 2 s .c om
 *
 * @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.AbstractJsonHandler.java

/**
 * Methods is used to verify if given <tt>oldValue</tt> has not been
 * modified during parsing. It can be used to enforce read-only policy on
 * some fields. Exception will be thrown only if <tt>oldValue</tt> is not
 * <tt>null</tt> nor empty which means that it has been set on edited
 * instance.// w ww .  j  av  a  2s.  c  o m
 *
 * @param oldValue old value of the field to be checked. If <tt>null</tt>
 *                 no exception will be thrown which means that value has
 *                 not been assigned yet.
 * @param newValue the new value to be verified if it matches the
 *                 previous one(if old value is not null nor empty).
 * @param key the name of read-only key which will be included in
 *            exception message.
 *
 * @return <tt>true</tt> if read only policy has not been violated.
 *
 * @throws ParseException if <tt>oldValue</tt> is not null nor empty and
 *         <tt>newValue</tt> is not equal to the same as the old one.
 */
protected boolean checkImmutableString(String oldValue, String newValue, String key) throws ParseException {
    if (!StringUtils.isNullOrEmpty(oldValue) && !oldValue.equals(newValue)) {
        throw new ParseException(ParseException.ERROR_UNEXPECTED_EXCEPTION,
                "Attempt to modify immutable " + key + " property: " + oldValue + " -> " + newValue);
    } else {
        return true;
    }
}

From source file:org.jitsi.impl.reservation.rest.json.ConferenceJsonHandler.java

/**
 * {@inheritDoc}/*from  w  w w. j  av  a2 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   ww  w .  j  av a2s. 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;
}