Skip to content

Commit 4f5bf16

Browse files
committed
* Adds protected entrySet accessor to JSONObject
* Updates loops that request key/value pairs to use the new entrySet accessor
1 parent fbd2be7 commit 4f5bf16

File tree

7 files changed

+95
-88
lines changed

7 files changed

+95
-88
lines changed

CookieList.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.json;
22

3+
import java.util.Map.Entry;
4+
35
/*
46
Copyright (c) 2002 JSON.org
57
@@ -24,8 +26,6 @@ of this software and associated documentation files (the "Software"), to deal
2426
SOFTWARE.
2527
*/
2628

27-
import java.util.Iterator;
28-
2929
/**
3030
* Convert a web browser cookie list string to a JSONObject and back.
3131
* @author JSON.org
@@ -69,18 +69,17 @@ public static JSONObject toJSONObject(String string) throws JSONException {
6969
*/
7070
public static String toString(JSONObject jo) throws JSONException {
7171
boolean b = false;
72-
Iterator<String> keys = jo.keys();
73-
String string;
7472
StringBuilder sb = new StringBuilder();
75-
while (keys.hasNext()) {
76-
string = keys.next();
77-
if (!jo.isNull(string)) {
73+
for (final Entry<String,?> entry : jo.entrySet()) {
74+
final String key = entry.getKey();
75+
final Object value = entry.getValue();
76+
if (!JSONObject.NULL.equals(value)) {
7877
if (b) {
7978
sb.append(';');
8079
}
81-
sb.append(Cookie.escape(string));
80+
sb.append(Cookie.escape(key));
8281
sb.append("=");
83-
sb.append(Cookie.escape(jo.getString(string)));
82+
sb.append(Cookie.escape(value.toString()));
8483
b = true;
8584
}
8685
}

HTTP.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ of this software and associated documentation files (the "Software"), to deal
2424
SOFTWARE.
2525
*/
2626

27-
import java.util.Iterator;
2827
import java.util.Locale;
28+
import java.util.Map.Entry;
2929

3030
/**
3131
* Convert an HTTP header to a JSONObject and back.
@@ -126,8 +126,6 @@ public static JSONObject toJSONObject(String string) throws JSONException {
126126
* information.
127127
*/
128128
public static String toString(JSONObject jo) throws JSONException {
129-
Iterator<String> keys = jo.keys();
130-
String string;
131129
StringBuilder sb = new StringBuilder();
132130
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
133131
sb.append(jo.getString("HTTP-Version"));
@@ -147,14 +145,14 @@ public static String toString(JSONObject jo) throws JSONException {
147145
throw new JSONException("Not enough material for an HTTP header.");
148146
}
149147
sb.append(CRLF);
150-
while (keys.hasNext()) {
151-
string = keys.next();
152-
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) &&
153-
!"Reason-Phrase".equals(string) && !"Method".equals(string) &&
154-
!"Request-URI".equals(string) && !jo.isNull(string)) {
155-
sb.append(string);
148+
for (final Entry<String,?> entry : jo.entrySet()) {
149+
final String key = entry.getKey();
150+
if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) &&
151+
!"Reason-Phrase".equals(key) && !"Method".equals(key) &&
152+
!"Request-URI".equals(key) && !JSONObject.NULL.equals(entry.getValue())) {
153+
sb.append(key);
156154
sb.append(": ");
157-
sb.append(jo.getString(string));
155+
sb.append(jo.optString(key));
158156
sb.append(CRLF);
159157
}
160158
}

JSONArray.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1263,8 +1263,14 @@ public boolean similar(Object other) {
12631263
return false;
12641264
}
12651265
for (int i = 0; i < len; i += 1) {
1266-
Object valueThis = this.get(i);
1267-
Object valueOther = ((JSONArray)other).get(i);
1266+
Object valueThis = this.myArrayList.get(i);
1267+
Object valueOther = ((JSONArray)other).myArrayList.get(i);
1268+
if(valueThis == valueOther) {
1269+
return true;
1270+
}
1271+
if(valueThis == null) {
1272+
return false;
1273+
}
12681274
if (valueThis instanceof JSONObject) {
12691275
if (!((JSONObject)valueThis).similar(valueOther)) {
12701276
return false;

JSONML.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.json;
22

3+
import java.util.Map.Entry;
4+
35
/*
46
Copyright (c) 2008 JSON.org
57
@@ -24,9 +26,6 @@ of this software and associated documentation files (the "Software"), to deal
2426
SOFTWARE.
2527
*/
2628

27-
import java.util.Iterator;
28-
29-
3029
/**
3130
* This provides static methods to convert an XML text into a JSONArray or
3231
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
@@ -397,13 +396,10 @@ public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings) throws
397396
public static String toString(JSONArray ja) throws JSONException {
398397
int i;
399398
JSONObject jo;
400-
String key;
401-
Iterator<String> keys;
402399
int length;
403400
Object object;
404401
StringBuilder sb = new StringBuilder();
405402
String tagName;
406-
String value;
407403

408404
// Emit <tagName
409405

@@ -420,17 +416,16 @@ public static String toString(JSONArray ja) throws JSONException {
420416

421417
// Emit the attributes
422418

423-
keys = jo.keys();
424-
while (keys.hasNext()) {
425-
key = keys.next();
419+
for (final Entry<String, ?> entry : jo.entrySet()) {
420+
final String key = entry.getKey();
426421
XML.noSpace(key);
427-
value = jo.optString(key);
422+
final Object value = entry.getValue();
428423
if (value != null) {
429424
sb.append(' ');
430425
sb.append(XML.escape(key));
431426
sb.append('=');
432427
sb.append('"');
433-
sb.append(XML.escape(value));
428+
sb.append(XML.escape(value.toString()));
434429
sb.append('"');
435430
}
436431
}
@@ -482,12 +477,10 @@ public static String toString(JSONObject jo) throws JSONException {
482477
StringBuilder sb = new StringBuilder();
483478
int i;
484479
JSONArray ja;
485-
String key;
486-
Iterator<String> keys;
487480
int length;
488481
Object object;
489482
String tagName;
490-
String value;
483+
Object value;
491484

492485
//Emit <tagName
493486

@@ -502,18 +495,17 @@ public static String toString(JSONObject jo) throws JSONException {
502495

503496
//Emit the attributes
504497

505-
keys = jo.keys();
506-
while (keys.hasNext()) {
507-
key = keys.next();
498+
for (final Entry<String, ?> entry : jo.entrySet()) {
499+
final String key = entry.getKey();
508500
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
509501
XML.noSpace(key);
510-
value = jo.optString(key);
502+
value = entry.getValue();
511503
if (value != null) {
512504
sb.append(' ');
513505
sb.append(XML.escape(key));
514506
sb.append('=');
515507
sb.append('"');
516-
sb.append(XML.escape(value));
508+
sb.append(XML.escape(value.toString()));
517509
sb.append('"');
518510
}
519511
}

JSONObject.java

+50-35
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ public JSONObject(JSONTokener x) throws JSONException {
245245
/**
246246
* Construct a JSONObject from a Map.
247247
*
248-
* @param map
248+
* @param m
249249
* A map object that can be used to initialize the contents of
250250
* the JSONObject.
251251
*/
252-
public JSONObject(Map<?, ?> map) {
252+
public JSONObject(Map<?, ?> m) {
253253
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()) {
256256
final Object value = e.getValue();
257257
if (value != null) {
258258
this.map.put(String.valueOf(e.getKey()), wrap(value));
@@ -729,14 +729,7 @@ public static String[] getNames(JSONObject jo) {
729729
if (length == 0) {
730730
return null;
731731
}
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]);
740733
}
741734

742735
/**
@@ -837,23 +830,45 @@ public boolean isNull(String key) {
837830
}
838831

839832
/**
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.
841835
*
836+
* @see Set#iterator()
837+
*
842838
* @return An iterator of the keys.
843839
*/
844840
public Iterator<String> keys() {
845841
return this.keySet().iterator();
846842
}
847843

848844
/**
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()
850849
*
851850
* @return A keySet.
852851
*/
853852
public Set<String> keySet() {
854853
return this.map.keySet();
855854
}
856855

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 A keySet.
867+
*/
868+
protected Set<Entry<String, Object>> entrySet() {
869+
return this.map.entrySet();
870+
}
871+
857872
/**
858873
* Get the number of keys stored in the JSONObject.
859874
*
@@ -871,12 +886,10 @@ public int length() {
871886
* is empty.
872887
*/
873888
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());
880893
}
881894

882895
/**
@@ -1762,15 +1775,19 @@ public boolean similar(Object other) {
17621775
if (!(other instanceof JSONObject)) {
17631776
return false;
17641777
}
1765-
Set<String> set = this.keySet();
1766-
if (!set.equals(((JSONObject)other).keySet())) {
1778+
if (!this.keySet().equals(((JSONObject)other).keySet())) {
17671779
return false;
17681780
}
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();
17731784
Object valueOther = ((JSONObject)other).get(name);
1785+
if(valueThis == valueOther) {
1786+
return true;
1787+
}
1788+
if(valueThis == null) {
1789+
return false;
1790+
}
17741791
if (valueThis instanceof JSONObject) {
17751792
if (!((JSONObject)valueThis).similar(valueOther)) {
17761793
return false;
@@ -2220,34 +2237,32 @@ public Writer write(Writer writer, int indentFactor, int indent)
22202237
try {
22212238
boolean commanate = false;
22222239
final int length = this.length();
2223-
Iterator<String> keys = this.keys();
22242240
writer.write('{');
22252241

22262242
if (length == 1) {
2227-
Object key = keys.next();
2228-
writer.write(quote(key.toString()));
2243+
final Entry<String,?> entry = this.entrySet().iterator().next();
2244+
writer.write(quote(entry.getKey()));
22292245
writer.write(':');
22302246
if (indentFactor > 0) {
22312247
writer.write(' ');
22322248
}
2233-
writeValue(writer, this.map.get(key), indentFactor, indent);
2249+
writeValue(writer, entry.getValue(), indentFactor, indent);
22342250
} else if (length != 0) {
22352251
final int newindent = indent + indentFactor;
2236-
while (keys.hasNext()) {
2237-
Object key = keys.next();
2252+
for (final Entry<String,?> entry : this.entrySet()) {
22382253
if (commanate) {
22392254
writer.write(',');
22402255
}
22412256
if (indentFactor > 0) {
22422257
writer.write('\n');
22432258
}
22442259
indent(writer, newindent);
2245-
writer.write(quote(key.toString()));
2260+
writer.write(quote(entry.getKey()));
22462261
writer.write(':');
22472262
if (indentFactor > 0) {
22482263
writer.write(' ');
22492264
}
2250-
writeValue(writer, this.map.get(key), indentFactor, newindent);
2265+
writeValue(writer, entry.getValue(), indentFactor, newindent);
22512266
commanate = true;
22522267
}
22532268
if (indentFactor > 0) {
@@ -2273,7 +2288,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
22732288
*/
22742289
public Map<String, Object> toMap() {
22752290
Map<String, Object> results = new HashMap<String, Object>();
2276-
for (Entry<String, Object> entry : this.map.entrySet()) {
2291+
for (Entry<String, Object> entry : this.entrySet()) {
22772292
Object value;
22782293
if (entry.getValue() == null || NULL.equals(entry.getValue())) {
22792294
value = null;

Property.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
2525
*/
2626

2727
import java.util.Enumeration;
28-
import java.util.Iterator;
28+
import java.util.Map.Entry;
2929
import java.util.Properties;
3030

3131
/**
@@ -61,10 +61,11 @@ public static JSONObject toJSONObject(java.util.Properties properties) throws JS
6161
public static Properties toProperties(JSONObject jo) throws JSONException {
6262
Properties properties = new Properties();
6363
if (jo != null) {
64-
Iterator<String> keys = jo.keys();
65-
while (keys.hasNext()) {
66-
String name = keys.next();
67-
properties.put(name, jo.getString(name));
64+
for (final Entry<String, ?> entry : jo.entrySet()) {
65+
Object value = entry.getValue();
66+
if (!JSONObject.NULL.equals(value)) {
67+
properties.put(entry.getKey(), value.toString());
68+
}
6869
}
6970
}
7071
return properties;

0 commit comments

Comments
 (0)