Java Code Examples for org.json.JSONString

The following examples show how to use org.json.JSONString. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: json-schema   Source File: JSONWriter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Make a JSON text of an Object value. If the object has an
 * value.toJSONString() method, then that method will be used to produce the
 * JSON text. The method is required to produce a strictly conforming text.
 * If the object does not contain a toJSONString method (which is the most
 * common case), then a text will be produced by other means. If the value
 * is an array or Collection, then a JSONArray will be made from it and its
 * toJSONString method will be called. If the value is a MAP, then a
 * JSONObject will be made from it and its toJSONString method will be
 * called. Otherwise, the value's toString method will be called, and the
 * result will be quoted.
 * <p>
 * <p>
 * Warning: This method assumes that the data structure is acyclical.
 *
 * @param value
 *         The value to be serialized.
 * @return a printable, displayable, transmittable representation of the
 * object, beginning with <code>{</code>&nbsp;<small>(left
 * brace)</small> and ending with <code>}</code>&nbsp;<small>(right
 * brace)</small>.
 * @throws JSONException
 *         If the value is or contains an invalid number.
 */
static String valueToString(Object value) throws JSONException {
    if (value == null || value == JSONObject.NULL) {
        return "null";
    }

    if (implementsJSONString(value)) {
        String object;
        try {
            object = ((JSONString) value).toJSONString();
        } catch (Exception e) {
            throw new JSONException(e);
        }
        if (object != null) {
            return object;
        }
        throw new JSONException("Bad value from toJSONString: null");
    }
    if (value instanceof Number) {
        // not all Numbers may match actual JSON Numbers. i.e. Fractions or Complex
        final String numberAsString = JSONObject.numberToString((Number) value);
        if (NUMBER_PATTERN.matcher(numberAsString).matches()) {
            // Close enough to a JSON number that we will return it unquoted
            return numberAsString;
        }
        // The Number value is not a valid JSON number.
        // Instead we will quote it as a string
        return JSONObject.quote(numberAsString);
    }
    if (value instanceof Boolean || value instanceof JSONObject
            || value instanceof JSONArray) {
        return value.toString();
    }
    if (value instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) value;
        return new JSONObject(map).toString();
    }
    if (value instanceof Collection) {
        Collection<?> coll = (Collection<?>) value;
        return new JSONArray(coll).toString();
    }
    if (value.getClass().isArray()) {
        return new JSONArray(value).toString();
    }
    if (value instanceof Enum<?>) {
        return JSONObject.quote(((Enum<?>) value).name());
    }
    return JSONObject.quote(value.toString());
}