Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain sort order if object is an instance of SortedMap #91

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
* A JSONObject is an unordered collection of name/value pairs. Its external
Expand Down Expand Up @@ -240,7 +242,11 @@ public JSONObject(JSONTokener x) throws JSONException {
* @throws JSONException
*/
public JSONObject(Map map) {
this.map = new HashMap();
if (map instanceof SortedMap) {
this.map = new TreeMap(map);
} else {
this.map = new HashMap();
}
if (map != null) {
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Expand All @@ -252,7 +258,7 @@ public JSONObject(Map map) {
}
}
}

/**
* Construct a JSONObject from an Object using bean getters. It reflects on
* all of the public methods of the object. For each of the methods with no
Expand Down