/** * Creates a new empty JsonObject. This is equivalent to creating a new JsonObject using the constructor. * * @return a new empty JSON object */ public static JsonObject object() { return new JsonObject(); }
@Override public JsonObject startObject() { return new JsonObject(); }
/** * Returns an unmodifiable JsonObject for the specified one. This method allows to provide read-only access to a * JsonObject. * <p> * The returned JsonObject is backed by the given object and reflect changes that happen to it. Attempts to modify the * returned JsonObject result in an <code>UnsupportedOperationException</code>. * </p> * * @param object * the JsonObject for which an unmodifiable JsonObject is to be returned * @return an unmodifiable view of the specified JsonObject */ public static JsonObject unmodifiableObject(JsonObject object) { return new JsonObject(object, true); }
/** * Put a value in the JsonArray, where the value will be a JsonObject which is * produced from a Map. * * @param value A Map value. * @return this. */ public JsonArray put(Map<?, ?> value) { put(new JsonObject(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 value in the JsonArray, where the value will be a JsonObject which is * produced from a Map. * * @param index The subscript. * @param value The Map value. * @return this. * @throws JsonException If the index is negative or if the the value is an invalid * number. */ public JsonArray put(int index, Map<?, ?> value) throws JsonException { put(index, new JsonObject(value)); return this; }
/** * Construct a JsonArray from a collection of beans. The collection should * have Java Beans. * * @throws JsonException If not an array. */ public JsonArray(Collection<?> collection, boolean includeSuperClass) { this.myArrayList = new ArrayList<Object>(); if (collection != null) { Iterator<?> iter = collection.iterator(); ; while (iter.hasNext()) { Object o = iter.next(); if (o instanceof Map<?, ?>) { this.myArrayList.add(new JsonObject((Map<?, ?>) o, includeSuperClass)); } else if (!JsonObject.isStandardProperty(o.getClass())) { this.myArrayList.add(new JsonObject(o, includeSuperClass)); } else { this.myArrayList.add(o); } } } }
/** * Construct a JsonObject from a Map. * <p/> * Note: Use this constructor when the map contains <key,bean>. * * @param map - A map with Key-Bean data. * @param includeSuperClass - Tell whether to include the super class properties. */ public JsonObject(Map<Object, Object> map, boolean includeSuperClass) { this.map = new HashMap<Object, Object>(); if (map != null) { Iterator<Map.Entry<Object, Object>> i = map.entrySet().iterator(); while (i.hasNext()) { Map.Entry<Object, Object> e = i.next(); if (isStandardProperty(e.getValue().getClass())) { this.map.put(e.getKey(), e.getValue()); } else { this.map.put(e.getKey(), new JsonObject(e.getValue(), includeSuperClass)); } } } }
/** * Construct a JsonArray from an array with a bean. The array should have Java * Beans. * * @throws JsonException If not an array. */ public JsonArray(Object array, boolean includeSuperClass) throws JsonException { this(); if (array.getClass().isArray()) { int length = Array.getLength(array); for (int i = 0; i < length; i += 1) { Object o = Array.get(array, i); if (JsonObject.isStandardProperty(o.getClass())) { this.myArrayList.add(o); } else { this.myArrayList.add(new JsonObject(o, includeSuperClass)); } } } else { throw new JsonException("JsonArray initial value should be a string or collection or array."); } }
/** * 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(); }
new JsonObject(makeRequest("", parametersWithAdditionalParameter(Parameter.with(IDS_PARAM_NAME, join(ids)), parameters)));
/** * Begin appending a new object. All keys and values until the balancing * <code>endObject</code> will be appended to this object. The * <code>endObject</code> method must be called to mark the object's end. * * @return this * @throws com.restfb.json.JsonException If the nesting is too deep, or if the object is started in the * wrong place (for example as a key or after the end of the * outermost array or object). */ public JsonWriter object() throws JsonException { if (this.mode == 'i') { this.mode = 'o'; } if (this.mode == 'o' || this.mode == 'a') { this.append("{"); this.push(new JsonObject()); this.comma = false; return this; } throw new JsonException("Misplaced object."); }
case '{': back(); return new JsonObject(this); case '[': case '(':
errorObject = new JsonObject(json); } catch (JsonException e) {
JsonObject normalizedJson = new JsonObject();
@SuppressWarnings("unchecked") protected <T> Connection<T> mapToConnection(String connectionJson, Class<T> connectionType) { List<T> data = new ArrayList<T>(); String previous = null; String next = null; try { JsonObject jsonObject = new JsonObject(connectionJson); // Pull out data JsonArray jsonData = jsonObject.getJsonArray("data"); for (int i = 0; i < jsonData.length(); i++) data.add(connectionType.equals(JsonObject.class) ? (T) jsonData.get(i) : jsonMapper.toJavaObject(jsonData .get(i).toString(), connectionType)); // Pull out paging info, if present if (jsonObject.has("paging")) { JsonObject jsonPaging = jsonObject.getJsonObject("paging"); previous = jsonPaging.has("previous") ? jsonPaging.getString("previous") : null; next = jsonPaging.has("next") ? jsonPaging.getString("next") : null; } } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to map connection JSON to Java objects", e); } return new Connection<T>(data, previous, next); }
/** * 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; }
return; JsonObject errorObject = new JsonObject(json);
parametersWithAdditionalParameter(Parameter.with(QUERIES_PARAM_NAME, queriesToJson(queries)), parameters))); JsonObject normalizedJson = new JsonObject();
JsonObject result = new JsonObject();