Skip to content

Commit

Permalink
fixed problems caused by nonsense logic in opt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Lopes committed Mar 3, 2021
1 parent 1786f7a commit b955163
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ In project's pom.xml add in ```<dependencies>```:
<dependency>
<groupId>com.github.leonardofel</groupId>
<artifactId>json-java-put-null-fix</artifactId>
<version>3.0.30</version>
<version>3.0.31</version>
</dependency>
</dependencies>
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.github.leonardofel</groupId>
<artifactId>json-java-put-null-fix</artifactId>
<version>3.0.30</version>
<version>3.0.31</version>
<packaging>jar</packaging>

<name>JSON in Java WITH WORKING .put(null)</name>
Expand Down
99 changes: 49 additions & 50 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,11 @@ public double optDouble(int index) {
* @return The value.
*/
public double optDouble(int index, double defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
try {
return this.getDouble(index);
} catch (Exception ex) {
return defaultValue;
}
final double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
}

/**
Expand Down Expand Up @@ -672,15 +668,11 @@ public float optFloat(int index) {
* @return The value.
*/
public float optFloat(int index, float defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
try {
return this.getFloat(index);
} catch (Exception ex) {
return defaultValue;
}
final float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
}

/**
Expand Down Expand Up @@ -708,11 +700,11 @@ public int optInt(int index) {
* @return The value.
*/
public int optInt(int index, int defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
try {
return this.getInt(index);
} catch (Exception ex) {
return defaultValue;
}
return val.intValue();
}

/**
Expand Down Expand Up @@ -776,8 +768,11 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
* @return The value.
*/
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
Object val = this.opt(index);
return JSONObject.objectToBigInteger(val, defaultValue);
try {
return this.getBigInteger(index);
} catch (Exception ex) {
return defaultValue;
}
}

/**
Expand All @@ -795,8 +790,11 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
* @return The value.
*/
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
Object val = this.opt(index);
return JSONObject.objectToBigDecimal(val, defaultValue);
try {
return this.getBigDecimal(index);
} catch (Exception ex) {
return defaultValue;
}
}

/**
Expand Down Expand Up @@ -851,11 +849,11 @@ public long optLong(int index) {
* @return The value.
*/
public long optLong(int index, long defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
try {
return this.getLong(index);
} catch (Exception ex) {
return defaultValue;
}
return val.longValue();
}

/**
Expand Down Expand Up @@ -885,22 +883,11 @@ public Number optNumber(int index) {
* @return An object which is the value.
*/
public Number optNumber(int index, Number defaultValue) {
Object val = this.opt(index);
if (JSONObject.NULL.equals(val)) {
try {
return this.getNumber(index);
} catch (Exception ex) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}

if (val instanceof String) {
try {
return JSONObject.stringToNumber((String) val);
} catch (Exception e) {
return defaultValue;
}
}
return defaultValue;
}

/**
Expand Down Expand Up @@ -932,6 +919,30 @@ public String optString(int index, String defaultValue) {
.toString();
}

public Integer getInteger(int index) {
final Object object = this.get(index);
if (object instanceof Number) {
return ((Number) object).intValue();
}
try {
return Integer.parseInt(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(index, "integer", e);
}
}

public Integer optInteger(int index) {
return this.optInteger(index, null);
}

public Integer optInteger(int index, Integer defaultValue) {
try {
return this.getInteger(index);
} catch (Exception ex) {
return defaultValue;
}
}

/**
* Append a boolean value. This increases the array's length by one.
*
Expand Down Expand Up @@ -1700,16 +1711,4 @@ private static JSONException wrongValueFormatException(
"JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
, cause);
}

public Integer optInteger(int index) {
return this.optInteger(index, null);
}

public Integer optInteger(int index, Integer defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
}
83 changes: 48 additions & 35 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,8 +1140,11 @@ public boolean optBoolean(String key, boolean defaultValue) {
* @return An object which is the value.
*/
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
Object val = this.opt(key);
return objectToBigDecimal(val, defaultValue);
try {
return this.getBigDecimal(key);
} catch (Exception ex) {
return defaultValue;
}
}

/**
Expand Down Expand Up @@ -1191,8 +1194,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
* @return An object which is the value.
*/
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
Object val = this.opt(key);
return objectToBigInteger(val, defaultValue);
try {
return this.getBigInteger(key);
} catch (Exception ex) {
return defaultValue;
}
}

