From 291628b14064cca53c86a1ef3b1d5cee8b45a824 Mon Sep 17 00:00:00 2001 From: REAndroid Date: Wed, 18 Jan 2023 09:47:17 -0500 Subject: [PATCH] change JSONObject map to LinkedHashMap --- .../com/reandroid/lib/json/JSONObject.java | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/reandroid/lib/json/JSONObject.java b/src/main/java/com/reandroid/lib/json/JSONObject.java index a1bc0fb..79f0c69 100644 --- a/src/main/java/com/reandroid/lib/json/JSONObject.java +++ b/src/main/java/com/reandroid/lib/json/JSONObject.java @@ -14,15 +14,8 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.Collection; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Locale; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.ResourceBundle; -import java.util.Set; import java.util.regex.Pattern; public class JSONObject extends JSONItem{ @@ -53,18 +46,12 @@ public class JSONObject extends JSONItem{ static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?"); - private final Map map; + private final LinkedHashMap map; public static final Object NULL = new Null(); public JSONObject() { - // HashMap is used on purpose to ensure that elements are unordered by - // the specification. - // JSON tends to be a portable transfer format to allows the container - // implementations to rearrange their items for a faster element - // retrieval based on associative access. - // Therefore, an implementation mustn't rely on the order of the item. - this.map = new HashMap(); + this.map = new LinkedHashMap<>(); } public JSONObject(JSONObject jo, String ... names) { @@ -139,9 +126,9 @@ public class JSONObject extends JSONItem{ public JSONObject(Map m) { if (m == null) { - this.map = new HashMap(); + this.map = new LinkedHashMap(); } else { - this.map = new HashMap(m.size()); + this.map = new LinkedHashMap(m.size()); for (final Entry e : m.entrySet()) { if(e.getKey() == null) { throw new NullPointerException("Null key."); @@ -210,7 +197,7 @@ public class JSONObject extends JSONItem{ protected JSONObject(int initialCapacity){ - this.map = new HashMap(initialCapacity); + this.map = new LinkedHashMap(initialCapacity); } @@ -1374,8 +1361,8 @@ public class JSONObject extends JSONItem{ } } - public Map toMap() { - Map results = new HashMap(); + public LinkedHashMap toMap() { + LinkedHashMap results = new LinkedHashMap(); for (Entry entry : this.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) {