mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
keyPool
This commit is contained in:
parent
1b5a59f428
commit
50c3afb216
1 changed files with 23 additions and 1 deletions
|
@ -91,9 +91,21 @@ import java.util.Set;
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2012-10-27
|
* @version 2012-12-01
|
||||||
*/
|
*/
|
||||||
public class JSONObject {
|
public class JSONObject {
|
||||||
|
/**
|
||||||
|
* The maximum number of keys in the key pool.
|
||||||
|
*/
|
||||||
|
private static final int keyPoolSize = 100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key pooling is like string interning, but without permanently tying up
|
||||||
|
* memory. To help conserve memory, storage of duplicated key strings in
|
||||||
|
* JSONObjects will be avoided by using a key pool to manage unique key
|
||||||
|
* string objects. This is used by JSONObject.put(string, object).
|
||||||
|
*/
|
||||||
|
private static HashMap keyPool = new HashMap(keyPoolSize);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
|
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
|
||||||
|
@ -1110,11 +1122,21 @@ public class JSONObject {
|
||||||
* or if the key is null.
|
* or if the key is null.
|
||||||
*/
|
*/
|
||||||
public JSONObject put(String key, Object value) throws JSONException {
|
public JSONObject put(String key, Object value) throws JSONException {
|
||||||
|
String pooled;
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
throw new JSONException("Null key.");
|
throw new JSONException("Null key.");
|
||||||
}
|
}
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
testValidity(value);
|
testValidity(value);
|
||||||
|
pooled = (String)keyPool.get(key);
|
||||||
|
if (pooled == null) {
|
||||||
|
if (keyPool.size() >= keyPoolSize) {
|
||||||
|
keyPool = new HashMap(keyPoolSize);
|
||||||
|
}
|
||||||
|
keyPool.put(key, key);
|
||||||
|
} else {
|
||||||
|
key = pooled;
|
||||||
|
}
|
||||||
this.map.put(key, value);
|
this.map.put(key, value);
|
||||||
} else {
|
} else {
|
||||||
this.remove(key);
|
this.remove(key);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue