Skip to content

Commit c17f7d2

Browse files
committed
added an option to use a LinkedHashMap to keep the original order of properties
1 parent ba2585f commit c17f7d2

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

JSONObject.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ of this software and associated documentation files (the "Software"), to deal
3636
import java.util.Enumeration;
3737
import java.util.HashMap;
3838
import java.util.Iterator;
39+
import java.util.LinkedHashMap;
3940
import java.util.Locale;
4041
import java.util.Map;
4142
import java.util.Map.Entry;
@@ -157,6 +158,16 @@ public JSONObject() {
157158
this.map = new HashMap<String, Object>();
158159
}
159160

161+
/**
162+
* Construct an empty JSONObject.
163+
*
164+
* @param ordered
165+
* if ordered == true, then the JSONObject keeps the original order of properties
166+
*/
167+
public JSONObject(boolean ordered) {
168+
this.map = ordered ? new LinkedHashMap<String, Object>() : new HashMap<String, Object>();
169+
}
170+
160171
/**
161172
* Construct a JSONObject from a subset of another JSONObject. An array of
162173
* strings is used to identify the keys that should be copied. Missing keys
@@ -240,7 +251,7 @@ public JSONObject(JSONTokener x) throws JSONException {
240251
* the JSONObject.
241252
*/
242253
public JSONObject(Map<?, ?> map) {
243-
this.map = new HashMap<String, Object>();
254+
this.map = map instanceof LinkedHashMap ? new LinkedHashMap<String, Object>() : new HashMap<String, Object>();
244255
if (map != null) {
245256
for (final Entry<?, ?> e : map.entrySet()) {
246257
final Object value = e.getValue();

0 commit comments

Comments
 (0)