What is the difference between putAll(Map map) and accumulateAll(Map map) method of json-lib?

StackOverflow https://stackoverflow.com/questions/19246576

  •  30-06-2022
  •  | 
  •  

Domanda

JsonObjectclass of Library json-lib-2.2.3(net.sf.json) has two methods :

public void putAll( Map map )

AND

public void accumulateAll( Map map )

I have to put all map details in JSONObject.

Which method should i use and why(i.e What is the difference between the two) ?

È stato utile?

Soluzione

From the javadoc on accumulate:

Accumulate values under a key. It is similar to the element 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 replace method replaces the previous value.

From this, it seems that accumulateAll would call accumulate for all values in the map, i.e. values already in the object are not replaced, whereas putAll might replace existing values.

Example:

You have an object like this: {"chars":"A"}.

  • putAll with a map containing "chars" -> "B" would result in {"chars":"B"}
  • accumulateAll with a map containing "chars" -> "B" would result in {"chars":["A","B"]}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top