/**
Expand Down Expand Up @@ -1264,15 +1270,11 @@ public double optDouble(String key) {
* @return An object which is the value.
*/
public double optDouble(String key, double defaultValue) {
Number val = this.optNumber(key);
if (val == null) {
try {
return this.getDouble(key);
} catch (Exception ex) {
return defaultValue;
}
final double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
}

/**
Expand Down Expand Up @@ -1300,15 +1302,11 @@ public float optFloat(String key) {
* @return The value.
*/
public float optFloat(String key, float defaultValue) {
Number val = this.optNumber(key);
if (val == null) {
try {
return this.getFloat(key);
} catch (Exception ex) {
return defaultValue;
}
final float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return defaultValue;
// }
return floatValue;
}

/**
Expand Down Expand Up @@ -1336,11 +1334,11 @@ public int optInt(String key) {
* @return An object which is the value.
*/
public int optInt(String key, int defaultValue) {
final Number val = this.optNumber(key, null);
if (val == null) {
try {
return this.getInt(key);
} catch (Exception ex) {
return defaultValue;
}
return val.intValue();
}

/**
Expand Down Expand Up @@ -1394,12 +1392,11 @@ public long optLong(String key) {
* @return An object which is the value.
*/
public long optLong(String key, long defaultValue) {
final Number val = this.optNumber(key, null);
if (val == null) {
try {
return this.getLong(key);
} catch (Exception ex) {
return defaultValue;
}

return val.longValue();
}

/**
Expand Down Expand Up @@ -1429,17 +1426,9 @@ public Number optNumber(String key) {
* @return An object which is the value.
*/
public Number optNumber(String key, Number defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Number){
return (Number) val;
}

try {
return stringToNumber(val.toString());
} catch (Exception e) {
return this.getNumber(key);
} catch (Exception ex) {
return defaultValue;
}
}
Expand Down Expand Up @@ -1472,6 +1461,30 @@ public String optString(String key, String defaultValue) {
return NULL.equals(object) ? defaultValue : object.toString();
}

public Integer getInteger(String key) {
final Object object = this.get(key);
if (object instanceof Number) {
return ((Number) object).intValue();
}
try {
return Integer.parseInt(object.toString());
} catch (Exception e) {
throw wrongValueFormatException(key, "integer", e);
}
}

public Integer optInteger(String key) {
return this.optInteger(key, null);
}

public Integer optInteger(String key, Integer defaultValue) {
try {
return this.getInteger(key);
} catch (Exception ex) {
return defaultValue;
}
}

/**
* Populates the internal map of the JSONObject with the bean properties. The
* bean can not be recursive.
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/json/junit/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,44 @@ public void testPutNullObject() {
jsonObject.put(null, new Object());
fail("Expected an exception");
}

@Test
public void testPutFloat() {
// put null should remove the item.
JSONObject json_0 = new JSONObject()
.put("myKey_0", "0")
.put("myKey_1", "1")
.put("myKey_2", "-1");
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_0") == json_0.getFloat("myKey_0"));
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_1") == json_0.getFloat("myKey_1"));
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_2") == json_0.getFloat("myKey_2"));
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_3", -123) == -123.00F);

JSONObject json_1 = new JSONObject()
.put("myKey_0", "0.00")
.put("myKey_1", "1.00")
.put("myKey_2", "-1.00");
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_0") == json_1.getFloat("myKey_0"));
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_1") == json_1.getFloat("myKey_1"));
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_2") == json_1.getFloat("myKey_2"));
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_3", -123) == -123.00F);

JSONObject json_2 = new JSONObject()
.put("myKey_0", "00")
.put("myKey_1", "01")
.put("myKey_2", "-01");
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_0") == json_2.getFloat("myKey_0"));
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_1") == json_2.getFloat("myKey_1"));
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_2") == json_2.getFloat("myKey_2"));
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_3", -123) == -123.00F);

JSONObject json_3 = new JSONObject()
.put("myKey_0", "00.00")
.put("myKey_1", "01.00")
.put("myKey_2", "-01.00");
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_0") == json_3.getFloat("myKey_0"));
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_1") == json_3.getFloat("myKey_1"));
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_2") == json_3.getFloat("myKey_2"));
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_3", -123) == -123.00F);
}
}

0 comments on commit b955163

Please sign in to comment.