Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between JSONObject accumulate and put?

I have been dabbling with JSON and I see that in the documentation (JAVA), JSONObject's put() and accumulate() pretty much do the same thing?

What is that about?

like image 548
Chintan Shah Avatar asked Mar 05 '15 14:03

Chintan Shah


People also ask

What is JSONObject put?

JSONObject. put(String key, java.util.Collection value) Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.

What is the difference between JSONObject and JSONObject?

JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one. I had some issue with the date formatting in JSONObject.

What is the difference between JSONObject and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

Does JSONObject maintain order?

The JSON Data Interchange Standard definition at json.org specifies that “An object is an unordered [emphasis mine] set of name/value pairs”, whereas an array is an “ordered collection of values”. In other words, by definition the order of the key/value pairs within JSON objects simply does not, and should not, matter.


1 Answers

I saw the Java Source Code for JSONObject and the difference between accumulate and put is that with accumulate(String key,Object Value), if there exists some value for "key" then the Object is checked for being an array, if it is an array then the "value" is added to the array else an array is created for this key.

In put, however, the key if it exists, it's value is replaced by the value - "value"

Here is the source of JSONObject accumulate(String key, Object Value)

/**
 * Appends {@code value} to the array already mapped to {@code name}. If
 * this object has no mapping for {@code name}, this inserts a new mapping.
 * If the mapping exists but its value is not an array, the existing
 * and new values are inserted in order into a new array which is itself
 * mapped to {@code name}. In aggregate, this allows values to be added to a
 * mapping one at a time.
 *
 * <p> Note that {@code append(String, Object)} provides better semantics.
 * In particular, the mapping for {@code name} will <b>always</b> be a
 * {@link JSONArray}. Using {@code accumulate} will result in either a
 * {@link JSONArray} or a mapping whose type is the type of {@code value}
 * depending on the number of calls to it.
 *
 * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
 *     Integer, Long, Double, {@link #NULL} or null. May not be {@link
 *     Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
 */

  public JSONObject accumulate(String name, Object value) throws JSONException {
    Object current = nameValuePairs.get(checkName(name));
    if (current == null) {
        return put(name, value);
    }

    if (current instanceof JSONArray) {
        JSONArray array = (JSONArray) current;
        array.checkedPut(value);
    } else {
        JSONArray array = new JSONArray();
        array.checkedPut(current);
        array.checkedPut(value);
        nameValuePairs.put(name, array);
    }
    return this;
}

And here is the code for JSONObject put (String key, Object value)

 /**
 * Maps {@code name} to {@code value}, clobbering any existing name/value
 * mapping with the same name.
 *
 * @return this object.
 */
public JSONObject put(String name, boolean value) throws JSONException {
    nameValuePairs.put(checkName(name), value);
    return this;
}
like image 81
Chintan Shah Avatar answered Oct 19 '22 05:10

Chintan Shah