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

View file

@ -83,10 +83,9 @@ import java.util.ResourceBundle;
* by <code>:</code>.</li> * by <code>:</code>.</li>
* <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as * <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as
* well as by <code>,</code> <small>(comma)</small>.</li> * well as by <code>,</code> <small>(comma)</small>.</li>
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
* </ul> * </ul>
* @author JSON.org * @author JSON.org
* @version 2011-10-16 * @version 2011-11-24
*/ */
public class JSONObject { public class JSONObject {
@ -129,7 +128,7 @@ public class JSONObject {
/** /**
* The map where the JSONObject's properties are kept. * The map where the JSONObject's properties are kept.
*/ */
private Map map; private final Map map;
/** /**
@ -162,7 +161,7 @@ public class JSONObject {
this(); this();
for (int i = 0; i < names.length; i += 1) { for (int i = 0; i < names.length; i += 1) {
try { try {
putOnce(names[i], jo.opt(names[i])); this.putOnce(names[i], jo.opt(names[i]));
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
@ -205,7 +204,7 @@ public class JSONObject {
} else if (c != ':') { } else if (c != ':') {
throw x.syntaxError("Expected a ':' after a key"); throw x.syntaxError("Expected a ':' after a key");
} }
putOnce(key, x.nextValue()); this.putOnce(key, x.nextValue());
// Pairs are separated by ','. We will also tolerate ';'. // Pairs are separated by ','. We will also tolerate ';'.
@ -269,7 +268,7 @@ public class JSONObject {
*/ */
public JSONObject(Object bean) { public JSONObject(Object bean) {
this(); this();
populateMap(bean); this.populateMap(bean);
} }
@ -290,7 +289,7 @@ public class JSONObject {
for (int i = 0; i < names.length; i += 1) { for (int i = 0; i < names.length; i += 1) {
String name = names[i]; String name = names[i];
try { try {
putOpt(name, c.getField(name).get(object)); this.putOpt(name, c.getField(name).get(object));
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
@ -372,14 +371,15 @@ public class JSONObject {
Object value Object value
) throws JSONException { ) throws JSONException {
testValidity(value); testValidity(value);
Object object = opt(key); Object object = this.opt(key);
if (object == null) { if (object == null) {
put(key, value instanceof JSONArray ? this.put(key, value instanceof JSONArray
new JSONArray().put(value) : value); ? new JSONArray().put(value)
: value);
} else if (object instanceof JSONArray) { } else if (object instanceof JSONArray) {
((JSONArray)object).put(value); ((JSONArray)object).put(value);
} else { } else {
put(key, new JSONArray().put(object).put(value)); this.put(key, new JSONArray().put(object).put(value));
} }
return this; return this;
} }
@ -398,11 +398,11 @@ public class JSONObject {
*/ */
public JSONObject append(String key, Object value) throws JSONException { public JSONObject append(String key, Object value) throws JSONException {
testValidity(value); testValidity(value);
Object object = opt(key); Object object = this.opt(key);
if (object == null) { if (object == null) {
put(key, new JSONArray().put(value)); this.put(key, new JSONArray().put(value));
} else if (object instanceof JSONArray) { } else if (object instanceof JSONArray) {
put(key, ((JSONArray)object).put(value)); this.put(key, ((JSONArray)object).put(value));
} else { } else {
throw new JSONException("JSONObject[" + key + throw new JSONException("JSONObject[" + key +
"] is not a JSONArray."); "] is not a JSONArray.");
@ -449,7 +449,7 @@ public class JSONObject {
if (key == null) { if (key == null) {
throw new JSONException("Null key."); throw new JSONException("Null key.");
} }
Object object = opt(key); Object object = this.opt(key);
if (object == null) { if (object == null) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] not found."); "] not found.");
@ -467,7 +467,7 @@ public class JSONObject {
* if the value is not a Boolean or the String "true" or "false". * if the value is not a Boolean or the String "true" or "false".
*/ */
public boolean getBoolean(String key) throws JSONException { public boolean getBoolean(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
if (object.equals(Boolean.FALSE) || if (object.equals(Boolean.FALSE) ||
(object instanceof String && (object instanceof String &&
((String)object).equalsIgnoreCase("false"))) { ((String)object).equalsIgnoreCase("false"))) {
@ -490,11 +490,11 @@ public class JSONObject {
* if the value is not a Number object and cannot be converted to a number. * if the value is not a Number object and cannot be converted to a number.
*/ */
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? return object instanceof Number
((Number)object).doubleValue() : ? ((Number)object).doubleValue()
Double.parseDouble((String)object); : Double.parseDouble((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not a number."); "] is not a number.");
@ -511,11 +511,11 @@ public class JSONObject {
* be converted to an integer. * be converted to an integer.
*/ */
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? return object instanceof Number
((Number)object).intValue() : ? ((Number)object).intValue()
Integer.parseInt((String)object); : Integer.parseInt((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not an int."); "] is not an int.");
@ -532,7 +532,7 @@ public class JSONObject {
* if the value is not a JSONArray. * if the value is not a JSONArray.
*/ */
public JSONArray getJSONArray(String key) throws JSONException { public JSONArray getJSONArray(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray)object; return (JSONArray)object;
} }
@ -550,7 +550,7 @@ public class JSONObject {
* if the value is not a JSONObject. * if the value is not a JSONObject.
*/ */
public JSONObject getJSONObject(String key) throws JSONException { public JSONObject getJSONObject(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
if (object instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject)object; return (JSONObject)object;
} }
@ -568,11 +568,11 @@ public class JSONObject {
* be converted to a long. * be converted to a long.
*/ */
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
try { try {
return object instanceof Number ? return object instanceof Number
((Number)object).longValue() : ? ((Number)object).longValue()
Long.parseLong((String)object); : Long.parseLong((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not a long."); "] is not a long.");
@ -632,7 +632,7 @@ public class JSONObject {
* @throws JSONException if there is no string value for the key. * @throws JSONException if there is no string value for the key.
*/ */
public String getString(String key) throws JSONException { public String getString(String key) throws JSONException {
Object object = get(key); Object object = this.get(key);
if (object instanceof String) { if (object instanceof String) {
return (String)object; return (String)object;
} }
@ -661,17 +661,17 @@ public class JSONObject {
* that is not an Integer, Long, Double, or Float. * that is not an Integer, Long, Double, or Float.
*/ */
public JSONObject increment(String key) throws JSONException { public JSONObject increment(String key) throws JSONException {
Object value = opt(key); Object value = this.opt(key);
if (value == null) { if (value == null) {
put(key, 1); this.put(key, 1);
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
put(key, ((Integer)value).intValue() + 1); this.put(key, ((Integer)value).intValue() + 1);
} else if (value instanceof Long) { } else if (value instanceof Long) {
put(key, ((Long)value).longValue() + 1); this.put(key, ((Long)value).longValue() + 1);
} else if (value instanceof Double) { } else if (value instanceof Double) {
put(key, ((Double)value).doubleValue() + 1); this.put(key, ((Double)value).doubleValue() + 1);
} else if (value instanceof Float) { } else if (value instanceof Float) {
put(key, ((Float)value).floatValue() + 1); this.put(key, ((Float)value).floatValue() + 1);
} else { } else {
throw new JSONException("Unable to increment [" + quote(key) + "]."); throw new JSONException("Unable to increment [" + quote(key) + "].");
} }
@ -687,7 +687,7 @@ public class JSONObject {
* the value is the JSONObject.NULL object. * the value is the JSONObject.NULL object.
*/ */
public boolean isNull(String key) { public boolean isNull(String key) {
return JSONObject.NULL.equals(opt(key)); return JSONObject.NULL.equals(this.opt(key));
} }
@ -774,7 +774,7 @@ public class JSONObject {
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(String key) { public boolean optBoolean(String key) {
return optBoolean(key, false); return this.optBoolean(key, false);
} }
@ -789,7 +789,7 @@ public class JSONObject {
*/ */
public boolean optBoolean(String key, boolean defaultValue) { public boolean optBoolean(String key, boolean defaultValue) {
try { try {
return getBoolean(key); return this.getBoolean(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -806,7 +806,7 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public double optDouble(String key) { public double optDouble(String key) {
return optDouble(key, Double.NaN); return this.optDouble(key, Double.NaN);
} }
@ -822,7 +822,7 @@ public class JSONObject {
*/ */
public double optDouble(String key, double defaultValue) { public double optDouble(String key, double defaultValue) {
try { try {
return getDouble(key); return this.getDouble(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -839,7 +839,7 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public int optInt(String key) { public int optInt(String key) {
return optInt(key, 0); return this.optInt(key, 0);
} }
@ -855,7 +855,7 @@ public class JSONObject {
*/ */
public int optInt(String key, int defaultValue) { public int optInt(String key, int defaultValue) {
try { try {
return getInt(key); return this.getInt(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -871,7 +871,7 @@ public class JSONObject {
* @return A JSONArray which is the value. * @return A JSONArray which is the value.
*/ */
public JSONArray optJSONArray(String key) { public JSONArray optJSONArray(String key) {
Object o = opt(key); Object o = this.opt(key);
return o instanceof JSONArray ? (JSONArray)o : null; return o instanceof JSONArray ? (JSONArray)o : null;
} }
@ -885,7 +885,7 @@ public class JSONObject {
* @return A JSONObject which is the value. * @return A JSONObject which is the value.
*/ */
public JSONObject optJSONObject(String key) { public JSONObject optJSONObject(String key) {
Object object = opt(key); Object object = this.opt(key);
return object instanceof JSONObject ? (JSONObject)object : null; return object instanceof JSONObject ? (JSONObject)object : null;
} }
@ -900,7 +900,7 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public long optLong(String key) { public long optLong(String key) {
return optLong(key, 0); return this.optLong(key, 0);
} }
@ -916,7 +916,7 @@ public class JSONObject {
*/ */
public long optLong(String key, long defaultValue) { public long optLong(String key, long defaultValue) {
try { try {
return getLong(key); return this.getLong(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -932,7 +932,7 @@ public class JSONObject {
* @return A string which is the value. * @return A string which is the value.
*/ */
public String optString(String key) { public String optString(String key) {
return optString(key, ""); return this.optString(key, "");
} }
@ -945,7 +945,7 @@ public class JSONObject {
* @return A string which is the value. * @return A string which is the value.
*/ */
public String optString(String key, String defaultValue) { public String optString(String key, String defaultValue) {
Object object = opt(key); Object object = this.opt(key);
return NULL.equals(object) ? defaultValue : object.toString(); return NULL.equals(object) ? defaultValue : object.toString();
} }
@ -957,8 +957,9 @@ public class JSONObject {
boolean includeSuperClass = klass.getClassLoader() != null; boolean includeSuperClass = klass.getClassLoader() != null;
Method[] methods = (includeSuperClass) ? Method[] methods = includeSuperClass
klass.getMethods() : klass.getDeclaredMethods(); ? klass.getMethods()
: klass.getDeclaredMethods();
for (int i = 0; i < methods.length; i += 1) { for (int i = 0; i < methods.length; i += 1) {
try { try {
Method method = methods[i]; Method method = methods[i];
@ -987,7 +988,7 @@ public class JSONObject {
Object result = method.invoke(bean, (Object[])null); Object result = method.invoke(bean, (Object[])null);
if (result != null) { if (result != null) {
map.put(key, wrap(result)); this.map.put(key, wrap(result));
} }
} }
} }
@ -1006,7 +1007,7 @@ public class JSONObject {
* @throws JSONException If the key is null. * @throws JSONException If the key is null.
*/ */
public JSONObject put(String key, boolean value) throws JSONException { public JSONObject put(String key, boolean value) throws JSONException {
put(key, value ? Boolean.TRUE : Boolean.FALSE); this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
return this; return this;
} }
@ -1020,7 +1021,7 @@ public class JSONObject {
* @throws JSONException * @throws JSONException
*/ */
public JSONObject put(String key, Collection value) throws JSONException { public JSONObject put(String key, Collection value) throws JSONException {
put(key, new JSONArray(value)); this.put(key, new JSONArray(value));
return this; return this;
} }
@ -1034,7 +1035,7 @@ public class JSONObject {
* @throws JSONException If the key is null or if the number is invalid. * @throws JSONException 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 {
put(key, new Double(value)); this.put(key, new Double(value));
return this; return this;
} }
@ -1048,7 +1049,7 @@ public class JSONObject {
* @throws JSONException If the key is null. * @throws JSONException If the key is null.
*/ */
public JSONObject put(String key, int value) throws JSONException { public JSONObject put(String key, int value) throws JSONException {
put(key, new Integer(value)); this.put(key, new Integer(value));
return this; return this;
} }
@ -1062,7 +1063,7 @@ public class JSONObject {
* @throws JSONException If the key is null. * @throws JSONException If the key is null.
*/ */
public JSONObject put(String key, long value) throws JSONException { public JSONObject put(String key, long value) throws JSONException {
put(key, new Long(value)); this.put(key, new Long(value));
return this; return this;
} }
@ -1076,7 +1077,7 @@ public class JSONObject {
* @throws JSONException * @throws JSONException
*/ */
public JSONObject put(String key, Map value) throws JSONException { public JSONObject put(String key, Map value) throws JSONException {
put(key, new JSONObject(value)); this.put(key, new JSONObject(value));
return this; return this;
} }
@ -1100,7 +1101,7 @@ public class JSONObject {
testValidity(value); testValidity(value);
this.map.put(key, value); this.map.put(key, value);
} else { } else {
remove(key); this.remove(key);
} }
return this; return this;
} }
@ -1117,10 +1118,10 @@ public class JSONObject {
*/ */
public JSONObject putOnce(String key, Object value) throws JSONException { public JSONObject putOnce(String key, Object value) throws JSONException {
if (key != null && value != null) { if (key != null && value != null) {
if (opt(key) != null) { if (this.opt(key) != null) {
throw new JSONException("Duplicate key \"" + key + "\""); throw new JSONException("Duplicate key \"" + key + "\"");
} }
put(key, value); this.put(key, value);
} }
return this; return this;
} }
@ -1138,7 +1139,7 @@ public class JSONObject {
*/ */
public JSONObject putOpt(String key, Object value) throws JSONException { public JSONObject putOpt(String key, Object value) throws JSONException {
if (key != null && value != null) { if (key != null && value != null) {
put(key, value); this.put(key, value);
} }
return this; return this;
} }
@ -1242,22 +1243,14 @@ public class JSONObject {
/* /*
* If it might be a number, try converting it. * If it might be a number, try converting it.
* We support the non-standard 0x- convention.
* If a number cannot be produced, then the value will just * If a number cannot be produced, then the value will just
* be a string. Note that the 0x-, plus, and implied string * be a string. Note that the plus and implied string
* conventions are non-standard. A JSON parser may accept * conventions are non-standard. A JSON parser may accept
* non-JSON forms as long as it accepts all correct JSON forms. * non-JSON forms as long as it accepts all correct JSON forms.
*/ */
char b = string.charAt(0); char b = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') { if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
if (b == '0' && string.length() > 2 &&
(string.charAt(1) == 'x' || string.charAt(1) == 'X')) {
try {
return new Integer(Integer.parseInt(string.substring(2), 16));
} catch (Exception ignore) {
}
}
try { try {
if (string.indexOf('.') > -1 || if (string.indexOf('.') > -1 ||
string.indexOf('e') > -1 || string.indexOf('E') > -1) { string.indexOf('e') > -1 || string.indexOf('E') > -1) {
@ -1368,7 +1361,7 @@ public class JSONObject {
* @throws JSONException If the object contains an invalid number. * @throws JSONException If the object contains an invalid number.
*/ */
public String toString(int indentFactor) throws JSONException { public String toString(int indentFactor) throws JSONException {
return toString(indentFactor, 0); return this.toString(indentFactor, 0);
} }
@ -1579,8 +1572,9 @@ public class JSONObject {
return new JSONObject((Map)object); return new JSONObject((Map)object);
} }
Package objectPackage = object.getClass().getPackage(); Package objectPackage = object.getClass().getPackage();
String objectPackageName = objectPackage != null ? String objectPackageName = objectPackage != null
objectPackage.getName() : ""; ? objectPackage.getName()
: "";
if ( if (
objectPackageName.startsWith("java.") || objectPackageName.startsWith("java.") ||
objectPackageName.startsWith("javax.") || objectPackageName.startsWith("javax.") ||

View file

@ -496,14 +496,14 @@ public class Test extends TestCase {
XML.toString(jsonarray)); XML.toString(jsonarray));
jsonobject = new JSONObject("{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=> o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}"); jsonobject = new JSONObject("{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=> o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
assertEquals("{\n \"noh\": \"0x0x\",\n \"one\": [[1]],\n \"o\": 999,\n \"+\": 6.0E66,\n \"true\": true,\n \"forgiving\": \"This package can be used to parse formats that are similar to but not stricting conforming to JSON\",\n \"fun\": \"with non-standard forms\",\n \"double\": 0.666,\n \"uno\": [[{\"1\": 1}]],\n \"dec\": 666,\n \"oct\": 666,\n \"hex\": 1638,\n \"string\": \"o. k.\",\n \"empty\": \"\",\n \"false\": false,\n \"[true]\": [[\n \"!\",\n \"@\",\n \"*\"\n ]],\n \"pluses\": \"+++\",\n \"why\": \"To make it easier to migrate existing data to JSON\",\n \"null\": null\n}", jsonobject.toString(1)); assertEquals("{\n \"noh\": \"0x0x\",\n \"one\": [[1]],\n \"o\": 999,\n \"+\": 6.0E66,\n \"true\": true,\n \"forgiving\": \"This package can be used to parse formats that are similar to but not stricting conforming to JSON\",\n \"fun\": \"with non-standard forms\",\n \"double\": 0.666,\n \"uno\": [[{\"1\": 1}]],\n \"dec\": 666,\n \"oct\": 666,\n \"hex\": \"0x666\",\n \"string\": \"o. k.\",\n \"empty\": \"\",\n \"false\": false,\n \"[true]\": [[\n \"!\",\n \"@\",\n \"*\"\n ]],\n \"pluses\": \"+++\",\n \"why\": \"To make it easier to migrate existing data to JSON\",\n \"null\": null\n}", jsonobject.toString(1));
assertTrue(jsonobject.getBoolean("true")); assertTrue(jsonobject.getBoolean("true"));
assertFalse(jsonobject.getBoolean("false")); assertFalse(jsonobject.getBoolean("false"));
jsonobject = new JSONObject(jsonobject, new String[]{"dec", "oct", "hex", "missing"}); jsonobject = new JSONObject(jsonobject, new String[]{"dec", "oct", "hex", "missing"});
assertEquals("{\n \"oct\": 666,\n \"dec\": 666,\n \"hex\": 1638\n}", jsonobject.toString(1)); assertEquals("{\n \"oct\": 666,\n \"dec\": 666,\n \"hex\": \"0x666\"\n}", jsonobject.toString(1));
assertEquals("[[\"<escape>\",\"next is an implied null\",null,\"ok\"],{\"oct\":666,\"dec\":666,\"hex\":1638}]", assertEquals("[[\"<escape>\",\"next is an implied null\",null,\"ok\"],{\"oct\":666,\"dec\":666,\"hex\":\"0x666\"}]",
new JSONStringer().array().value(jsonarray).value(jsonobject).endArray().toString()); new JSONStringer().array().value(jsonarray).value(jsonobject).endArray().toString());
jsonobject = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}"); jsonobject = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");