From c182e60c9fd5e7c80a11bcf085e82b3173a7bd94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szab=C3=B3=20Andr=C3=A1s?= Date: Wed, 6 Jan 2016 17:29:39 +0100 Subject: [PATCH 1/3] changed HashMaps to LinkedHashMaps so the order of attributes are preserved --- JSONObject.java | 6 +++--- XMLTokener.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 2f613f855..18819981f 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -33,7 +33,7 @@ of this software and associated documentation files (the "Software"), to deal import java.math.*; import java.util.Collection; import java.util.Enumeration; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Iterator; import java.util.Locale; import java.util.Map; @@ -153,7 +153,7 @@ public String toString() { * Construct an empty JSONObject. */ public JSONObject() { - this.map = new HashMap(); + this.map = new LinkedHashMap(); } /** @@ -239,7 +239,7 @@ public JSONObject(JSONTokener x) throws JSONException { * the JSONObject. */ public JSONObject(Map map) { - this.map = new HashMap(); + this.map = new LinkedHashMap(); if (map != null) { for (final Entry e : map.entrySet()) { final Object value = e.getValue(); diff --git a/XMLTokener.java b/XMLTokener.java index e45e747dc..3271c2e29 100644 --- a/XMLTokener.java +++ b/XMLTokener.java @@ -36,10 +36,10 @@ public class XMLTokener extends JSONTokener { /** The table of entity values. It initially contains Character values for * amp, apos, gt, lt, quot. */ - public static final java.util.HashMap entity; + public static final java.util.LinkedHashMap entity; static { - entity = new java.util.HashMap(8); + entity = new java.util.LinkedHashMap(8); entity.put("amp", XML.AMP); entity.put("apos", XML.APOS); entity.put("gt", XML.GT); From e0894c347a8404cd5dba3bb5af6f90798dcd8975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szab=C3=B3=20Andr=C3=A1s?= Date: Tue, 2 Feb 2016 12:07:39 +0100 Subject: [PATCH 2/3] I hate that getString does not return String when toString is available, fixed --- JSONObject.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 18819981f..65b8bb9b9 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -30,11 +30,12 @@ of this software and associated documentation files (the "Software"), to deal import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.math.*; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Collection; import java.util.Enumeration; -import java.util.LinkedHashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; @@ -131,7 +132,8 @@ public boolean equals(Object object) { * * @return The string "null". */ - public String toString() { + @Override + public String toString() { return "null"; } } @@ -717,6 +719,10 @@ public String getString(String key) throws JSONException { if (object instanceof String) { return (String) object; } + // András changed this + if (object instanceof Number) { + return object.toString(); + } throw new JSONException("JSONObject[" + quote(key) + "] not a string."); } @@ -1579,7 +1585,8 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException { * brace) and ending with } (right * brace). */ - public String toString() { + @Override + public String toString() { try { return this.toString(0); } catch (Exception e) { From 6ce18370c058db78e3602bce53e51a6035a6d5c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szab=C3=B3=20Andr=C3=A1s?= Date: Tue, 2 Feb 2016 12:10:30 +0100 Subject: [PATCH 3/3] why limit this to Numbers only? return with .toString --- JSONObject.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 65b8bb9b9..10faa494f 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -720,9 +720,7 @@ public String getString(String key) throws JSONException { return (String) object; } // András changed this - if (object instanceof Number) { - return object.toString(); - } + if (object!=null) return object.toString(); throw new JSONException("JSONObject[" + quote(key) + "] not a string."); }