/** * Put a key/boolean pair in the JsonObject. * * @param key A key string. * @param value A boolean which is the value. * @return this. * @throws com.restfb.json.JsonException If the key is null. */ public JsonObject put(String key, boolean value) throws JsonException { put(key, value ? Boolean.TRUE : Boolean.FALSE); return this; }
/** * Put a key/value pair in the JsonObject, but only if the key and the value * are both non-null. * * @param key A key string. * @param value An object which is the value. It should be of one of these types: * Boolean, Double, Integer, JsonArray, JsonObject, Long, String, or * the JsonObject.NULL object. * @return this. * @throws com.restfb.json.JsonException If the value is a non-finite number. */ public JsonObject putOpt(String key, Object value) throws JsonException { if (key != null && value != null) { put(key, value); } return this; }
/** * Put a key/int pair in the JsonObject. * * @param key A key string. * @param value An int which is the value. * @return this. * @throws com.restfb.json.JsonException If the key is null. */ public JsonObject put(String key, int value) throws JsonException { put(key, new Integer(value)); return this; }
/** * Put a key/long pair in the JsonObject. * * @param key A key string. * @param value A long which is the value. * @return this. * @throws com.restfb.json.JsonException If the key is null. */ public JsonObject put(String key, long value) throws JsonException { put(key, new Long(value)); return this; }
/** * Put a key/double pair in the JsonObject. * * @param key A key string. * @param value A double which is the value. * @return this. * @throws com.restfb.json.JsonException If the key is null or if the number is invalid. */ public JsonObject put(String key, double value) throws JsonException { put(key, new Double(value)); return this; }
/** * Put a key/value pair in the JsonObject, where the value will be a * JsonObject which is produced from a Map. * * @param key A key string. * @param value A Map value. * @return this. * @throws com.restfb.json.JsonException */ public JsonObject put(String key, Map<?, ?> value) throws JsonException { put(key, new JsonObject(value)); return this; }
/** * Put a key/value pair in the JsonObject, where the value will be a JsonArray * which is produced from a Collection. * * @param key A key string. * @param value A Collection value. * @return this. * @throws com.restfb.json.JsonException */ public JsonObject put(String key, Collection<?> value) throws JsonException { put(key, new JsonArray(value)); return this; }
/** * Put a key/value pair in the JsonObject, but only if the key and the value * are both non-null, and only if there is not already a member with that * name. * * @param key * @param value * @return his. * @throws com.restfb.json.JsonException if the key is a duplicate */ public JsonObject putOnce(String key, Object value) throws JsonException { if (key != null && value != null) { if (opt(key) != null) { throw new JsonException("Duplicate key \"" + key + "\""); } put(key, value); } return this; }
/** * Given a map of query names to queries, verify that it contains valid data * and convert it to a JSON object string. * * @param queries The query map to convert. * @return The {@code queries} in JSON string format. * @throws IllegalArgumentException If the provided {@code queries} are invalid. */ protected String queriesToJson(Map<String, String> queries) { verifyParameterPresence("queries", queries); if (queries.keySet().size() == 0) throw new IllegalArgumentException("You must specify at least one query."); JsonObject jsonObject = new JsonObject(); for (Entry<String, String> entry : queries.entrySet()) { if (isBlank(entry.getKey()) || isBlank(entry.getValue())) throw new IllegalArgumentException("Provided queries must have non-blank keys and values. " + "You provided: " + queries); try { jsonObject.put(trimToEmpty(entry.getKey()), trimToEmpty(entry.getValue())); } catch (JsonException e) { // Shouldn't happen unless bizarre input is provided throw new IllegalArgumentException("Unable to convert " + queries + " to JSON.", e); } } return jsonObject.toString(); }
/** * Append values to the array under a key. If the key does not exist in the * JsonObject, then the key is put in the JsonObject with its value being a * JsonArray containing the value parameter. If the key was already associated * with a JsonArray, then the value parameter is appended to it. * * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws com.restfb.json.JsonException If the key is null or if the current value associated with the * key is not a JsonArray. */ public JsonObject append(String key, Object value) throws JsonException { testValidity(value); Object o = opt(key); if (o == null) { put(key, new JsonArray().put(value)); } else if (o instanceof JsonArray) { put(key, ((JsonArray) o).put(value)); } else { throw new JsonException("JsonObject[" + key + "] is not a JsonArray."); } return this; }
jsonObject.put((String) entry.getKey(), toJsonInternal(entry.getValue())); } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to process value '" + entry.getValue() + "' for key '" jsonObject.put(facebookFieldName, toJsonInternal(fieldWithAnnotation.getField().get(object))); } catch (Exception e) { throw new FacebookJsonMappingException("Unable to process field '" + facebookFieldName + "' for "
/** * Accumulate values under a key. It is similar to the put method except that * if there is already an object stored under the key then a JsonArray is * stored under the key to hold all of the accumulated values. If there is * already a JsonArray, then the new value is appended to it. In contrast, the * put method replaces the previous value. * * @param key A key string. * @param value An object to be accumulated under the key. * @return this. * @throws com.restfb.json.JsonException If the value is an invalid number or if the key is null. */ public JsonObject accumulate(String key, Object value) throws JsonException { testValidity(value); Object o = opt(key); if (o == null) { put(key, value instanceof JsonArray ? new JsonArray().put(value) : value); } else if (o instanceof JsonArray) { ((JsonArray) o).put(value); } else { put(key, new JsonArray().put(o).put(value)); } return this; }
: new JsonArray(); normalizedJson.put(jsonObject.getString("name"), resultsArray);
/** * Produce a JsonObject by combining a JsonArray of names with the values of * this JsonArray. * * @param names A JsonArray containing a list of key strings. These will be paired * with the values. * @return A JsonObject, or null if there are no names or if this JsonArray * has no values. * @throws JsonException If any of the names are null. */ public JsonObject toJsonObject(JsonArray names) throws JsonException { if (names == null || names.length() == 0 || length() == 0) { return null; } JsonObject jo = new JsonObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
: new JsonArray(); normalizedJson.put(jsonObject.getString("name"), resultsArray);