@@ -245,14 +245,14 @@ public JSONObject(JSONTokener x) throws JSONException {
245
245
/**
246
246
* Construct a JSONObject from a Map.
247
247
*
248
- * @param map
248
+ * @param m
249
249
* A map object that can be used to initialize the contents of
250
250
* the JSONObject.
251
251
*/
252
- public JSONObject (Map <?, ?> map ) {
252
+ public JSONObject (Map <?, ?> m ) {
253
253
this .map = new HashMap <String , Object >();
254
- if (map != null ) {
255
- for (final Entry <?, ?> e : map .entrySet ()) {
254
+ if (m != null ) {
255
+ for (final Entry <?, ?> e : m .entrySet ()) {
256
256
final Object value = e .getValue ();
257
257
if (value != null ) {
258
258
this .map .put (String .valueOf (e .getKey ()), wrap (value ));
@@ -729,14 +729,7 @@ public static String[] getNames(JSONObject jo) {
729
729
if (length == 0 ) {
730
730
return null ;
731
731
}
732
- Iterator <String > iterator = jo .keys ();
733
- String [] names = new String [length ];
734
- int i = 0 ;
735
- while (iterator .hasNext ()) {
736
- names [i ] = iterator .next ();
737
- i ++;
738
- }
739
- return names ;
732
+ return jo .keySet ().toArray (new String [length ]);
740
733
}
741
734
742
735
/**
@@ -837,23 +830,45 @@ public boolean isNull(String key) {
837
830
}
838
831
839
832
/**
840
- * Get an enumeration of the keys of the JSONObject.
833
+ * Get an enumeration of the keys of the JSONObject. Modifying this key Set will also
834
+ * modify the JSONObject. Use with caution.
841
835
*
836
+ * @see Set#iterator()
837
+ *
842
838
* @return An iterator of the keys.
843
839
*/
844
840
public Iterator <String > keys () {
845
841
return this .keySet ().iterator ();
846
842
}
847
843
848
844
/**
849
- * Get a set of keys of the JSONObject.
845
+ * Get a set of keys of the JSONObject. Modifying this key Set will also modify the
846
+ * JSONObject. Use with caution.
847
+ *
848
+ * @see Map#keySet()
850
849
*
851
850
* @return A keySet.
852
851
*/
853
852
public Set <String > keySet () {
854
853
return this .map .keySet ();
855
854
}
856
855
856
+ /**
857
+ * Get a set of entries of the JSONObject. These are raw values and may not
858
+ * match what is returned by the JSONObject get* and opt* functions. Modifying
859
+ * the returned EntrySet or the Entry objects contained therein will modify the
860
+ * backing JSONObject. This does not return a clone or a read-only view.
861
+ *
862
+ * Use with caution.
863
+ *
864
+ * @see Map#entrySet()
865
+ *
866
+ * @return An Entry Set
867
+ */
868
+ protected Set <Entry <String , Object >> entrySet () {
869
+ return this .map .entrySet ();
870
+ }
871
+
857
872
/**
858
873
* Get the number of keys stored in the JSONObject.
859
874
*
@@ -871,12 +886,10 @@ public int length() {
871
886
* is empty.
872
887
*/
873
888
public JSONArray names () {
874
- JSONArray ja = new JSONArray ();
875
- Iterator <String > keys = this .keys ();
876
- while (keys .hasNext ()) {
877
- ja .put (keys .next ());
878
- }
879
- return ja .length () == 0 ? null : ja ;
889
+ if (this .map .isEmpty ()) {
890
+ return null ;
891
+ }
892
+ return new JSONArray (this .map .keySet ());
880
893
}
881
894
882
895
/**
@@ -1762,15 +1775,19 @@ public boolean similar(Object other) {
1762
1775
if (!(other instanceof JSONObject )) {
1763
1776
return false ;
1764
1777
}
1765
- Set <String > set = this .keySet ();
1766
- if (!set .equals (((JSONObject )other ).keySet ())) {
1778
+ if (!this .keySet ().equals (((JSONObject )other ).keySet ())) {
1767
1779
return false ;
1768
1780
}
1769
- Iterator <String > iterator = set .iterator ();
1770
- while (iterator .hasNext ()) {
1771
- String name = iterator .next ();
1772
- Object valueThis = this .get (name );
1781
+ for (final Entry <String ,?> entry : this .entrySet ()) {
1782
+ String name = entry .getKey ();
1783
+ Object valueThis = entry .getValue ();
1773
1784
Object valueOther = ((JSONObject )other ).get (name );
1785
+ if (valueThis == valueOther ) {
1786
+ return true ;
1787
+ }
1788
+ if (valueThis == null ) {
1789
+ return false ;
1790
+ }
1774
1791
if (valueThis instanceof JSONObject ) {
1775
1792
if (!((JSONObject )valueThis ).similar (valueOther )) {
1776
1793
return false ;
@@ -2201,8 +2218,7 @@ static final void indent(Writer writer, int indent) throws IOException {
2201
2218
}
2202
2219
2203
2220
/**
2204
- * Write the contents of the JSONObject as JSON text to a writer. For
2205
- * compactness, no whitespace is added.
2221
+ * Write the contents of the JSONObject as JSON text to a writer.
2206
2222
* <p>
2207
2223
* Warning: This method assumes that the data structure is acyclical.
2208
2224
*
@@ -2220,34 +2236,32 @@ public Writer write(Writer writer, int indentFactor, int indent)
2220
2236
try {
2221
2237
boolean commanate = false ;
2222
2238
final int length = this .length ();
2223
- Iterator <String > keys = this .keys ();
2224
2239
writer .write ('{' );
2225
2240
2226
2241
if (length == 1 ) {
2227
- Object key = keys .next ();
2228
- writer .write (quote (key . toString ()));
2242
+ final Entry < String ,?> entry = this . entrySet (). iterator () .next ();
2243
+ writer .write (quote (entry . getKey ()));
2229
2244
writer .write (':' );
2230
2245
if (indentFactor > 0 ) {
2231
2246
writer .write (' ' );
2232
2247
}
2233
- writeValue (writer , this . map . get ( key ), indentFactor , indent );
2248
+ writeValue (writer , entry . getValue ( ), indentFactor , indent );
2234
2249
} else if (length != 0 ) {
2235
2250
final int newindent = indent + indentFactor ;
2236
- while (keys .hasNext ()) {
2237
- Object key = keys .next ();
2251
+ for (final Entry <String ,?> entry : this .entrySet ()) {
2238
2252
if (commanate ) {
2239
2253
writer .write (',' );
2240
2254
}
2241
2255
if (indentFactor > 0 ) {
2242
2256
writer .write ('\n' );
2243
2257
}
2244
2258
indent (writer , newindent );
2245
- writer .write (quote (key . toString ()));
2259
+ writer .write (quote (entry . getKey ()));
2246
2260
writer .write (':' );
2247
2261
if (indentFactor > 0 ) {
2248
2262
writer .write (' ' );
2249
2263
}
2250
- writeValue (writer , this . map . get ( key ), indentFactor , newindent );
2264
+ writeValue (writer , entry . getValue ( ), indentFactor , newindent );
2251
2265
commanate = true ;
2252
2266
}
2253
2267
if (indentFactor > 0 ) {
@@ -2273,7 +2287,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
2273
2287
*/
2274
2288
public Map <String , Object > toMap () {
2275
2289
Map <String , Object > results = new HashMap <String , Object >();
2276
- for (Entry <String , Object > entry : this .map . entrySet ()) {
2290
+ for (Entry <String , Object > entry : this .entrySet ()) {
2277
2291
Object value ;
2278
2292
if (entry .getValue () == null || NULL .equals (entry .getValue ())) {
2279
2293
value = null ;
0 commit comments