From c870094f691e7b9e690111ed780a6e5d73a6fa68 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 16 May 2017 15:35:05 -0400 Subject: [PATCH] Fixes spelling in comments and removes compile time warnings --- CookieList.java | 2 +- JSONArray.java | 7 ++++--- JSONObject.java | 52 +++++++++++++++++++++++++++++++++++------------ JSONPointer.java | 34 ++++++++++++++++--------------- JSONStringer.java | 1 + JSONWriter.java | 10 ++++----- XML.java | 30 +++++++++++++-------------- 7 files changed, 82 insertions(+), 54 deletions(-) diff --git a/CookieList.java b/CookieList.java index 7a5628e91..d69e45753 100644 --- a/CookieList.java +++ b/CookieList.java @@ -39,7 +39,7 @@ public class CookieList { * The pairs are separated by ';'. The names and the values * will be unescaped, possibly converting '+' and '%' sequences. * - * To add a cookie to a cooklist, + * To add a cookie to a cookie list, * cookielistJSONObject.put(cookieJSONObject.getString("name"), * cookieJSONObject.getString("value")); * @param string A cookie list string diff --git a/JSONArray.java b/JSONArray.java index 132d46de1..36e7cadd4 100644 --- a/JSONArray.java +++ b/JSONArray.java @@ -183,7 +183,7 @@ public JSONArray(Object array) throws JSONException { @Override public Iterator iterator() { - return myArrayList.iterator(); + return this.myArrayList.iterator(); } /** @@ -1115,6 +1115,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException { * @return a printable, displayable, transmittable representation of the * array. */ + @Override public String toString() { try { return this.toString(0); @@ -1124,7 +1125,7 @@ public String toString() { } /** - * Make a prettyprinted JSON text of this JSONArray. Warning: This method + * Make a pretty-printed JSON text of this JSONArray. Warning: This method * assumes that the data structure is acyclical. * * @param indentFactor @@ -1166,7 +1167,7 @@ public Writer write(Writer writer) throws JSONException { * @param indentFactor * The number of spaces to add to each level of indentation. * @param indent - * The indention of the top level. + * The indentation of the top level. * @return The writer. * @throws JSONException */ diff --git a/JSONObject.java b/JSONObject.java index b71b3b618..f58f2dcbb 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -126,6 +126,15 @@ protected final Object clone() { public boolean equals(Object object) { return object == null || object == this; } + /** + * A Null object is equal to the null value and to itself. + * + * @return always returns 0. + */ + @Override + public int hashCode() { + return 0; + } /** * Get the "null" string value. @@ -754,13 +763,13 @@ public JSONObject increment(String key) throws JSONException { } else if (value instanceof BigDecimal) { this.put(key, ((BigDecimal)value).add(BigDecimal.ONE)); } else if (value instanceof Integer) { - this.put(key, (Integer) value + 1); + this.put(key, ((Integer) value).intValue() + 1); } else if (value instanceof Long) { - this.put(key, (Long) value + 1); + this.put(key, ((Long) value).longValue() + 1L); } else if (value instanceof Double) { - this.put(key, (Double) value + 1); + this.put(key, ((Double) value).doubleValue() + 1D); } else if (value instanceof Float) { - this.put(key, (Float) value + 1); + this.put(key, ((Float) value).floatValue() + 1F); } else { throw new JSONException("Unable to increment [" + quote(key) + "]."); } @@ -887,7 +896,7 @@ public > E optEnum(Class clazz, String key) { * @param defaultValue * The default in case the value is not found * @return The enum value associated with the key or defaultValue - * if the value is not found or cannot be assigned to clazz + * if the value is not found or cannot be assigned to clazz */ public > E optEnum(Class clazz, String key, E defaultValue) { try { @@ -1218,7 +1227,23 @@ public JSONObject put(String key, Collection value) throws JSONException { * If the key is null or if the number is invalid. */ public JSONObject put(String key, double value) throws JSONException { - this.put(key, new Double(value)); + this.put(key, Double.valueOf(value)); + return this; + } + + /** + * Put a key/float pair in the JSONObject. + * + * @param key + * A key string. + * @param value + * A float which is the value. + * @return this. + * @throws JSONException + * If the key is null or if the number is invalid. + */ + public JSONObject put(String key, float value) throws JSONException { + this.put(key, Float.valueOf(value)); return this; } @@ -1234,7 +1259,7 @@ public JSONObject put(String key, double value) throws JSONException { * If the key is null. */ public JSONObject put(String key, int value) throws JSONException { - this.put(key, new Integer(value)); + this.put(key, Integer.valueOf(value)); return this; } @@ -1250,7 +1275,7 @@ public JSONObject put(String key, int value) throws JSONException { * If the key is null. */ public JSONObject put(String key, long value) throws JSONException { - this.put(key, new Long(value)); + this.put(key, Long.valueOf(value)); return this; } @@ -1340,7 +1365,7 @@ public JSONObject putOpt(String key, Object value) throws JSONException { } /** - * Creates a JSONPointer using an intialization string and tries to + * Creates a JSONPointer using an initialization string and tries to * match it to an item within this JSONObject. For example, given a * JSONObject initialized with this document: *
@@ -1362,7 +1387,7 @@ public Object query(String jsonPointer) {
         return query(new JSONPointer(jsonPointer));
     }
     /**
-     * Uses a uaer initialized JSONPointer  and tries to 
+     * Uses a user initialized JSONPointer  and tries to 
      * match it to an item within this JSONObject. For example, given a
      * JSONObject initialized with this document:
      * 
@@ -1665,7 +1690,7 @@ public String toString() {
     }
 
     /**
-     * Make a prettyprinted JSON text of this JSONObject.
+     * Make a pretty-printed JSON text of this JSONObject.
      * 

* Warning: This method assumes that the data structure is acyclical. * @@ -1730,7 +1755,8 @@ public static String valueToString(Object value) throws JSONException { final String numberAsString = numberToString((Number) value); try { // Use the BigDecimal constructor for it's parser to validate the format. - new BigDecimal(numberAsString); + @SuppressWarnings("unused") + BigDecimal unused = new BigDecimal(numberAsString); // Close enough to a JSON number that we will return it unquoted return numberAsString; } catch (NumberFormatException ex){ @@ -1891,7 +1917,7 @@ static final void indent(Writer writer, int indent) throws IOException { * @param indentFactor * The number of spaces to add to each level of indentation. * @param indent - * The indention of the top level. + * The indentation of the top level. * @return The writer. * @throws JSONException */ diff --git a/JSONPointer.java b/JSONPointer.java index 82de7f933..8142f9a6c 100644 --- a/JSONPointer.java +++ b/JSONPointer.java @@ -68,11 +68,11 @@ public static class Builder { * {@link #append(String)} method calls. */ public JSONPointer build() { - return new JSONPointer(refTokens); + return new JSONPointer(this.refTokens); } /** - * Adds an arbitary token to the list of reference tokens. It can be any non-null value. + * Adds an arbitrary token to the list of reference tokens. It can be any non-null value. * * Unlike in the case of JSON string or URI fragment representation of JSON pointers, the * argument of this method MUST NOT be escaped. If you want to query the property called @@ -87,7 +87,7 @@ public Builder append(String token) { if (token == null) { throw new NullPointerException("token cannot be null"); } - refTokens.add(token); + this.refTokens.add(token); return this; } @@ -99,7 +99,7 @@ public Builder append(String token) { * @return {@code this} */ public Builder append(int arrayIndex) { - refTokens.add(String.valueOf(arrayIndex)); + this.refTokens.add(String.valueOf(arrayIndex)); return this; } } @@ -134,29 +134,30 @@ public static Builder builder() { * @param pointer the JSON String or URI Fragment representation of the JSON pointer. * @throws IllegalArgumentException if {@code pointer} is not a valid JSON pointer */ - public JSONPointer(String pointer) { + public JSONPointer(final String pointer) { if (pointer == null) { throw new NullPointerException("pointer cannot be null"); } if (pointer.isEmpty() || pointer.equals("#")) { - refTokens = Collections.emptyList(); + this.refTokens = Collections.emptyList(); return; } + String refs; if (pointer.startsWith("#/")) { - pointer = pointer.substring(2); + refs = pointer.substring(2); try { - pointer = URLDecoder.decode(pointer, ENCODING); + refs = URLDecoder.decode(refs, ENCODING); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else if (pointer.startsWith("/")) { - pointer = pointer.substring(1); + refs = pointer.substring(1); } else { throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'"); } - refTokens = new ArrayList(); - for (String token : pointer.split("/")) { - refTokens.add(unescape(token)); + this.refTokens = new ArrayList(); + for (String token : refs.split("/")) { + this.refTokens.add(unescape(token)); } } @@ -181,11 +182,11 @@ private String unescape(String token) { * @throws JSONPointerException if an error occurs during evaluation */ public Object queryFrom(Object document) { - if (refTokens.isEmpty()) { + if (this.refTokens.isEmpty()) { return document; } Object current = document; - for (String token : refTokens) { + for (String token : this.refTokens) { if (current instanceof JSONObject) { current = ((JSONObject) current).opt(unescape(token)); } else if (current instanceof JSONArray) { @@ -206,6 +207,7 @@ public Object queryFrom(Object document) { * @return the matched object. If no matching item is found a * JSONPointerException is thrown */ + @SuppressWarnings("boxing") private Object readByIndexToken(Object current, String indexToken) { try { int index = Integer.parseInt(indexToken); @@ -227,7 +229,7 @@ private Object readByIndexToken(Object current, String indexToken) { @Override public String toString() { StringBuilder rval = new StringBuilder(""); - for (String token: refTokens) { + for (String token: this.refTokens) { rval.append('/').append(escape(token)); } return rval.toString(); @@ -255,7 +257,7 @@ private String escape(String token) { public String toURIFragment() { try { StringBuilder rval = new StringBuilder("#"); - for (String token : refTokens) { + for (String token : this.refTokens) { rval.append('/').append(URLEncoder.encode(token, ENCODING)); } return rval.toString(); diff --git a/JSONStringer.java b/JSONStringer.java index 5fbc96a9a..6e05d228f 100644 --- a/JSONStringer.java +++ b/JSONStringer.java @@ -72,6 +72,7 @@ public JSONStringer() { * endArray). * @return The JSON text. */ + @Override public String toString() { return this.mode == 'd' ? this.writer.toString() : null; } diff --git a/JSONWriter.java b/JSONWriter.java index 696ee2307..549f93ee6 100644 --- a/JSONWriter.java +++ b/JSONWriter.java @@ -149,18 +149,18 @@ public JSONWriter array() throws JSONException { /** * End something. - * @param mode Mode + * @param m Mode * @param c Closing character * @return this * @throws JSONException If unbalanced. */ - private JSONWriter end(char mode, char c) throws JSONException { - if (this.mode != mode) { - throw new JSONException(mode == 'a' + private JSONWriter end(char m, char c) throws JSONException { + if (this.mode != m) { + throw new JSONException(m == 'a' ? "Misplaced endArray." : "Misplaced endObject."); } - this.pop(mode); + this.pop(m); try { this.writer.append(c); } catch (IOException e) { diff --git a/XML.java b/XML.java index 78dd6a00c..a43807866 100644 --- a/XML.java +++ b/XML.java @@ -211,7 +211,7 @@ public static String unescape(String string) { sb.append('<'); } else if ("gt".equalsIgnoreCase(entity)) { sb.append('>'); - } else { + } else {// unsupported xml entity. leave encoded sb.append('&').append(entity).append(';'); } } @@ -219,7 +219,7 @@ public static String unescape(String string) { i += entity.length() + 1; } else { // this shouldn't happen in most cases since the parser - // errors on unclosed enties. + // errors on unclosed entries. sb.append(c); } } else { @@ -508,7 +508,7 @@ public static String toString(Object object) throws JSONException { * @return A string. * @throws JSONException Thrown if there is an error parsing the string */ - public static String toString(Object object, String tagName) + public static String toString(final Object object, final String tagName) throws JSONException { StringBuilder sb = new StringBuilder(); JSONArray ja; @@ -595,21 +595,19 @@ public static String toString(Object object, String tagName) } - if (object != null) { - if (object.getClass().isArray()) { - object = new JSONArray(object); - } - - if (object instanceof JSONArray) { + if (object != null && (object instanceof JSONArray || object.getClass().isArray())) { + if(object.getClass().isArray()) { + ja = new JSONArray(object); + } else { ja = (JSONArray) object; - for (Object val : ja) { - // XML does not have good support for arrays. If an array - // appears in a place where XML is lacking, synthesize an - // element. - sb.append(toString(val, tagName == null ? "array" : tagName)); - } - return sb.toString(); } + for (Object val : ja) { + // XML does not have good support for arrays. If an array + // appears in a place where XML is lacking, synthesize an + // element. + sb.append(toString(val, tagName == null ? "array" : tagName)); + } + return sb.toString(); } string = (object == null) ? "null" : escape(object.toString());