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

The following examples show how to use org.json.JSONObject#quote() . 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 File: JobService.java    From firebase-jobdispatcher-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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 File: FileUploadResult.java    From reader with 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
Source File: FileUploadResult.java    From L.TileLayer.Cordova with 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 4
Source File: FileUploadResult.java    From reader with 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 File: FileUploadResult.java    From reader with 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
Source File: JobInvocation.java    From firebase-jobdispatcher-android with Apache License 2.0 5 votes vote down vote up
@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 File: FileUploadResult.java    From reader with 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 File: FileUploadResult.java    From jpHolo with 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 File: JSONObjectTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: ApiUtils.java    From ForPDA with 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 11
Source File: PluginResult.java    From countly-sdk-cordova with MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 12
Source File: GfJsonObject.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static String quote(String string) {
  return JSONObject.quote(string);
}
 
Example 13
Source File: PluginResult.java    From crosswalk-cordova-android with Apache License 2.0 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 14
Source File: PluginResult.java    From reader with MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 15
Source File: PluginResult.java    From cordova-plugin-intent with MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 16
Source File: PluginResult.java    From chappiecast with Mozilla Public License 2.0 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 17
Source File: PluginResult.java    From pychat with MIT License 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}
 
Example 18
Source File: JSONWriter.java    From json-schema with 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 File: PluginResult.java    From lona with 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 File: PluginResult.java    From a2cardboard with Apache License 2.0 4 votes vote down vote up
public String getMessage() {
    if (encodedMessage == null) {
        encodedMessage = JSONObject.quote(strMessage);
    }
    return encodedMessage;
}