mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
removes duplicate code in number getters
This commit is contained in:
parent
71c6dd1e34
commit
34cfe6df14
2 changed files with 72 additions and 187 deletions
122
JSONArray.java
122
JSONArray.java
|
@ -263,13 +263,7 @@ public class JSONArray implements Iterable<Object> {
|
||||||
* to a number.
|
* to a number.
|
||||||
*/
|
*/
|
||||||
public double getDouble(int index) throws JSONException {
|
public double getDouble(int index) throws JSONException {
|
||||||
Object object = this.get(index);
|
return this.getNumber(index).doubleValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).doubleValue()
|
|
||||||
: Double.parseDouble((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -283,14 +277,7 @@ public class JSONArray implements Iterable<Object> {
|
||||||
* object and cannot be converted to a number.
|
* object and cannot be converted to a number.
|
||||||
*/
|
*/
|
||||||
public float getFloat(int index) throws JSONException {
|
public float getFloat(int index) throws JSONException {
|
||||||
Object object = this.get(index);
|
return this.getNumber(index).floatValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).floatValue()
|
|
||||||
: Float.parseFloat(object.toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index
|
|
||||||
+ "] is not a number.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -394,13 +381,7 @@ public class JSONArray implements Iterable<Object> {
|
||||||
* If the key is not found or if the value is not a number.
|
* 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 = this.get(index);
|
return this.getNumber(index).intValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).intValue()
|
|
||||||
: Integer.parseInt((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -450,13 +431,7 @@ public class JSONArray implements Iterable<Object> {
|
||||||
* to a number.
|
* to a number.
|
||||||
*/
|
*/
|
||||||
public long getLong(int index) throws JSONException {
|
public long getLong(int index) throws JSONException {
|
||||||
Object object = this.get(index);
|
return this.getNumber(index).longValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).longValue()
|
|
||||||
: Long.parseLong((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -500,13 +475,16 @@ public class JSONArray implements Iterable<Object> {
|
||||||
*/
|
*/
|
||||||
public String join(String separator) throws JSONException {
|
public String join(String separator) throws JSONException {
|
||||||
int len = this.length();
|
int len = this.length();
|
||||||
StringBuilder sb = new StringBuilder();
|
if (len == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < len; i += 1) {
|
StringBuilder sb = new StringBuilder(
|
||||||
if (i > 0) {
|
JSONObject.valueToString(this.myArrayList.get(0)));
|
||||||
sb.append(separator);
|
|
||||||
}
|
for (int i = 1; i < len; i++) {
|
||||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
sb.append(separator)
|
||||||
|
.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
@ -589,21 +567,15 @@ 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 val = this.opt(index);
|
final Number val = this.optNumber(index, null);
|
||||||
if (JSONObject.NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
final double doubleValue = val.doubleValue();
|
||||||
return ((Number) val).doubleValue();
|
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
|
||||||
}
|
// return defaultValue;
|
||||||
if (val instanceof String) {
|
// }
|
||||||
try {
|
return doubleValue;
|
||||||
return Double.parseDouble((String) val);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -631,21 +603,15 @@ public class JSONArray implements Iterable<Object> {
|
||||||
* @return The value.
|
* @return The value.
|
||||||
*/
|
*/
|
||||||
public float optFloat(int index, float defaultValue) {
|
public float optFloat(int index, float defaultValue) {
|
||||||
Object val = this.opt(index);
|
final Number val = this.optNumber(index, null);
|
||||||
if (JSONObject.NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
final float floatValue = val.floatValue();
|
||||||
return ((Number) val).floatValue();
|
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
|
||||||
}
|
// return floatValue;
|
||||||
if (val instanceof String) {
|
// }
|
||||||
try {
|
return floatValue;
|
||||||
return Float.parseFloat((String) val);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -673,22 +639,11 @@ 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 val = this.opt(index);
|
final Number val = this.optNumber(index, null);
|
||||||
if (JSONObject.NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
return val.intValue();
|
||||||
return ((Number) val).intValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val instanceof String) {
|
|
||||||
try {
|
|
||||||
return new BigDecimal(val.toString()).intValue();
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -827,22 +782,11 @@ 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 val = this.opt(index);
|
final Number val = this.optNumber(index, null);
|
||||||
if (JSONObject.NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
return val.longValue();
|
||||||
return ((Number) val).longValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val instanceof String) {
|
|
||||||
try {
|
|
||||||
return new BigDecimal(val.toString()).longValue();
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
137
JSONObject.java
137
JSONObject.java
|
@ -681,14 +681,7 @@ public class JSONObject {
|
||||||
* object and cannot be converted to 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 = this.get(key);
|
return this.getNumber(key).doubleValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).doubleValue()
|
|
||||||
: Double.parseDouble(object.toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONObject[" + quote(key)
|
|
||||||
+ "] is not a number.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -702,14 +695,7 @@ public class JSONObject {
|
||||||
* object and cannot be converted to a number.
|
* object and cannot be converted to a number.
|
||||||
*/
|
*/
|
||||||
public float getFloat(String key) throws JSONException {
|
public float getFloat(String key) throws JSONException {
|
||||||
Object object = this.get(key);
|
return this.getNumber(key).floatValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).floatValue()
|
|
||||||
: Float.parseFloat(object.toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONObject[" + quote(key)
|
|
||||||
+ "] is not a number.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -746,14 +732,7 @@ public class JSONObject {
|
||||||
* to an integer.
|
* to an integer.
|
||||||
*/
|
*/
|
||||||
public int getInt(String key) throws JSONException {
|
public int getInt(String key) throws JSONException {
|
||||||
Object object = this.get(key);
|
return this.getNumber(key).intValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).intValue()
|
|
||||||
: Integer.parseInt((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONObject[" + quote(key)
|
|
||||||
+ "] is not an int.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -803,14 +782,7 @@ public class JSONObject {
|
||||||
* to a long.
|
* to a long.
|
||||||
*/
|
*/
|
||||||
public long getLong(String key) throws JSONException {
|
public long getLong(String key) throws JSONException {
|
||||||
Object object = this.get(key);
|
return this.getNumber(key).longValue();
|
||||||
try {
|
|
||||||
return object instanceof Number ? ((Number) object).longValue()
|
|
||||||
: Long.parseLong((String) object);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("JSONObject[" + quote(key)
|
|
||||||
+ "] is not a long.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1266,21 +1238,15 @@ 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 val = this.opt(key);
|
Number val = this.optNumber(key);
|
||||||
if (NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
final double doubleValue = val.doubleValue();
|
||||||
return ((Number) val).doubleValue();
|
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
|
||||||
}
|
// return defaultValue;
|
||||||
if (val instanceof String) {
|
// }
|
||||||
try {
|
return doubleValue;
|
||||||
return Double.parseDouble((String) val);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1308,21 +1274,15 @@ public class JSONObject {
|
||||||
* @return The value.
|
* @return The value.
|
||||||
*/
|
*/
|
||||||
public float optFloat(String key, float defaultValue) {
|
public float optFloat(String key, float defaultValue) {
|
||||||
Object val = this.opt(key);
|
Number val = this.optNumber(key);
|
||||||
if (JSONObject.NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
final float floatValue = val.floatValue();
|
||||||
return ((Number) val).floatValue();
|
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
|
||||||
}
|
// return defaultValue;
|
||||||
if (val instanceof String) {
|
// }
|
||||||
try {
|
return floatValue;
|
||||||
return Float.parseFloat((String) val);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1350,22 +1310,11 @@ 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 val = this.opt(key);
|
final Number val = this.optNumber(key, null);
|
||||||
if (NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
return val.intValue();
|
||||||
return ((Number) val).intValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val instanceof String) {
|
|
||||||
try {
|
|
||||||
return new BigDecimal((String) val).intValue();
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1419,22 +1368,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 val = this.opt(key);
|
final Number val = this.optNumber(key, null);
|
||||||
if (NULL.equals(val)) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (val instanceof Number){
|
|
||||||
return ((Number) val).longValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val instanceof String) {
|
return val.longValue();
|
||||||
try {
|
|
||||||
return new BigDecimal((String) val).longValue();
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1472,14 +1411,11 @@ public class JSONObject {
|
||||||
return (Number) val;
|
return (Number) val;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (val instanceof String) {
|
try {
|
||||||
try {
|
return stringToNumber(val.toString());
|
||||||
return stringToNumber((String) val);
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
return defaultValue;
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2201,22 +2137,26 @@ public class JSONObject {
|
||||||
* can't be converted, return the string.
|
* can't be converted, return the string.
|
||||||
*
|
*
|
||||||
* @param string
|
* @param string
|
||||||
* A String.
|
* A String. can not be null.
|
||||||
* @return A simple JSON value.
|
* @return A simple JSON value.
|
||||||
|
* @throws NullPointerException
|
||||||
|
* Thrown if the string is null.
|
||||||
*/
|
*/
|
||||||
// Changes to this method must be copied to the corresponding method in
|
// Changes to this method must be copied to the corresponding method in
|
||||||
// the XML class to keep full support for Android
|
// the XML class to keep full support for Android
|
||||||
public static Object stringToValue(String string) {
|
public static Object stringToValue(String string) {
|
||||||
if (string.equals("")) {
|
if ("".equals(string)) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
if (string.equalsIgnoreCase("true")) {
|
|
||||||
|
// check JSON key words true/false/null
|
||||||
|
if ("true".equalsIgnoreCase(string)) {
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
}
|
}
|
||||||
if (string.equalsIgnoreCase("false")) {
|
if ("false".equalsIgnoreCase(string)) {
|
||||||
return Boolean.FALSE;
|
return Boolean.FALSE;
|
||||||
}
|
}
|
||||||
if (string.equalsIgnoreCase("null")) {
|
if ("null".equalsIgnoreCase(string)) {
|
||||||
return JSONObject.NULL;
|
return JSONObject.NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2228,7 +2168,8 @@ public class JSONObject {
|
||||||
char initial = string.charAt(0);
|
char initial = string.charAt(0);
|
||||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||||
try {
|
try {
|
||||||
// if we want full Big Number support this block can be replaced with:
|
// if we want full Big Number support the contents of this
|
||||||
|
// `try` block can be replaced with:
|
||||||
// return stringToNumber(string);
|
// return stringToNumber(string);
|
||||||
if (isDecimalNotation(string)) {
|
if (isDecimalNotation(string)) {
|
||||||
Double d = Double.valueOf(string);
|
Double d = Double.valueOf(string);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue