Skip to content

Commit b955163

Browse files
author
Leonardo Lopes
committed
fixed problems caused by nonsense logic in opt methods
1 parent 1786f7a commit b955163

File tree

5 files changed

+139
-87
lines changed

5 files changed

+139
-87
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In project's pom.xml add in ```<dependencies>```:
2828
<dependency>
2929
<groupId>com.github.leonardofel</groupId>
3030
<artifactId>json-java-put-null-fix</artifactId>
31-
<version>3.0.30</version>
31+
<version>3.0.31</version>
3232
</dependency>
3333
</dependencies>
3434
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.github.leonardofel</groupId>
55
<artifactId>json-java-put-null-fix</artifactId>
6-
<version>3.0.30</version>
6+
<version>3.0.31</version>
77
<packaging>jar</packaging>
88

99
<name>JSON in Java WITH WORKING .put(null)</name>

src/main/java/org/json/JSONArray.java

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -636,15 +636,11 @@ public double optDouble(int index) {
636636
* @return The value.
637637
*/
638638
public double optDouble(int index, double defaultValue) {
639-
final Number val = this.optNumber(index, null);
640-
if (val == null) {
639+
try {
640+
return this.getDouble(index);
641+
} catch (Exception ex) {
641642
return defaultValue;
642643
}
643-
final double doubleValue = val.doubleValue();
644-
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
645-
// return defaultValue;
646-
// }
647-
return doubleValue;
648644
}
649645

650646
/**
@@ -672,15 +668,11 @@ public float optFloat(int index) {
672668
* @return The value.
673669
*/
674670
public float optFloat(int index, float defaultValue) {
675-
final Number val = this.optNumber(index, null);
676-
if (val == null) {
671+
try {
672+
return this.getFloat(index);
673+
} catch (Exception ex) {
677674
return defaultValue;
678675
}
679-
final float floatValue = val.floatValue();
680-
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
681-
// return floatValue;
682-
// }
683-
return floatValue;
684676
}
685677

686678
/**
@@ -708,11 +700,11 @@ public int optInt(int index) {
708700
* @return The value.
709701
*/
710702
public int optInt(int index, int defaultValue) {
711-
final Number val = this.optNumber(index, null);
712-
if (val == null) {
703+
try {
704+
return this.getInt(index);
705+
} catch (Exception ex) {
713706
return defaultValue;
714707
}
715-
return val.intValue();
716708
}
717709

718710
/**
@@ -776,8 +768,11 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
776768
* @return The value.
777769
*/
778770
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
779-
Object val = this.opt(index);
780-
return JSONObject.objectToBigInteger(val, defaultValue);
771+
try {
772+
return this.getBigInteger(index);
773+
} catch (Exception ex) {
774+
return defaultValue;
775+
}
781776
}
782777

783778
/**
@@ -795,8 +790,11 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
795790
* @return The value.
796791
*/
797792
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
798-
Object val = this.opt(index);
799-
return JSONObject.objectToBigDecimal(val, defaultValue);
793+
try {
794+
return this.getBigDecimal(index);
795+
} catch (Exception ex) {
796+
return defaultValue;
797+
}
800798
}
801799

802800
/**
@@ -851,11 +849,11 @@ public long optLong(int index) {
851849
* @return The value.
852850
*/
853851
public long optLong(int index, long defaultValue) {
854-
final Number val = this.optNumber(index, null);
855-
if (val == null) {
852+
try {
853+
return this.getLong(index);
854+
} catch (Exception ex) {
856855
return defaultValue;
857856
}
858-
return val.longValue();
859857
}
860858

861859
/**
@@ -885,22 +883,11 @@ public Number optNumber(int index) {
885883
* @return An object which is the value.
886884
*/
887885
public Number optNumber(int index, Number defaultValue) {
888-
Object val = this.opt(index);
889-
if (JSONObject.NULL.equals(val)) {
886+
try {
887+
return this.getNumber(index);
888+
} catch (Exception ex) {
890889
return defaultValue;
891890
}
892-
if (val instanceof Number){
893-
return (Number) val;
894-
}
895-
896-
if (val instanceof String) {
897-
try {
898-
return JSONObject.stringToNumber((String) val);
899-
} catch (Exception e) {
900-
return defaultValue;
901-
}
902-
}
903-
return defaultValue;
904891
}
905892

906893
/**
@@ -932,6 +919,30 @@ public String optString(int index, String defaultValue) {
932919
.toString();
933920
}
934921

922+
public Integer getInteger(int index) {
923+
final Object object = this.get(index);
924+
if (object instanceof Number) {
925+
return ((Number) object).intValue();
926+
}
927+
try {
928+
return Integer.parseInt(object.toString());
929+
} catch (Exception e) {
930+
throw wrongValueFormatException(index, "integer", e);
931+
}
932+
}
933+
934+
public Integer optInteger(int index) {
935+
return this.optInteger(index, null);
936+
}
937+
938+
public Integer optInteger(int index, Integer defaultValue) {
939+
try {
940+
return this.getInteger(index);
941+
} catch (Exception ex) {
942+
return defaultValue;
943+
}
944+
}
945+
935946
/**
936947
* Append a boolean value. This increases the array's length by one.
937948
*
@@ -1700,16 +1711,4 @@ private static JSONException wrongValueFormatException(
17001711
"JSONArray[" + idx + "] is not a " + valueType + " (" + value + ")."
17011712
, cause);
17021713
}
1703-
1704-
public Integer optInteger(int index) {
1705-
return this.optInteger(index, null);
1706-
}
1707-
1708-
public Integer optInteger(int index, Integer defaultValue) {
1709-
final Number val = this.optNumber(index, null);
1710-
if (val == null) {
1711-
return defaultValue;
1712-
}
1713-
return val.intValue();
1714-
}
17151714
}

src/main/java/org/json/JSONObject.java

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,11 @@ public boolean optBoolean(String key, boolean defaultValue) {
11401140
* @return An object which is the value.
11411141
*/
11421142
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
1143-
Object val = this.opt(key);
1144-
return objectToBigDecimal(val, defaultValue);
1143+
try {
1144+
return this.getBigDecimal(key);
1145+
} catch (Exception ex) {
1146+
return defaultValue;
1147+
}
11451148
}
11461149

11471150
/**
@@ -1191,8 +1194,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
11911194
* @return An object which is the value.
11921195
*/
11931196
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
1194-
Object val = this.opt(key);
1195-
return objectToBigInteger(val, defaultValue);
1197+
try {
1198+
return this.getBigInteger(key);
1199+
} catch (Exception ex) {
1200+
return defaultValue;
1201+
}
11961202
}
11971203

11981204
/**
@@ -1264,15 +1270,11 @@ public double optDouble(String key) {
12641270
* @return An object which is the value.
12651271
*/
12661272
public double optDouble(String key, double defaultValue) {
1267-
Number val = this.optNumber(key);
1268-
if (val == null) {
1273+
try {
1274+
return this.getDouble(key);
1275+
} catch (Exception ex) {
12691276
return defaultValue;
12701277
}
1271-
final double doubleValue = val.doubleValue();
1272-
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
1273-
// return defaultValue;
1274-
// }
1275-
return doubleValue;
12761278
}
12771279

12781280
/**
@@ -1300,15 +1302,11 @@ public float optFloat(String key) {
13001302
* @return The value.
13011303
*/
13021304
public float optFloat(String key, float defaultValue) {
1303-
Number val = this.optNumber(key);
1304-
if (val == null) {
1305+
try {
1306+
return this.getFloat(key);
1307+
} catch (Exception ex) {
13051308
return defaultValue;
13061309
}
1307-
final float floatValue = val.floatValue();
1308-
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
1309-
// return defaultValue;
1310-
// }
1311-
return floatValue;
13121310
}
13131311

13141312
/**
@@ -1336,11 +1334,11 @@ public int optInt(String key) {
13361334
* @return An object which is the value.
13371335
*/
13381336
public int optInt(String key, int defaultValue) {
1339-
final Number val = this.optNumber(key, null);
1340-
if (val == null) {
1337+
try {
1338+
return this.getInt(key);
1339+
} catch (Exception ex) {
13411340
return defaultValue;
13421341
}
1343-
return val.intValue();
13441342
}
13451343

13461344
/**
@@ -1394,12 +1392,11 @@ public long optLong(String key) {
13941392
* @return An object which is the value.
13951393
*/
13961394
public long optLong(String key, long defaultValue) {
1397-
final Number val = this.optNumber(key, null);
1398-
if (val == null) {
1395+
try {
1396+
return this.getLong(key);
1397+
} catch (Exception ex) {
13991398
return defaultValue;
14001399
}
1401-
1402-
return val.longValue();
14031400
}
14041401

14051402
/**
@@ -1429,17 +1426,9 @@ public Number optNumber(String key) {
14291426
* @return An object which is the value.
14301427
*/
14311428
public Number optNumber(String key, Number defaultValue) {
1432-
Object val = this.opt(key);
1433-
if (NULL.equals(val)) {
1434-
return defaultValue;
1435-
}
1436-
if (val instanceof Number){
1437-
return (Number) val;
1438-
}
1439-
14401429
try {
1441-
return stringToNumber(val.toString());
1442-
} catch (Exception e) {
1430+
return this.getNumber(key);
1431+
} catch (Exception ex) {
14431432
return defaultValue;
14441433
}
14451434
}
@@ -1472,6 +1461,30 @@ public String optString(String key, String defaultValue) {
14721461
return NULL.equals(object) ? defaultValue : object.toString();
14731462
}
14741463

1464+
public Integer getInteger(String key) {
1465+
final Object object = this.get(key);
1466+
if (object instanceof Number) {
1467+
return ((Number) object).intValue();
1468+
}
1469+
try {
1470+
return Integer.parseInt(object.toString());
1471+
} catch (Exception e) {
1472+
throw wrongValueFormatException(key, "integer", e);
1473+
}
1474+
}
1475+
1476+
public Integer optInteger(String key) {
1477+
return this.optInteger(key, null);
1478+
}
1479+
1480+
public Integer optInteger(String key, Integer defaultValue) {
1481+
try {
1482+
return this.getInteger(key);
1483+
} catch (Exception ex) {
1484+
return defaultValue;
1485+
}
1486+
}
1487+
14751488
/**
14761489
* Populates the internal map of the JSONObject with the bean properties. The
14771490
* bean can not be recursive.

src/test/java/org/json/junit/JSONTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,44 @@ public void testPutNullObject() {
167167
jsonObject.put(null, new Object());
168168
fail("Expected an exception");
169169
}
170+
171+
@Test
172+
public void testPutFloat() {
173+
// put null should remove the item.
174+
JSONObject json_0 = new JSONObject()
175+
.put("myKey_0", "0")
176+
.put("myKey_1", "1")
177+
.put("myKey_2", "-1");
178+
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_0") == json_0.getFloat("myKey_0"));
179+
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_1") == json_0.getFloat("myKey_1"));
180+
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_2") == json_0.getFloat("myKey_2"));
181+
assertTrue("JSONObject getFloat differs from optFloat", json_0.optFloat("myKey_3", -123) == -123.00F);
182+
183+
JSONObject json_1 = new JSONObject()
184+
.put("myKey_0", "0.00")
185+
.put("myKey_1", "1.00")
186+
.put("myKey_2", "-1.00");
187+
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_0") == json_1.getFloat("myKey_0"));
188+
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_1") == json_1.getFloat("myKey_1"));
189+
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_2") == json_1.getFloat("myKey_2"));
190+
assertTrue("JSONObject getFloat differs from optFloat", json_1.optFloat("myKey_3", -123) == -123.00F);
191+
192+
JSONObject json_2 = new JSONObject()
193+
.put("myKey_0", "00")
194+
.put("myKey_1", "01")
195+
.put("myKey_2", "-01");
196+
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_0") == json_2.getFloat("myKey_0"));
197+
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_1") == json_2.getFloat("myKey_1"));
198+
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_2") == json_2.getFloat("myKey_2"));
199+
assertTrue("JSONObject getFloat differs from optFloat", json_2.optFloat("myKey_3", -123) == -123.00F);
200+
201+
JSONObject json_3 = new JSONObject()
202+
.put("myKey_0", "00.00")
203+
.put("myKey_1", "01.00")
204+
.put("myKey_2", "-01.00");
205+
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_0") == json_3.getFloat("myKey_0"));
206+
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_1") == json_3.getFloat("myKey_1"));
207+
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_2") == json_3.getFloat("myKey_2"));
208+
assertTrue("JSONObject getFloat differs from optFloat", json_3.optFloat("myKey_3", -123) == -123.00F);
209+
}
170210
}

0 commit comments

Comments
 (0)