Java Code Examples for org.json.JSONObject#quote()

The following examples show how to use org.json.JSONObject#quote() . 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
/**
 * Package-private alias for {@link #dump(FileDescriptor, PrintWriter, String[])}.
 *
 * <p>The {@link #dump(FileDescriptor, PrintWriter, String[])} method is protected. This
 * implementation method is marked package-private to facilitate testing.
 */
@VisibleForTesting
final void dumpImpl(PrintWriter writer) {
  synchronized (runningJobs) {
    if (runningJobs.isEmpty()) {
      writer.println("No running jobs");
      return;
    }

    long now = SystemClock.elapsedRealtime();

    writer.println("Running jobs:");
    for (int i = 0; i < runningJobs.size(); i++) {
      JobCallback callback = runningJobs.get(runningJobs.keyAt(i));

      // Add sanitized quotes around the tag to make this easier to parse for robots
      String name = JSONObject.quote(callback.job.getTag());
      // Produces strings like "02:30"
      String duration =
          DateUtils.formatElapsedTime(MILLISECONDS.toSeconds(now - callback.startedAtElapsed));

      writer.println("    * " + name + " has been running for " + duration);
    }
  }
}
 
Example 2
Source Project: reader   File: FileUploadResult.java    License: MIT License 5 votes vote down vote up
public JSONObject toJSONObject() throws JSONException {
    return new JSONObject(
            "{bytesSent:" + bytesSent +
            ",responseCode:" + responseCode +
            ",response:" + JSONObject.quote(response) +
            ",objectId:" + JSONObject.quote(objectId) + "}");
}
 
Example 3
/**
 * Exercise JSONObject quote() method
 * This purpose of quote() is to ensure that for strings with embedded
 * quotes, the quotes are properly escaped.
 */
@Test
public void jsonObjectQuote() {
    String str;
    str = "";
    String quotedStr;
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped quotes, found "+quotedStr,
            "\"\"".equals(quotedStr));
    str = "\"\"";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped quotes, found "+quotedStr,
            "\"\\\"\\\"\"".equals(quotedStr));
    str = "</";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped frontslash, found "+quotedStr,
            "\"<\\/\"".equals(quotedStr));
    str = "AB\bC";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped backspace, found "+quotedStr,
            "\"AB\\bC\"".equals(quotedStr));
    str = "ABC\n";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped newline, found "+quotedStr,
            "\"ABC\\n\"".equals(quotedStr));
    str = "AB\fC";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped formfeed, found "+quotedStr,
            "\"AB\\fC\"".equals(quotedStr));
    str = "\r";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped return, found "+quotedStr,
            "\"\\r\"".equals(quotedStr));
    str = "\u1234\u0088";
    quotedStr = JSONObject.quote(str);
    assertTrue("quote() expected escaped unicode, found "+quotedStr,
            "\"\u1234\\u0088\"".equals(quotedStr));
}
 
Example 4
Source Project: jpHolo   File: FileUploadResult.java    License: MIT License 5 votes vote down vote up
public JSONObject toJSONObject() throws JSONException {
    return new JSONObject(
            "{bytesSent:" + bytesSent +
            ",responseCode:" + responseCode +
            ",response:" + JSONObject.quote(response) +
            ",objectId:" + JSONObject.quote(objectId) + "}");
}
 
Example 5
Source Project: reader   File: FileUploadResult.java    License: MIT License 5 votes vote down vote up
public JSONObject toJSONObject() throws JSONException {
    return new JSONObject(
            "{bytesSent:" + bytesSent +
            ",responseCode:" + responseCode +
            ",response:" + JSONObject.quote(response) +
            ",objectId:" + JSONObject.quote(objectId) + "}");
}
 
Example 6
@Override
public String toString() {
  return "JobInvocation{"
      + "tag='"
      + JSONObject.quote(tag)
      + '\''
      + ", service='"
      + service
      + '\''
      + ", trigger="
      + trigger
      + ", recurring="
      + recurring
      + ", lifetime="
      + lifetime
      + ", constraints="
      + Arrays.toString(constraints)
      + ", extras="
      + extras
      + ", retryStrategy="
      + retryStrategy
      + ", replaceCurrent="
      + replaceCurrent
      + ", triggerReason="
      + triggerReason
      + '}';
}
 
Example 7
Source Project: reader   File: FileUploadResult.java    License: MIT License 5 votes vote down vote up
public JSONObject toJSONObject() throws JSONException {
    return new JSONObject(
            "{bytesSent:" + bytesSent +
            ",responseCode:" + responseCode +
            ",response:" + JSONObject.quote(response) +
            ",objectId:" + JSONObject.quote(objectId) + "}");
}
 
Example 8
Source Project: reader   File: FileUploadResult.java    License: MIT License 5 votes vote down vote up
public JSONObject toJSONObject() throws JSONException {
    return new JSONObject(
            "{bytesSent:" + bytesSent +
            ",responseCode:" + responseCode +
            ",response:" + JSONObject.quote(response) +
            ",objectId:" + JSONObject.quote(objectId) + "}");
}
 
Example 9
Source Project: L.TileLayer.Cordova   File: FileUploadResult.java    License: MIT License 5 votes vote down vote up
public JSONObject toJSONObject() throws JSONException {
    return new JSONObject(
            "{bytesSent:" + bytesSent +
            ",responseCode:" + responseCode +
            ",response:" + JSONObject.quote(response) +
            ",objectId:" + JSONObject.quote(objectId) + "}");
}
 
Example 10
Source Project: pychat   File: PluginResult.java    License: MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 11
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 12
Source Project: cordova-plugin-intent   File: PluginResult.java    License: MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 13
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 14
Source Project: gemfirexd-oss   File: GfJsonObject.java    License: Apache License 2.0 4 votes vote down vote up
public static String quote(String string) {
  return JSONObject.quote(string);
}
 
Example 15
Source Project: ForPDA   File: ApiUtils.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static String escapeQuotes(String s) {
    String escaped = JSONObject.quote(s);
    escaped = escaped.substring(1, escaped.length() - 1);
    return escaped;
}
 
Example 16
Source Project: countly-sdk-cordova   File: PluginResult.java    License: MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 17
Source Project: reader   File: PluginResult.java    License: MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 18
Source Project: json-schema   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());
}
 
Example 19
Source Project: lona   File: PluginResult.java    License: GNU General Public License v3.0 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 20
Source Project: a2cardboard   File: PluginResult.java    License: Apache License 2.0 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}