From c17f7d2870bbad42d3c66325f33a1a45cdc9be70 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Tue, 2 Feb 2016 11:30:01 +0100 Subject: [PATCH] added an option to use a LinkedHashMap to keep the original order of properties --- JSONObject.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 27be3ec18..e9d6bc8b7 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -36,6 +36,7 @@ of this software and associated documentation files (the "Software"), to deal import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; @@ -157,6 +158,16 @@ public JSONObject() { this.map = new HashMap(); } + /** + * Construct an empty JSONObject. + * + * @param ordered + * if ordered == true, then the JSONObject keeps the original order of properties + */ + public JSONObject(boolean ordered) { + this.map = ordered ? new LinkedHashMap() : new HashMap(); + } + /** * Construct a JSONObject from a subset of another JSONObject. An array of * strings is used to identify the keys that should be copied. Missing keys @@ -240,7 +251,7 @@ public JSONObject(JSONTokener x) throws JSONException { * the JSONObject. */ public JSONObject(Map map) { - this.map = new HashMap(); + this.map = map instanceof LinkedHashMap ? new LinkedHashMap() : new HashMap(); if (map != null) { for (final Entry e : map.entrySet()) { final Object value = e.getValue();