@@ -36,6 +36,7 @@ of this software and associated documentation files (the "Software"), to deal
3636import java .util .Enumeration ;
3737import java .util .HashMap ;
3838import java .util .Iterator ;
39+ import java .util .LinkedHashMap ;
3940import java .util .Locale ;
4041import java .util .Map ;
4142import 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