Skip to content

Commit 7589408

Browse files
committed
Fixed incorrect cast getting float from array
Added test for getting float from array
1 parent fa46da4 commit 7589408

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public double getDouble(int index) throws JSONException {
326326
public float getFloat(int index) throws JSONException {
327327
final Object object = this.get(index);
328328
if(object instanceof Number) {
329-
return ((Float)object).floatValue();
329+
return ((Number)object).floatValue();
330330
}
331331
try {
332332
return Float.parseFloat(object.toString());

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ public void getArrayValues() {
364364
new Double(23.45e-4).equals(jsonArray.getDouble(5)));
365365
assertTrue("Array string double",
366366
new Double(23.45).equals(jsonArray.getDouble(6)));
367+
assertTrue("Array double can be float",
368+
new Float(23.45e-4f).equals(jsonArray.getFloat(5)));
367369
// ints
368370
assertTrue("Array value int",
369371
new Integer(42).equals(jsonArray.getInt(7)));

0 commit comments

Comments
 (0)