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

Remove 0x-

This commit is contained in:
Douglas Crockford 2011-11-24 07:45:34 -08:00
parent 2a39f47197
commit 806900e0d1
3 changed files with 139 additions and 144 deletions

View file

@ -73,12 +73,10 @@ import java.util.Map;
* <code>false</code>, or <code>null</code>.</li>
* <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
* well as by <code>,</code> <small>(comma)</small>.</li>
* <li>Numbers may have the
* <code>0x-</code> <small>(hex)</small> prefix.</li>
* </ul>
* @author JSON.org
* @version 2011-08-25
* @version 2011-11-24
*/
public class JSONArray {
@ -86,7 +84,7 @@ public class JSONArray {
/**
* The arrayList where the JSONArray's properties are kept.
*/
private ArrayList myArrayList;
private final ArrayList myArrayList;
/**
@ -155,12 +153,12 @@ public class JSONArray {
if (collection != null) {
Iterator iter = collection.iterator();
while (iter.hasNext()) {
this.myArrayList.add(JSONObject.wrap(iter.next()));
this.myArrayList.add(JSONObject.wrap(iter.next()));
}
}
}
/**
* Construct a JSONArray from an array
* @throws JSONException If not an array.
@ -177,8 +175,8 @@ public class JSONArray {
"JSONArray initial value should be a string or collection or array.");
}
}
/**
* Get the object value associated with an index.
* @param index
@ -187,7 +185,7 @@ public class JSONArray {
* @throws JSONException If there is no value for the index.
*/
public Object get(int index) throws JSONException {
Object object = opt(index);
Object object = this.opt(index);
if (object == null) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
@ -205,7 +203,7 @@ public class JSONArray {
* value is not convertible to boolean.
*/
public boolean getBoolean(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
if (object.equals(Boolean.FALSE) ||
(object instanceof String &&
((String)object).equalsIgnoreCase("false"))) {
@ -228,11 +226,11 @@ public class JSONArray {
* be converted to a number.
*/
public double getDouble(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
try {
return object instanceof Number ?
((Number)object).doubleValue() :
Double.parseDouble((String)object);
return object instanceof Number
? ((Number)object).doubleValue()
: Double.parseDouble((String)object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] is not a number.");
@ -248,11 +246,11 @@ public class JSONArray {
* @throws JSONException If the key is not found or if the value is not a number.
*/
public int getInt(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
try {
return object instanceof Number ?
((Number)object).intValue() :
Integer.parseInt((String)object);
return object instanceof Number
? ((Number)object).intValue()
: Integer.parseInt((String)object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] is not a number.");
@ -268,7 +266,7 @@ public class JSONArray {
* value is not a JSONArray
*/
public JSONArray getJSONArray(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
if (object instanceof JSONArray) {
return (JSONArray)object;
}
@ -285,7 +283,7 @@ public class JSONArray {
* value is not a JSONObject
*/
public JSONObject getJSONObject(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
if (object instanceof JSONObject) {
return (JSONObject)object;
}
@ -303,11 +301,11 @@ public class JSONArray {
* be converted to a number.
*/
public long getLong(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
try {
return object instanceof Number ?
((Number)object).longValue() :
Long.parseLong((String)object);
return object instanceof Number
? ((Number)object).longValue()
: Long.parseLong((String)object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] is not a number.");
@ -322,7 +320,7 @@ public class JSONArray {
* @throws JSONException If there is no string value for the index.
*/
public String getString(int index) throws JSONException {
Object object = get(index);
Object object = this.get(index);
if (object instanceof String) {
return (String)object;
}
@ -336,7 +334,7 @@ public class JSONArray {
* @return true if the value at the index is null, or if there is no value.
*/
public boolean isNull(int index) {
return JSONObject.NULL.equals(opt(index));
return JSONObject.NULL.equals(this.opt(index));
}
@ -349,7 +347,7 @@ public class JSONArray {
* @throws JSONException If the array contains an invalid number.
*/
public String join(String separator) throws JSONException {
int len = length();
int len = this.length();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i += 1) {
@ -379,8 +377,9 @@ public class JSONArray {
* object at that index.
*/
public Object opt(int index) {
return (index < 0 || index >= length()) ?
null : this.myArrayList.get(index);
return (index < 0 || index >= this.length())
? null
: this.myArrayList.get(index);
}
@ -393,7 +392,7 @@ public class JSONArray {
* @return The truth.
*/
public boolean optBoolean(int index) {
return optBoolean(index, false);
return this.optBoolean(index, false);
}
@ -408,7 +407,7 @@ public class JSONArray {
*/
public boolean optBoolean(int index, boolean defaultValue) {
try {
return getBoolean(index);
return this.getBoolean(index);
} catch (Exception e) {
return defaultValue;
}
@ -424,7 +423,7 @@ public class JSONArray {
* @return The value.
*/
public double optDouble(int index) {
return optDouble(index, Double.NaN);
return this.optDouble(index, Double.NaN);
}
@ -439,7 +438,7 @@ public class JSONArray {
*/
public double optDouble(int index, double defaultValue) {
try {
return getDouble(index);
return this.getDouble(index);
} catch (Exception e) {
return defaultValue;
}
@ -455,7 +454,7 @@ public class JSONArray {
* @return The value.
*/
public int optInt(int index) {
return optInt(index, 0);
return this.optInt(index, 0);
}
@ -469,7 +468,7 @@ public class JSONArray {
*/
public int optInt(int index, int defaultValue) {
try {
return getInt(index);
return this.getInt(index);
} catch (Exception e) {
return defaultValue;
}
@ -483,7 +482,7 @@ public class JSONArray {
* or if the value is not a JSONArray.
*/
public JSONArray optJSONArray(int index) {
Object o = opt(index);
Object o = this.opt(index);
return o instanceof JSONArray ? (JSONArray)o : null;
}
@ -497,7 +496,7 @@ public class JSONArray {
* @return A JSONObject value.
*/
public JSONObject optJSONObject(int index) {
Object o = opt(index);
Object o = this.opt(index);
return o instanceof JSONObject ? (JSONObject)o : null;
}
@ -511,7 +510,7 @@ public class JSONArray {
* @return The value.
*/
public long optLong(int index) {
return optLong(index, 0);
return this.optLong(index, 0);
}
@ -525,7 +524,7 @@ public class JSONArray {
*/
public long optLong(int index, long defaultValue) {
try {
return getLong(index);
return this.getLong(index);
} catch (Exception e) {
return defaultValue;
}
@ -541,7 +540,7 @@ public class JSONArray {
* @return A String value.
*/
public String optString(int index) {
return optString(index, "");
return this.optString(index, "");
}
@ -554,8 +553,10 @@ public class JSONArray {
* @return A String value.
*/
public String optString(int index, String defaultValue) {
Object object = opt(index);
return JSONObject.NULL.equals(object) ? object.toString() : defaultValue;
Object object = this.opt(index);
return JSONObject.NULL.equals(object)
? object.toString()
: defaultValue;
}
@ -566,7 +567,7 @@ public class JSONArray {
* @return this.
*/
public JSONArray put(boolean value) {
put(value ? Boolean.TRUE : Boolean.FALSE);
this.put(value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
@ -578,7 +579,7 @@ public class JSONArray {
* @return this.
*/
public JSONArray put(Collection value) {
put(new JSONArray(value));
this.put(new JSONArray(value));
return this;
}
@ -593,7 +594,7 @@ public class JSONArray {
public JSONArray put(double value) throws JSONException {
Double d = new Double(value);
JSONObject.testValidity(d);
put(d);
this.put(d);
return this;
}
@ -605,7 +606,7 @@ public class JSONArray {
* @return this.
*/
public JSONArray put(int value) {
put(new Integer(value));
this.put(new Integer(value));
return this;
}
@ -617,7 +618,7 @@ public class JSONArray {
* @return this.
*/
public JSONArray put(long value) {
put(new Long(value));
this.put(new Long(value));
return this;
}
@ -629,7 +630,7 @@ public class JSONArray {
* @return this.
*/
public JSONArray put(Map value) {
put(new JSONObject(value));
this.put(new JSONObject(value));
return this;
}
@ -657,7 +658,7 @@ public class JSONArray {
* @throws JSONException If the index is negative.
*/
public JSONArray put(int index, boolean value) throws JSONException {
put(index, value ? Boolean.TRUE : Boolean.FALSE);
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
return this;
}
@ -672,7 +673,7 @@ public class JSONArray {
* not finite.
*/
public JSONArray put(int index, Collection value) throws JSONException {
put(index, new JSONArray(value));
this.put(index, new JSONArray(value));
return this;
}
@ -688,7 +689,7 @@ public class JSONArray {
* not finite.
*/
public JSONArray put(int index, double value) throws JSONException {
put(index, new Double(value));
this.put(index, new Double(value));
return this;
}
@ -703,7 +704,7 @@ public class JSONArray {
* @throws JSONException If the index is negative.
*/
public JSONArray put(int index, int value) throws JSONException {
put(index, new Integer(value));
this.put(index, new Integer(value));
return this;
}
@ -718,7 +719,7 @@ public class JSONArray {
* @throws JSONException If the index is negative.
*/
public JSONArray put(int index, long value) throws JSONException {
put(index, new Long(value));
this.put(index, new Long(value));
return this;
}
@ -733,7 +734,7 @@ public class JSONArray {
* an invalid number.
*/
public JSONArray put(int index, Map value) throws JSONException {
put(index, new JSONObject(value));
this.put(index, new JSONObject(value));
return this;
}
@ -755,18 +756,18 @@ public class JSONArray {
if (index < 0) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
if (index < length()) {
if (index < this.length()) {
this.myArrayList.set(index, value);
} else {
while (index != length()) {
put(JSONObject.NULL);
while (index != this.length()) {
this.put(JSONObject.NULL);
}
put(value);
this.put(value);
}
return this;
}
/**
* Remove an index and close the hole.
* @param index The index of the element to be removed.
@ -774,7 +775,7 @@ public class JSONArray {
* or null if there was no value.
*/
public Object remove(int index) {
Object o = opt(index);
Object o = this.opt(index);
this.myArrayList.remove(index);
return o;
}
@ -790,7 +791,7 @@ public class JSONArray {
* @throws JSONException If any of the names are null.
*/
public JSONObject toJSONObject(JSONArray names) throws JSONException {
if (names == null || names.length() == 0 || length() == 0) {
if (names == null || names.length() == 0 || this.length() == 0) {
return null;
}
JSONObject jo = new JSONObject();
@ -814,7 +815,7 @@ public class JSONArray {
*/
public String toString() {
try {
return '[' + join(",") + ']';
return '[' + this.join(",") + ']';
} catch (Exception e) {
return null;
}
@ -833,7 +834,7 @@ public class JSONArray {
* @throws JSONException
*/
public String toString(int indentFactor) throws JSONException {
return toString(indentFactor, 0);
return this.toString(indentFactor, 0);
}
@ -848,7 +849,7 @@ public class JSONArray {
* @throws JSONException
*/
String toString(int indentFactor, int indent) throws JSONException {
int len = length();
int len = this.length();
if (len == 0) {
return "[]";
}
@ -892,7 +893,7 @@ public class JSONArray {
public Writer write(Writer writer) throws JSONException {
try {
boolean b = false;
int len = length();
int len = this.length();
writer.write('[');