public static String getMapAsJson(Map<String, Object> objectMap) { String nullString = "null"; StringBuffer sb = new StringBuffer("{"); boolean firstIteration = true; for (Entry<String, Object> entry : objectMap.entrySet()) { if (!firstIteration) { sb.append(','); } sb.append(JSONObject.quote(entry.getKey())); sb.append(':'); Object value = entry.getValue(); if (value == null) { sb.append(nullString); } else if (value instanceof Boolean) { sb.append(((Boolean) value).booleanValue()); } else if (value instanceof String) { sb.append(JSONObject.quote(value.toString())); } else { sb.append(value.toString()); } firstIteration = false; } sb.append("}"); return sb.toString(); }
public String getHiddenFieldsJson() { StringBuilder sb = new StringBuilder(); sb.append("{"); sb.append("\"hiddenFields\":["); for (int j=0;j<hiddenFields.size();j++) { sb.append("{\"name\":\""); sb.append(hiddenFields.get(j).getName()); sb.append("\",\"val\":"); sb.append(JSONObject.quote(hiddenFields.get(j).getValue())); sb.append("}"); if (j < hiddenFields.size()-1) { sb.append(","); } } sb.append("]}"); return sb.toString(); }
/** * Produce a string in double quotes with backslash sequences in all the * right places. A backslash will be inserted within </, allowing JSON * text to be delivered in HTML. In JSON text, a string cannot contain a * control character or an unescaped quote or backslash. * @param string A String * @return A String correctly formatted for insertion in a JSON text. */ public static String quote(String string) { return quote(string, true); } public static String quote(String string, boolean escapeForwardSlashAlways) {
private double doGetDouble(String key, Object o) throws JSONException { try { return o instanceof Number ? ((Number)o).doubleValue() : Double.valueOf((String)o).doubleValue(); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); } }
private boolean doGetBoolean(String key, Object o) throws JSONException { if (o.equals(Boolean.FALSE) || (o instanceof String && ((String)o).equalsIgnoreCase("false"))) { return false; } else if (o.equals(Boolean.TRUE) || (o instanceof String && ((String)o).equalsIgnoreCase("true"))) { return true; } throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean."); }
sb.append(quote(o.toString(), escapeForwardSlashAlways)); sb.append(':'); sb.append(valueToString(this.myHashMap.get(o), escapeForwardSlashAlways));
/** * Get the value object associated with a key. * * @param key A key string. * @return The object associated with the key. * @throws JSONException if the key is not found. */ public Object get(String key) throws JSONException { Object o = opt(key); if (o == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } return o; }
/** * Get the JSONObject value associated with a key. * * @param key A key string. * @return A JSONObject which is the value. * @throws JSONException if the key is not found or * if the value is not a JSONObject. */ public JSONObject getJSONObject(String key) throws JSONException { Object o = get(key); if (o instanceof JSONObject) { return (JSONObject)o; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the JSONArray value associated with a key. * * @param key A key string. * @return A JSONArray which is the value. * @throws JSONException if the key is not found or * if the value is not a JSONArray. */ public JSONArray getJSONArray(String key) throws JSONException { Object o = get(key); if (o instanceof JSONArray) { return (JSONArray)o; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Append a key. The key will be associated with the next value. In an * object, every value must be preceded by a key. * @param s A key string. * @return this * @throws JSONException If the key is out of place. For example, keys * do not belong in arrays or if the key is null. */ public JSONWriter key(String s) throws JSONException { if (s == null) { throw new JSONException("Null key."); } if (this.mode == 'k') { try { if (this.comma) { this.writer.write(','); } this.writer.write(JSONObject.quote(s)); this.writer.write(':'); this.comma = false; this.mode = 'o'; return this; } catch (IOException e) { throw new JSONException(e); } } throw new JSONException("Misplaced key."); }
&& ignoredElements != null && ignoredElements.contains(k); if (!mayBeDropSimpleElement) { writer.write(quote(k, escapeForwardSlashAlways)); writer.write(':');
return value.toString(); return quote(value.toString(), escapeForwardSlash);
return ((JSONArray)value).toString(indentFactor, indent); return quote(value.toString(), escapeForwardSlash);
@Override public Promise<Void> addWatcher(final URI watchersUri, final String username) { return post(watchersUri, JSONObject.quote(username)); }
@Override public Promise<Void> addWatcher(final URI watchersUri, final String username) { return post(watchersUri, JSONObject.quote(username)); }
@Override public Promise<Void> addWatcher(final URI watchersUri, @Nullable final String username) { Request request = client.newRequest(watchersUri); if (username != null) { request.setEntity(JSONObject.quote(username)); } return call(request.post()); }
@Override public Void call() throws Exception { final WebResource.Builder builder = client.resource(watchersUri).type(MediaType.APPLICATION_JSON_TYPE); if (username != null) { builder.post(JSONObject.quote(username)); } else { builder.post(); } return null; } });
@Override public Void call() throws Exception { final WebResource.Builder builder = client.resource(watchersUri).type(MediaType.APPLICATION_JSON_TYPE); if (username != null) { builder.post(JSONObject.quote(username)); } else { builder.post(); } return null; } });