diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index f11b328d8..9160aef01 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -259,11 +259,10 @@ public Iterator iterator() { * If there is no value for the index. */ public Object get(int index) throws JSONException { - Object object = this.opt(index); - if (object == null) { + if (index < 0 || index >= length()) { throw new JSONException("JSONArray[" + index + "] not found."); } - return object; + return this.opt(index); } /** diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index f718c0618..a8e54209a 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -38,8 +38,8 @@ of this software and associated documentation files (the "Software"), to deal import java.math.BigInteger; import java.util.Collection; 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; @@ -175,13 +175,8 @@ public String toString() { * Construct an empty JSONObject. */ 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(); + // Ordered hashmap for aesthetics. + this.map = new LinkedHashMap<>(); } /** @@ -286,17 +281,17 @@ public JSONObject(JSONTokener x) throws JSONException { */ 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."); } final Object value = e.getValue(); - if (value != null) { + this.map.put(String.valueOf(e.getKey()), wrap(value)); - } + } } } @@ -457,7 +452,7 @@ public JSONObject(String baseName, Locale locale) throws JSONException { * @param initialCapacity initial capacity of the internal map. */ protected JSONObject(int initialCapacity){ - this.map = new HashMap(initialCapacity); + this.map = new LinkedHashMap(initialCapacity); } /** @@ -1819,12 +1814,10 @@ public JSONObject put(String key, Object value) throws JSONException { if (key == null) { throw new NullPointerException("Null key."); } - if (value != null) { - testValidity(value); - this.map.put(key, value); - } else { - this.remove(key); - } + + testValidity(value); + + this.map.put(key, value); return this; } @@ -2216,19 +2209,7 @@ public static Object stringToValue(String string) { * If o is a non-finite number. */ public static void testValidity(Object o) throws JSONException { - if (o != null) { - if (o instanceof Double) { - if (((Double) o).isInfinite() || ((Double) o).isNaN()) { - throw new JSONException( - "JSON does not allow non-finite numbers."); - } - } else if (o instanceof Float) { - if (((Float) o).isInfinite() || ((Float) o).isNaN()) { - throw new JSONException( - "JSON does not allow non-finite numbers."); - } - } - } + } /** @@ -2549,7 +2530,7 @@ public Writer write(Writer writer, int indentFactor, int indent) * @return a java.util.Map containing the entries of this object */ public Map toMap() { - Map results = new HashMap(); + Map results = new LinkedHashMap(); for (Entry entry : this.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) { diff --git a/src/main/java/org/json/JSONPointer.java b/src/main/java/org/json/JSONPointer.java index e8a0b78c9..7e4a4de3e 100644 --- a/src/main/java/org/json/JSONPointer.java +++ b/src/main/java/org/json/JSONPointer.java @@ -210,7 +210,7 @@ public Object queryFrom(Object document) throws JSONPointerException { Object current = document; for (String token : this.refTokens) { if (current instanceof JSONObject) { - current = ((JSONObject) current).opt(unescape(token)); + current = ((JSONObject) current).opt(token); } else if (current instanceof JSONArray) { current = readByIndexToken(current, token); } else { diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index e06851eb7..37384ed02 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -124,7 +124,7 @@ public void backslashEscaping() { @Test public void quotationEscaping() { - assertSame(document.get("k\"l"), query("/k\\\\\\\"l")); + assertSame(document.get("k\"l"), query("/k\\\"l")); } @Test @@ -276,6 +276,20 @@ public void queryFromJSONObjectUsingPointer() { } } + /** + * Coverage for JSONObject query(JSONPointer) + */ + @Test + public void queryFromJSONObjectUsingPointer2() { + String str = "{"+ + "\"string\\\\\\\\Key\":\"hello world!\","+ + "}"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + Object obj = jsonObject.query(new JSONPointer("/string\\\\\\\\Key")); + assertTrue("Expected 'hello world!'", "hello world!".equals(obj)); + } + /** * Coverage for JSONObject optQuery(JSONPointer) */