@@ -91,9 +91,21 @@ of this software and associated documentation files (the "Software"), to deal
91
91
* </ul>
92
92
*
93
93
* @author JSON.org
94
- * @version 2012-10-27
94
+ * @version 2012-12-01
95
95
*/
96
96
public class JSONObject {
97
+ /**
98
+ * The maximum number of keys in the key pool.
99
+ */
100
+ private static final int keyPoolSize =100 ;
101
+
102
+ /**
103
+ * Key pooling is like string interning, but without permanently tying up
104
+ * memory. To help conserve memory, storage of duplicated key strings in
105
+ * JSONObjects will be avoided by using a key pool to manage unique key
106
+ * string objects. This is used by JSONObject.put(string, object).
107
+ */
108
+ private static HashMap keyPool =new HashMap (keyPoolSize );
97
109
98
110
/**
99
111
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
@@ -1110,11 +1122,21 @@ public JSONObject put(String key, Map value) throws JSONException {
1110
1122
* or if the key is null.
1111
1123
*/
1112
1124
public JSONObject put (String key ,Object value )throws JSONException {
1125
+ String pooled ;
1113
1126
if (key ==null ) {
1114
1127
throw new JSONException ("Null key." );
1115
1128
}
1116
1129
if (value !=null ) {
1117
1130
testValidity (value );
1131
+ pooled = (String )keyPool .get (key );
1132
+ if (pooled ==null ) {
1133
+ if (keyPool .size () >=keyPoolSize ) {
1134
+ keyPool =new HashMap (keyPoolSize );
1135
+ }
1136
+ keyPool .put (key ,key );
1137
+ }else {
1138
+ key =pooled ;
1139
+ }
1118
1140
this .map .put (key ,value );
1119
1141
}else {
1120
1142
this .remove (key );