Skip to content

Commit 835ec2c

Browse files
Retain sort order if object is an instance of SortedMap
1 parent 4d86b05 commit 835ec2c

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎JSONObject.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ of this software and associated documentation files (the "Software"), to deal
3838
import java.util.Map;
3939
import java.util.ResourceBundle;
4040
import java.util.Set;
41+
import java.util.SortedMap;
42+
import java.util.TreeMap;
4143

4244
/**
4345
* A JSONObject is an unordered collection of name/value pairs. Its external
@@ -240,7 +242,11 @@ public JSONObject(JSONTokener x) throws JSONException {
240242
* @throws JSONException
241243
*/
242244
public JSONObject(Map map) {
243-
this.map = new HashMap();
245+
if (map instanceof SortedMap) {
246+
this.map = new TreeMap(map);
247+
} else {
248+
this.map = new HashMap();
249+
}
244250
if (map != null) {
245251
Iterator i = map.entrySet().iterator();
246252
while (i.hasNext()) {
@@ -252,7 +258,7 @@ public JSONObject(Map map) {
252258
}
253259
}
254260
}
255-
261+
256262
/**
257263
* Construct a JSONObject from an Object using bean getters. It reflects on
258264
* all of the public methods of the object. For each of the methods with no

0 commit comments

Comments
 (0)