From 4f5bf16676ef3ca113b8145790fd486446756764 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 22 May 2017 00:50:39 -0400 Subject: [PATCH] * Adds protected entrySet accessor to JSONObject * Updates loops that request key/value pairs to use the new entrySet accessor --- CookieList.java | 17 +++++----- HTTP.java | 18 +++++------ JSONArray.java | 10 ++++-- JSONML.java | 30 +++++++---------- JSONObject.java | 85 +++++++++++++++++++++++++++++-------------------- Property.java | 11 ++++--- XML.java | 12 +++---- 7 files changed, 95 insertions(+), 88 deletions(-) diff --git a/CookieList.java b/CookieList.java index d69e457..8cb4e5e 100644 --- a/CookieList.java +++ b/CookieList.java @@ -1,5 +1,7 @@ package org.json; +import java.util.Map.Entry; + /* Copyright (c) 2002 JSON.org @@ -24,8 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import java.util.Iterator; - /** * Convert a web browser cookie list string to a JSONObject and back. * @author JSON.org @@ -69,18 +69,17 @@ public class CookieList { */ public static String toString(JSONObject jo) throws JSONException { boolean b = false; - Iterator keys = jo.keys(); - String string; StringBuilder sb = new StringBuilder(); - while (keys.hasNext()) { - string = keys.next(); - if (!jo.isNull(string)) { + for (final Entry entry : jo.entrySet()) { + final String key = entry.getKey(); + final Object value = entry.getValue(); + if (!JSONObject.NULL.equals(value)) { if (b) { sb.append(';'); } - sb.append(Cookie.escape(string)); + sb.append(Cookie.escape(key)); sb.append("="); - sb.append(Cookie.escape(jo.getString(string))); + sb.append(Cookie.escape(value.toString())); b = true; } } diff --git a/HTTP.java b/HTTP.java index 9b444ce..22635ff 100644 --- a/HTTP.java +++ b/HTTP.java @@ -24,8 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import java.util.Iterator; import java.util.Locale; +import java.util.Map.Entry; /** * Convert an HTTP header to a JSONObject and back. @@ -126,8 +126,6 @@ public class HTTP { * information. */ public static String toString(JSONObject jo) throws JSONException { - Iterator keys = jo.keys(); - String string; StringBuilder sb = new StringBuilder(); if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { sb.append(jo.getString("HTTP-Version")); @@ -147,14 +145,14 @@ public class HTTP { throw new JSONException("Not enough material for an HTTP header."); } sb.append(CRLF); - while (keys.hasNext()) { - string = keys.next(); - if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && - !"Reason-Phrase".equals(string) && !"Method".equals(string) && - !"Request-URI".equals(string) && !jo.isNull(string)) { - sb.append(string); + for (final Entry entry : jo.entrySet()) { + final String key = entry.getKey(); + if (!"HTTP-Version".equals(key) && !"Status-Code".equals(key) && + !"Reason-Phrase".equals(key) && !"Method".equals(key) && + !"Request-URI".equals(key) && !JSONObject.NULL.equals(entry.getValue())) { + sb.append(key); sb.append(": "); - sb.append(jo.getString(string)); + sb.append(jo.optString(key)); sb.append(CRLF); } } diff --git a/JSONArray.java b/JSONArray.java index 7965f3c..d08586d 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -1263,8 +1263,14 @@ public class JSONArray implements Iterable { return false; } for (int i = 0; i < len; i += 1) { - Object valueThis = this.get(i); - Object valueOther = ((JSONArray)other).get(i); + Object valueThis = this.myArrayList.get(i); + Object valueOther = ((JSONArray)other).myArrayList.get(i); + if(valueThis == valueOther) { + return true; + } + if(valueThis == null) { + return false; + } if (valueThis instanceof JSONObject) { if (!((JSONObject)valueThis).similar(valueOther)) { return false; diff --git a/JSONML.java b/JSONML.java index 9cb767f..c1d50b3 100644 --- a/JSONML.java +++ b/JSONML.java @@ -1,5 +1,7 @@ package org.json; +import java.util.Map.Entry; + /* Copyright (c) 2008 JSON.org @@ -24,9 +26,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import java.util.Iterator; - - /** * This provides static methods to convert an XML text into a JSONArray or * JSONObject, and to covert a JSONArray or JSONObject into an XML text using @@ -397,13 +396,10 @@ public class JSONML { public static String toString(JSONArray ja) throws JSONException { int i; JSONObject jo; - String key; - Iterator keys; int length; Object object; StringBuilder sb = new StringBuilder(); String tagName; - String value; // Emit entry : jo.entrySet()) { + final String key = entry.getKey(); XML.noSpace(key); - value = jo.optString(key); + final Object value = entry.getValue(); if (value != null) { sb.append(' '); sb.append(XML.escape(key)); sb.append('='); sb.append('"'); - sb.append(XML.escape(value)); + sb.append(XML.escape(value.toString())); sb.append('"'); } } @@ -482,12 +477,10 @@ public class JSONML { StringBuilder sb = new StringBuilder(); int i; JSONArray ja; - String key; - Iterator keys; int length; Object object; String tagName; - String value; + Object value; //Emit entry : jo.entrySet()) { + final String key = entry.getKey(); if (!"tagName".equals(key) && !"childNodes".equals(key)) { XML.noSpace(key); - value = jo.optString(key); + value = entry.getValue(); if (value != null) { sb.append(' '); sb.append(XML.escape(key)); sb.append('='); sb.append('"'); - sb.append(XML.escape(value)); + sb.append(XML.escape(value.toString())); sb.append('"'); } } diff --git a/JSONObject.java b/JSONObject.java index e7db5d1..0216567 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -245,14 +245,14 @@ public class JSONObject { /** * Construct a JSONObject from a Map. * - * @param map + * @param m * A map object that can be used to initialize the contents of * the JSONObject. */ - public JSONObject(Map map) { + public JSONObject(Map m) { this.map = new HashMap(); - if (map != null) { - for (final Entry e : map.entrySet()) { + if (m != null) { + for (final Entry e : m.entrySet()) { final Object value = e.getValue(); if (value != null) { this.map.put(String.valueOf(e.getKey()), wrap(value)); @@ -729,14 +729,7 @@ public class JSONObject { if (length == 0) { return null; } - Iterator iterator = jo.keys(); - String[] names = new String[length]; - int i = 0; - while (iterator.hasNext()) { - names[i] = iterator.next(); - i++; - } - return names; + return jo.keySet().toArray(new String[length]); } /** @@ -837,8 +830,11 @@ public class JSONObject { } /** - * Get an enumeration of the keys of the JSONObject. + * Get an enumeration of the keys of the JSONObject. Modifying this key Set will also + * modify the JSONObject. Use with caution. * + * @see Set#iterator() + * * @return An iterator of the keys. */ public Iterator keys() { @@ -846,7 +842,10 @@ public class JSONObject { } /** - * Get a set of keys of the JSONObject. + * Get a set of keys of the JSONObject. Modifying this key Set will also modify the + * JSONObject. Use with caution. + * + * @see Map#keySet() * * @return A keySet. */ @@ -854,6 +853,22 @@ public class JSONObject { return this.map.keySet(); } + /** + * Get a set of entries of the JSONObject. These are raw values and may not + * match what is returned by the JSONObject get* and opt* functions. Modifying + * the returned EntrySet or the Entry objects contained therein will modify the + * backing JSONObject. This does not return a clone or a read-only view. + * + * Use with caution. + * + * @see Map#entrySet() + * + * @return A keySet. + */ + protected Set> entrySet() { + return this.map.entrySet(); + } + /** * Get the number of keys stored in the JSONObject. * @@ -871,12 +886,10 @@ public class JSONObject { * is empty. */ public JSONArray names() { - JSONArray ja = new JSONArray(); - Iterator keys = this.keys(); - while (keys.hasNext()) { - ja.put(keys.next()); - } - return ja.length() == 0 ? null : ja; + if(this.map.isEmpty()) { + return null; + } + return new JSONArray(this.map.keySet()); } /** @@ -1762,15 +1775,19 @@ public class JSONObject { if (!(other instanceof JSONObject)) { return false; } - Set set = this.keySet(); - if (!set.equals(((JSONObject)other).keySet())) { + if (!this.keySet().equals(((JSONObject)other).keySet())) { return false; } - Iterator iterator = set.iterator(); - while (iterator.hasNext()) { - String name = iterator.next(); - Object valueThis = this.get(name); + for (final Entry entry : this.entrySet()) { + String name = entry.getKey(); + Object valueThis = entry.getValue(); Object valueOther = ((JSONObject)other).get(name); + if(valueThis == valueOther) { + return true; + } + if(valueThis == null) { + return false; + } if (valueThis instanceof JSONObject) { if (!((JSONObject)valueThis).similar(valueOther)) { return false; @@ -2220,21 +2237,19 @@ public class JSONObject { try { boolean commanate = false; final int length = this.length(); - Iterator keys = this.keys(); writer.write('{'); if (length == 1) { - Object key = keys.next(); - writer.write(quote(key.toString())); + final Entry entry = this.entrySet().iterator().next(); + writer.write(quote(entry.getKey())); writer.write(':'); if (indentFactor > 0) { writer.write(' '); } - writeValue(writer, this.map.get(key), indentFactor, indent); + writeValue(writer, entry.getValue(), indentFactor, indent); } else if (length != 0) { final int newindent = indent + indentFactor; - while (keys.hasNext()) { - Object key = keys.next(); + for (final Entry entry : this.entrySet()) { if (commanate) { writer.write(','); } @@ -2242,12 +2257,12 @@ public class JSONObject { writer.write('\n'); } indent(writer, newindent); - writer.write(quote(key.toString())); + writer.write(quote(entry.getKey())); writer.write(':'); if (indentFactor > 0) { writer.write(' '); } - writeValue(writer, this.map.get(key), indentFactor, newindent); + writeValue(writer, entry.getValue(), indentFactor, newindent); commanate = true; } if (indentFactor > 0) { @@ -2273,7 +2288,7 @@ public class JSONObject { */ public Map toMap() { Map results = new HashMap(); - for (Entry entry : this.map.entrySet()) { + for (Entry entry : this.entrySet()) { Object value; if (entry.getValue() == null || NULL.equals(entry.getValue())) { value = null; diff --git a/Property.java b/Property.java index 73ddb12..4f1d7c4 100644 --- a/Property.java +++ b/Property.java @@ -25,7 +25,7 @@ SOFTWARE. */ import java.util.Enumeration; -import java.util.Iterator; +import java.util.Map.Entry; import java.util.Properties; /** @@ -61,10 +61,11 @@ public class Property { public static Properties toProperties(JSONObject jo) throws JSONException { Properties properties = new Properties(); if (jo != null) { - Iterator keys = jo.keys(); - while (keys.hasNext()) { - String name = keys.next(); - properties.put(name, jo.getString(name)); + for (final Entry entry : jo.entrySet()) { + Object value = entry.getValue(); + if (!JSONObject.NULL.equals(value)) { + properties.put(entry.getKey(), value.toString()); + } } } return properties; diff --git a/XML.java b/XML.java index a438078..4dd9a2c 100644 --- a/XML.java +++ b/XML.java @@ -25,6 +25,7 @@ SOFTWARE. */ import java.util.Iterator; +import java.util.Map.Entry; /** * This provides static methods to convert an XML text into a JSONObject, and to @@ -513,10 +514,7 @@ public class XML { StringBuilder sb = new StringBuilder(); JSONArray ja; JSONObject jo; - String key; - Iterator keys; String string; - Object value; if (object instanceof JSONObject) { @@ -529,16 +527,14 @@ public class XML { // Loop thru the keys. jo = (JSONObject) object; - keys = jo.keys(); - while (keys.hasNext()) { - key = keys.next(); - value = jo.opt(key); + for (final Entry entry : jo.entrySet()) { + final String key = entry.getKey(); + Object value = entry.getValue(); if (value == null) { value = ""; } else if (value.getClass().isArray()) { value = new JSONArray(value); } - string = value instanceof String ? (String) value : null; // Emit content in body if ("content".equals(key)) {