Skip to content

Commit 9a0471d

Browse files
JSONObject's keys are unordered
1 parent 12b75c4 commit 9a0471d

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

JSONObject.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ of this software and associated documentation files (the "Software"), to deal
3636
import java.util.Locale;
3737
import java.util.Map;
3838
import java.util.ResourceBundle;
39-
import java.util.TreeSet;
4039

4140
/**
4241
* A JSONObject is an unordered collection of name/value pairs. Its
@@ -87,7 +86,7 @@ of this software and associated documentation files (the "Software"), to deal
8786
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
8887
* </ul>
8988
* @author JSON.org
90-
* @version 2010-12-28
89+
* @version 2011-01-31
9190
*/
9291
public class JSONObject {
9392

@@ -320,12 +319,12 @@ public JSONObject(String source) throws JSONException {
320319
*/
321320
public JSONObject(String baseName, Locale locale) throws JSONException {
322321
this();
323-
ResourceBundle r = ResourceBundle.getBundle(baseName, locale,
322+
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
324323
Thread.currentThread().getContextClassLoader());
325324

326325
// Iterate through the keys in the bundle.
327326

328-
Enumeration keys = r.getKeys();
327+
Enumeration keys = bundle.getKeys();
329328
while (keys.hasMoreElements()) {
330329
Object key = keys.nextElement();
331330
if (key instanceof String) {
@@ -346,7 +345,7 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
346345
}
347346
target = nextTarget;
348347
}
349-
target.put(path[last], r.getString((String)key));
348+
target.put(path[last], bundle.getString((String)key));
350349
}
351350
}
352351
}
@@ -358,6 +357,10 @@ public JSONObject(String baseName, Locale locale) throws JSONException {
358357
* JSONArray is stored under the key to hold all of the accumulated values.
359358
* If there is already a JSONArray, then the new value is appended to it.
360359
* In contrast, the put method replaces the previous value.
360+
*
361+
* If only one value is accumulated that is not a JSONArray, then the
362+
* result will be the same as using put. But if multiple values are
363+
* accumulated, then the result will be like append.
361364
* @param key A key string.
362365
* @param value An object to be accumulated under the key.
363366
* @return this.
@@ -710,7 +713,7 @@ public int length() {
710713
*/
711714
public JSONArray names() {
712715
JSONArray ja = new JSONArray();
713-
Iterator keys = keys();
716+
Iterator keys = this.keys();
714717
while (keys.hasNext()) {
715718
ja.put(keys.next());
716719
}
@@ -1210,16 +1213,6 @@ public Object remove(String key) {
12101213
return this.map.remove(key);
12111214
}
12121215

1213-
/**
1214-
* Get an enumeration of the keys of the JSONObject.
1215-
* The keys will be sorted alphabetically.
1216-
*
1217-
* @return An iterator of the keys.
1218-
*/
1219-
public Iterator sortedKeys() {
1220-
return new TreeSet(this.map.keySet()).iterator();
1221-
}
1222-
12231216
/**
12241217
* Try to convert a string into a number, boolean, or null. If the string
12251218
* can't be converted, return the string.
@@ -1332,7 +1325,7 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
13321325
*/
13331326
public String toString() {
13341327
try {
1335-
Iterator keys = keys();
1328+
Iterator keys = this.keys();
13361329
StringBuffer sb = new StringBuffer("{");
13371330

13381331
while (keys.hasNext()) {
@@ -1388,7 +1381,7 @@ String toString(int indentFactor, int indent) throws JSONException {
13881381
if (length == 0) {
13891382
return "{}";
13901383
}
1391-
Iterator keys = sortedKeys();
1384+
Iterator keys = this.keys();
13921385
int newindent = indent + indentFactor;
13931386
Object object;
13941387
StringBuffer sb = new StringBuffer("{");
@@ -1598,7 +1591,7 @@ public static Object wrap(Object object) {
15981591
public Writer write(Writer writer) throws JSONException {
15991592
try {
16001593
boolean commanate = false;
1601-
Iterator keys = keys();
1594+
Iterator keys = this.keys();
16021595
writer.write('{');
16031596

16041597
while (keys.hasNext()) {

0 commit comments

Comments
 (0)