3 Commits

Author SHA1 Message Date
Sean Leary
37582a44ad Update README 2016-08-10 09:13:22 -05:00
Sean Leary
154cfda9aa Merge pull request #261 from stleary/revert-249-master
Revert "reduces the use of unnecessary exceptions"
2016-08-10 09:03:57 -05:00
Sean Leary
45a7decba4 Revert "reduces the use of unnecessary exceptions" 2016-08-09 14:22:06 -05:00
3 changed files with 63 additions and 171 deletions

View File

@@ -78,7 +78,7 @@ import java.util.Map;
* </ul> * </ul>
* *
* @author JSON.org * @author JSON.org
* @version 2016-07-19 * @version 2016-05-20
*/ */
public class JSONArray implements Iterable<Object> { public class JSONArray implements Iterable<Object> {
@@ -241,16 +241,12 @@ public class JSONArray implements Iterable<Object> {
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
if (object instanceof Number) { return object instanceof Number ? ((Number) object).doubleValue()
return ((Number) object).doubleValue(); : Double.parseDouble((String) object);
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
throw new JSONException("JSONArray[" + index + "] is not a number."); throw new JSONException("JSONArray[" + index + "] is not a number.");
} }
}
/** /**
* Get the enum value associated with an index. * Get the enum value associated with an index.
@@ -329,16 +325,12 @@ public class JSONArray implements Iterable<Object> {
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
if (object instanceof Number) { return object instanceof Number ? ((Number) object).intValue()
return ((Number) object).intValue(); : Integer.parseInt((String) object);
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
throw new JSONException("JSONArray[" + index + "] is not a number."); throw new JSONException("JSONArray[" + index + "] is not a number.");
} }
}
/** /**
* Get the JSONArray associated with an index. * Get the JSONArray associated with an index.
@@ -389,16 +381,12 @@ public class JSONArray implements Iterable<Object> {
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
Object object = this.get(index); Object object = this.get(index);
try { try {
if (object instanceof Number) { return object instanceof Number ? ((Number) object).longValue()
return ((Number) object).longValue(); : Long.parseLong((String) object);
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
throw new JSONException("JSONArray[" + index + "] is not a number."); throw new JSONException("JSONArray[" + index + "] is not a number.");
} }
}
/** /**
* Get the string associated with an index. * Get the string associated with an index.
@@ -498,20 +486,11 @@ public class JSONArray implements Iterable<Object> {
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(int index, boolean defaultValue) { public boolean optBoolean(int index, boolean defaultValue) {
Object object = this.opt(index); try {
if (JSONObject.NULL.equals(object)) { return this.getBoolean(index);
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
return defaultValue;
} }
/** /**
@@ -539,21 +518,12 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public double optDouble(int index, double defaultValue) { public double optDouble(int index, double defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
if (object instanceof Number) { return this.getDouble(index);
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
return defaultValue; return defaultValue;
} }
}
/** /**
* Get the optional int value associated with an index. Zero is returned if * Get the optional int value associated with an index. Zero is returned if
@@ -580,21 +550,12 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public int optInt(int index, int defaultValue) { public int optInt(int index, int defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
if (object instanceof Number) { return this.getInt(index);
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
return defaultValue; return defaultValue;
} }
}
/** /**
* Get the enum value associated with a key. * Get the enum value associated with a key.
@@ -654,12 +615,8 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public BigInteger optBigInteger(int index, BigInteger defaultValue) { public BigInteger optBigInteger(int index, BigInteger defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
return new BigInteger(object.toString()); return this.getBigInteger(index);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@@ -677,12 +634,8 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) { public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
return new BigDecimal(object.toString()); return this.getBigDecimal(index);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@@ -740,21 +693,12 @@ public class JSONArray implements Iterable<Object> {
* @return The value. * @return The value.
*/ */
public long optLong(int index, long defaultValue) { public long optLong(int index, long defaultValue) {
Object object = this.opt(index);
if (JSONObject.NULL.equals(object)) {
return defaultValue;
}
try { try {
if (object instanceof Number) { return this.getLong(index);
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
return defaultValue; return defaultValue;
} }
}
/** /**
* Get the optional string value associated with an index. It returns an * Get the optional string value associated with an index. It returns an
@@ -1017,7 +961,7 @@ public class JSONArray implements Iterable<Object> {
} }
/** /**
* Creates a JSONPointer using an initialization string and tries to * Creates a JSONPointer using an intialization string and tries to
* match it to an item within this JSONArray. For example, given a * match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document: * JSONArray initialized with this document:
* <pre> * <pre>
@@ -1137,7 +1081,6 @@ 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);

View File

@@ -93,7 +93,7 @@ import java.util.Set;
* </ul> * </ul>
* *
* @author JSON.org * @author JSON.org
* @version 2016-07-19 * @version 2016-05-20
*/ */
public class JSONObject { public class JSONObject {
/** /**
@@ -132,7 +132,6 @@ public class JSONObject {
* *
* @return The string "null". * @return The string "null".
*/ */
@Override
public String toString() { public String toString() {
return "null"; return "null";
} }
@@ -577,16 +576,13 @@ public class JSONObject {
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { try {
if (object instanceof Number) { return object instanceof Number ? ((Number) object).doubleValue()
return ((Number) object).doubleValue(); : Double.parseDouble((String) object);
} else if (object instanceof String) {
return 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.");
} }
}
/** /**
* Get the int value associated with a key. * Get the int value associated with a key.
@@ -601,15 +597,12 @@ public class JSONObject {
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { try {
if (object instanceof Number) { return object instanceof Number ? ((Number) object).intValue()
return ((Number) object).intValue(); : Integer.parseInt((String) object);
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not an int.");
} }
throw new JSONException("JSONObject[" + quote(key) + "] is not an int.");
} }
/** /**
@@ -661,15 +654,12 @@ public class JSONObject {
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
Object object = this.get(key); Object object = this.get(key);
try { try {
if (object instanceof Number) { return object instanceof Number ? ((Number) object).longValue()
return ((Number) object).longValue(); : Long.parseLong((String) object);
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key)
+ "] is not a long.");
} }
throw new JSONException("JSONObject[" + quote(key) + "] is not a long.");
} }
/** /**
@@ -942,20 +932,11 @@ public class JSONObject {
* @return The truth. * @return The truth.
*/ */
public boolean optBoolean(String key, boolean defaultValue) { public boolean optBoolean(String key, boolean defaultValue) {
Object object = this.get(key); try {
if (NULL.equals(object)) { return this.getBoolean(key);
} catch (Exception e) {
return defaultValue; return defaultValue;
} }
if (object.equals(Boolean.FALSE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("false"))) {
return false;
} else if (object.equals(Boolean.TRUE)
|| (object instanceof String && ((String) object)
.equalsIgnoreCase("true"))) {
return true;
}
return defaultValue;
} }
/** /**
@@ -983,12 +964,8 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public BigInteger optBigInteger(String key, BigInteger defaultValue) { public BigInteger optBigInteger(String key, BigInteger defaultValue) {
Object object = this.get(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
return new BigInteger(object.toString()); return this.getBigInteger(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@@ -1006,12 +983,8 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) { public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
Object object = this.opt(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
return new BigDecimal(object.toString()); return this.getBigDecimal(key);
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@@ -1029,21 +1002,12 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public double optDouble(String key, double defaultValue) { public double optDouble(String key, double defaultValue) {
Object object = this.get(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
if (object instanceof Number) { return this.getDouble(key);
return ((Number) object).doubleValue();
} else if (object instanceof String) {
return Double.parseDouble((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
return defaultValue; return defaultValue;
} }
}
/** /**
* Get an optional int value associated with a key, or zero if there is no * Get an optional int value associated with a key, or zero if there is no
@@ -1070,21 +1034,12 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public int optInt(String key, int defaultValue) { public int optInt(String key, int defaultValue) {
Object object = this.get(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
if (object instanceof Number) { return this.getInt(key);
return ((Number) object).intValue();
} else if (object instanceof String) {
return Integer.parseInt((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
return defaultValue; return defaultValue;
} }
}
/** /**
* Get an optional JSONArray associated with a key. It returns null if there * Get an optional JSONArray associated with a key. It returns null if there
@@ -1137,21 +1092,12 @@ public class JSONObject {
* @return An object which is the value. * @return An object which is the value.
*/ */
public long optLong(String key, long defaultValue) { public long optLong(String key, long defaultValue) {
Object object = this.get(key);
if (NULL.equals(object)) {
return defaultValue;
}
try { try {
if (object instanceof Number) { return this.getLong(key);
return ((Number) object).longValue();
} else if (object instanceof String) {
return Long.parseLong((String) object);
}
} catch (Exception e) { } catch (Exception e) {
}
return defaultValue; return defaultValue;
} }
}
/** /**
* Get an optional string associated with a key. It returns an empty string * Get an optional string associated with a key. It returns an empty string
@@ -1675,7 +1621,6 @@ public class JSONObject {
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right * brace)</small> and ending with <code>}</code>&nbsp;<small>(right
* brace)</small>. * brace)</small>.
*/ */
@Override
public String toString() { public String toString() {
try { try {
return this.toString(0); return this.toString(0);
@@ -1935,13 +1880,13 @@ public class JSONObject {
} }
/** /**
* Returns a java.util.Map containing all of the entries in this object. * Returns a java.util.Map containing all of the entrys in this object.
* If an entry in the object is a JSONArray or JSONObject it will also * If an entry in the object is a JSONArray or JSONObject it will also
* be converted. * be converted.
* <p> * <p>
* Warning: This method assumes that the data structure is acyclical. * Warning: This method assumes that the data structure is acyclical.
* *
* @return a java.util.Map containing the entries of this object * @return a java.util.Map containing the entrys of this object
*/ */
public Map<String, Object> toMap() { public Map<String, Object> toMap() {
Map<String, Object> results = new HashMap<String, Object>(); Map<String, Object> results = new HashMap<String, Object>();

6
README
View File

@@ -90,7 +90,11 @@ invalid number formats (1.2e6.3) will cause errors as such documents can not be
Release history: Release history:
20160807 Java 1.6 compatability fixed, JSONArray.toList() and JSONObject.toMap(), 20160810 Revert code that was breaking opt*() methods.
20160807 This release contains a bug in the JSONObject.opt*() and JSONArray.opt*() methods,
it is not recommended for use.
Java 1.6 compatability fixed, JSONArray.toList() and JSONObject.toMap(),
RFC4180 compatibility, JSONPointer, some exception fixes, optional XML type conversion. RFC4180 compatibility, JSONPointer, some exception fixes, optional XML type conversion.
Contains the latest code as of 7 Aug, 2016 Contains the latest code as of 7 Aug, 2016