mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Revert "reduces the use of unnecessary exceptions"
This commit is contained in:
parent
8e079599c4
commit
45a7decba4
2 changed files with 58 additions and 170 deletions
101
JSONArray.java
101
JSONArray.java
|
@ -78,7 +78,7 @@ import java.util.Map;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-07-19
|
||||
* @version 2016-05-20
|
||||
*/
|
||||
public class JSONArray implements Iterable<Object> {
|
||||
|
||||
|
@ -241,16 +241,12 @@ public class JSONArray implements Iterable<Object> {
|
|||
public double getDouble(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return 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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return 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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSONArray associated with an index.
|
||||
|
@ -389,16 +381,12 @@ public class JSONArray implements Iterable<Object> {
|
|||
public long getLong(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return 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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string associated with an index.
|
||||
|
@ -498,20 +486,11 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(int index, boolean defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
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.
|
||||
*/
|
||||
public double optDouble(int index, double defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public int optInt(int index, int defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
return this.getInt(index);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the enum value associated with a key.
|
||||
|
@ -654,12 +615,8 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return new BigInteger(object.toString());
|
||||
return this.getBigInteger(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -677,12 +634,8 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return new BigDecimal(object.toString());
|
||||
return this.getBigDecimal(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -740,21 +693,12 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return The value.
|
||||
*/
|
||||
public long optLong(int index, long defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
if (JSONObject.NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* JSONArray initialized with this document:
|
||||
* <pre>
|
||||
|
@ -1137,7 +1081,6 @@ public class JSONArray implements Iterable<Object> {
|
|||
* @return a printable, displayable, transmittable representation of the
|
||||
* array.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
|
|
105
JSONObject.java
105
JSONObject.java
|
@ -93,7 +93,7 @@ import java.util.Set;
|
|||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2016-07-19
|
||||
* @version 2016-05-20
|
||||
*/
|
||||
public class JSONObject {
|
||||
/**
|
||||
|
@ -132,7 +132,6 @@ public class JSONObject {
|
|||
*
|
||||
* @return The string "null".
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "null";
|
||||
}
|
||||
|
@ -577,16 +576,13 @@ public class JSONObject {
|
|||
public double getDouble(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
return object instanceof Number ? ((Number) object).doubleValue()
|
||||
: Double.parseDouble((String) object);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new JSONException("JSONObject[" + quote(key)
|
||||
+ "] is not a number.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the int value associated with a key.
|
||||
|
@ -601,15 +597,12 @@ public class JSONObject {
|
|||
public int getInt(String key) throws JSONException {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
return object instanceof Number ? ((Number) object).intValue()
|
||||
: Integer.parseInt((String) object);
|
||||
} 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 {
|
||||
Object object = this.get(key);
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
return object instanceof Number ? ((Number) object).longValue()
|
||||
: Long.parseLong((String) object);
|
||||
} 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.
|
||||
*/
|
||||
public boolean optBoolean(String key, boolean defaultValue) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
try {
|
||||
return this.getBoolean(key);
|
||||
} catch (Exception e) {
|
||||
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.
|
||||
*/
|
||||
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return new BigInteger(object.toString());
|
||||
return this.getBigInteger(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -1006,12 +983,8 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
|
||||
Object object = this.opt(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return new BigDecimal(object.toString());
|
||||
return this.getBigDecimal(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -1029,21 +1002,12 @@ public class JSONObject {
|
|||
* @return An object which is the value.
|
||||
*/
|
||||
public double optDouble(String key, double defaultValue) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).doubleValue();
|
||||
} else if (object instanceof String) {
|
||||
return Double.parseDouble((String) object);
|
||||
}
|
||||
return this.getDouble(key);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public int optInt(String key, int defaultValue) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).intValue();
|
||||
} else if (object instanceof String) {
|
||||
return Integer.parseInt((String) object);
|
||||
}
|
||||
return this.getInt(key);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public long optLong(String key, long defaultValue) {
|
||||
Object object = this.get(key);
|
||||
if (NULL.equals(object)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
if (object instanceof Number) {
|
||||
return ((Number) object).longValue();
|
||||
} else if (object instanceof String) {
|
||||
return Long.parseLong((String) object);
|
||||
}
|
||||
return this.getLong(key);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> <small>(right
|
||||
* brace)</small>.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
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
|
||||
* be converted.
|
||||
* <p>
|
||||
* 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() {
|
||||
Map<String, Object> results = new HashMap<String, Object>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue