1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Fixes spelling in comments and removes compile time warnings

This commit is contained in:
John J. Aylward 2017-05-16 15:35:05 -04:00
parent ae1e9e2b6a
commit c870094f69
7 changed files with 82 additions and 54 deletions

View file

@ -39,7 +39,7 @@ public class CookieList {
* The pairs are separated by ';'. The names and the values * The pairs are separated by ';'. The names and the values
* will be unescaped, possibly converting '+' and '%' sequences. * 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"), * cookielistJSONObject.put(cookieJSONObject.getString("name"),
* cookieJSONObject.getString("value")); * cookieJSONObject.getString("value"));
* @param string A cookie list string * @param string A cookie list string

View file

@ -183,7 +183,7 @@ public class JSONArray implements Iterable<Object> {
@Override @Override
public Iterator<Object> iterator() { public Iterator<Object> iterator() {
return myArrayList.iterator(); return this.myArrayList.iterator();
} }
/** /**
@ -1115,6 +1115,7 @@ public class JSONArray implements Iterable<Object> {
* @return a printable, displayable, transmittable representation of the * @return a printable, displayable, transmittable representation of the
* array. * array.
*/ */
@Override
public String toString() { public String toString() {
try { try {
return this.toString(0); return this.toString(0);
@ -1124,7 +1125,7 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* 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. * assumes that the data structure is acyclical.
* *
* @param indentFactor * @param indentFactor
@ -1166,7 +1167,7 @@ public class JSONArray implements Iterable<Object> {
* @param indentFactor * @param indentFactor
* The number of spaces to add to each level of indentation. * The number of spaces to add to each level of indentation.
* @param indent * @param indent
* The indention of the top level. * The indentation of the top level.
* @return The writer. * @return The writer.
* @throws JSONException * @throws JSONException
*/ */

View file

@ -126,6 +126,15 @@ public class JSONObject {
public boolean equals(Object object) { public boolean equals(Object object) {
return object == null || object == this; 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. * Get the "null" string value.
@ -754,13 +763,13 @@ public class JSONObject {
} else if (value instanceof BigDecimal) { } else if (value instanceof BigDecimal) {
this.put(key, ((BigDecimal)value).add(BigDecimal.ONE)); this.put(key, ((BigDecimal)value).add(BigDecimal.ONE));
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
this.put(key, (Integer) value + 1); this.put(key, ((Integer) value).intValue() + 1);
} else if (value instanceof Long) { } else if (value instanceof Long) {
this.put(key, (Long) value + 1); this.put(key, ((Long) value).longValue() + 1L);
} else if (value instanceof Double) { } else if (value instanceof Double) {
this.put(key, (Double) value + 1); this.put(key, ((Double) value).doubleValue() + 1D);
} else if (value instanceof Float) { } else if (value instanceof Float) {
this.put(key, (Float) value + 1); this.put(key, ((Float) value).floatValue() + 1F);
} else { } else {
throw new JSONException("Unable to increment [" + quote(key) + "]."); throw new JSONException("Unable to increment [" + quote(key) + "].");
} }
@ -887,7 +896,7 @@ public class JSONObject {
* @param defaultValue * @param defaultValue
* The default in case the value is not found * The default in case the value is not found
* @return The enum value associated with the key or defaultValue * @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 <code>clazz</code>
*/ */
public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) { public <E extends Enum<E>> E optEnum(Class<E> clazz, String key, E defaultValue) {
try { try {
@ -1218,7 +1227,23 @@ public class JSONObject {
* If the key is null or if the number is invalid. * If the key is null or if the number is invalid.
*/ */
public JSONObject put(String key, double value) throws JSONException { 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; return this;
} }
@ -1234,7 +1259,7 @@ public class JSONObject {
* If the key is null. * If the key is null.
*/ */
public JSONObject put(String key, int value) throws JSONException { public JSONObject put(String key, int value) throws JSONException {
this.put(key, new Integer(value)); this.put(key, Integer.valueOf(value));
return this; return this;
} }
@ -1250,7 +1275,7 @@ public class JSONObject {
* If the key is null. * If the key is null.
*/ */
public JSONObject put(String key, long value) throws JSONException { public JSONObject put(String key, long value) throws JSONException {
this.put(key, new Long(value)); this.put(key, Long.valueOf(value));
return this; return this;
} }
@ -1340,7 +1365,7 @@ public class JSONObject {
} }
/** /**
* 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 * match it to an item within this JSONObject. For example, given a
* JSONObject initialized with this document: * JSONObject initialized with this document:
* <pre> * <pre>
@ -1362,7 +1387,7 @@ public class JSONObject {
return query(new JSONPointer(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 * match it to an item within this JSONObject. For example, given a
* JSONObject initialized with this document: * JSONObject initialized with this document:
* <pre> * <pre>
@ -1665,7 +1690,7 @@ public class JSONObject {
} }
/** /**
* Make a prettyprinted JSON text of this JSONObject. * Make a pretty-printed JSON text of this JSONObject.
* <p> * <p>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* *
@ -1730,7 +1755,8 @@ public class JSONObject {
final String numberAsString = numberToString((Number) value); final String numberAsString = numberToString((Number) value);
try { try {
// Use the BigDecimal constructor for it's parser to validate the format. // 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 // Close enough to a JSON number that we will return it unquoted
return numberAsString; return numberAsString;
} catch (NumberFormatException ex){ } catch (NumberFormatException ex){
@ -1891,7 +1917,7 @@ public class JSONObject {
* @param indentFactor * @param indentFactor
* The number of spaces to add to each level of indentation. * The number of spaces to add to each level of indentation.
* @param indent * @param indent
* The indention of the top level. * The indentation of the top level.
* @return The writer. * @return The writer.
* @throws JSONException * @throws JSONException
*/ */

View file

@ -68,11 +68,11 @@ public class JSONPointer {
* {@link #append(String)} method calls. * {@link #append(String)} method calls.
*/ */
public JSONPointer build() { 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 * 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 * argument of this method MUST NOT be escaped. If you want to query the property called
@ -87,7 +87,7 @@ public class JSONPointer {
if (token == null) { if (token == null) {
throw new NullPointerException("token cannot be null"); throw new NullPointerException("token cannot be null");
} }
refTokens.add(token); this.refTokens.add(token);
return this; return this;
} }
@ -99,7 +99,7 @@ public class JSONPointer {
* @return {@code this} * @return {@code this}
*/ */
public Builder append(int arrayIndex) { public Builder append(int arrayIndex) {
refTokens.add(String.valueOf(arrayIndex)); this.refTokens.add(String.valueOf(arrayIndex));
return this; return this;
} }
} }
@ -134,29 +134,30 @@ public class JSONPointer {
* @param pointer the JSON String or URI Fragment representation of the JSON pointer. * @param pointer the JSON String or URI Fragment representation of the JSON pointer.
* @throws IllegalArgumentException if {@code pointer} is not a valid 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) { if (pointer == null) {
throw new NullPointerException("pointer cannot be null"); throw new NullPointerException("pointer cannot be null");
} }
if (pointer.isEmpty() || pointer.equals("#")) { if (pointer.isEmpty() || pointer.equals("#")) {
refTokens = Collections.emptyList(); this.refTokens = Collections.emptyList();
return; return;
} }
String refs;
if (pointer.startsWith("#/")) { if (pointer.startsWith("#/")) {
pointer = pointer.substring(2); refs = pointer.substring(2);
try { try {
pointer = URLDecoder.decode(pointer, ENCODING); refs = URLDecoder.decode(refs, ENCODING);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} else if (pointer.startsWith("/")) { } else if (pointer.startsWith("/")) {
pointer = pointer.substring(1); refs = pointer.substring(1);
} else { } else {
throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'"); throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
} }
refTokens = new ArrayList<String>(); this.refTokens = new ArrayList<String>();
for (String token : pointer.split("/")) { for (String token : refs.split("/")) {
refTokens.add(unescape(token)); this.refTokens.add(unescape(token));
} }
} }
@ -181,11 +182,11 @@ public class JSONPointer {
* @throws JSONPointerException if an error occurs during evaluation * @throws JSONPointerException if an error occurs during evaluation
*/ */
public Object queryFrom(Object document) { public Object queryFrom(Object document) {
if (refTokens.isEmpty()) { if (this.refTokens.isEmpty()) {
return document; return document;
} }
Object current = document; Object current = document;
for (String token : refTokens) { for (String token : this.refTokens) {
if (current instanceof JSONObject) { if (current instanceof JSONObject) {
current = ((JSONObject) current).opt(unescape(token)); current = ((JSONObject) current).opt(unescape(token));
} else if (current instanceof JSONArray) { } else if (current instanceof JSONArray) {
@ -206,6 +207,7 @@ public class JSONPointer {
* @return the matched object. If no matching item is found a * @return the matched object. If no matching item is found a
* JSONPointerException is thrown * JSONPointerException is thrown
*/ */
@SuppressWarnings("boxing")
private Object readByIndexToken(Object current, String indexToken) { private Object readByIndexToken(Object current, String indexToken) {
try { try {
int index = Integer.parseInt(indexToken); int index = Integer.parseInt(indexToken);
@ -227,7 +229,7 @@ public class JSONPointer {
@Override @Override
public String toString() { public String toString() {
StringBuilder rval = new StringBuilder(""); StringBuilder rval = new StringBuilder("");
for (String token: refTokens) { for (String token: this.refTokens) {
rval.append('/').append(escape(token)); rval.append('/').append(escape(token));
} }
return rval.toString(); return rval.toString();
@ -255,7 +257,7 @@ public class JSONPointer {
public String toURIFragment() { public String toURIFragment() {
try { try {
StringBuilder rval = new StringBuilder("#"); StringBuilder rval = new StringBuilder("#");
for (String token : refTokens) { for (String token : this.refTokens) {
rval.append('/').append(URLEncoder.encode(token, ENCODING)); rval.append('/').append(URLEncoder.encode(token, ENCODING));
} }
return rval.toString(); return rval.toString();

View file

@ -72,6 +72,7 @@ public class JSONStringer extends JSONWriter {
* <code>endArray</code>). * <code>endArray</code>).
* @return The JSON text. * @return The JSON text.
*/ */
@Override
public String toString() { public String toString() {
return this.mode == 'd' ? this.writer.toString() : null; return this.mode == 'd' ? this.writer.toString() : null;
} }

View file

@ -149,18 +149,18 @@ public class JSONWriter {
/** /**
* End something. * End something.
* @param mode Mode * @param m Mode
* @param c Closing character * @param c Closing character
* @return this * @return this
* @throws JSONException If unbalanced. * @throws JSONException If unbalanced.
*/ */
private JSONWriter end(char mode, char c) throws JSONException { private JSONWriter end(char m, char c) throws JSONException {
if (this.mode != mode) { if (this.mode != m) {
throw new JSONException(mode == 'a' throw new JSONException(m == 'a'
? "Misplaced endArray." ? "Misplaced endArray."
: "Misplaced endObject."); : "Misplaced endObject.");
} }
this.pop(mode); this.pop(m);
try { try {
this.writer.append(c); this.writer.append(c);
} catch (IOException e) { } catch (IOException e) {

View file

@ -211,7 +211,7 @@ public class XML {
sb.append('<'); sb.append('<');
} else if ("gt".equalsIgnoreCase(entity)) { } else if ("gt".equalsIgnoreCase(entity)) {
sb.append('>'); sb.append('>');
} else { } else {// unsupported xml entity. leave encoded
sb.append('&').append(entity).append(';'); sb.append('&').append(entity).append(';');
} }
} }
@ -219,7 +219,7 @@ public class XML {
i += entity.length() + 1; i += entity.length() + 1;
} else { } else {
// this shouldn't happen in most cases since the parser // this shouldn't happen in most cases since the parser
// errors on unclosed enties. // errors on unclosed entries.
sb.append(c); sb.append(c);
} }
} else { } else {
@ -508,7 +508,7 @@ public class XML {
* @return A string. * @return A string.
* @throws JSONException Thrown if there is an error parsing the 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 { throws JSONException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
JSONArray ja; JSONArray ja;
@ -595,13 +595,12 @@ public class XML {
} }
if (object != null) { if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
if(object.getClass().isArray()) { if(object.getClass().isArray()) {
object = new JSONArray(object); ja = new JSONArray(object);
} } else {
if (object instanceof JSONArray) {
ja = (JSONArray) object; ja = (JSONArray) object;
}
for (Object val : ja) { for (Object val : ja) {
// XML does not have good support for arrays. If an array // XML does not have good support for arrays. If an array
// appears in a place where XML is lacking, synthesize an // appears in a place where XML is lacking, synthesize an
@ -610,7 +609,6 @@ public class XML {
} }
return sb.toString(); return sb.toString();
} }
}
string = (object == null) ? "null" : escape(object.toString()); string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\"" return (tagName == null) ? "\"" + string + "\""