Skip to content

Commit 0640856

Browse files
committed
unexpected double behavior
1 parent 994a19b commit 0640856

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

JSONObjectTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,33 @@ public void jsonObjectNonAndWrongValues() {
604604
exceptionCount == tryCount);
605605
}
606606

607+
@Test
608+
public void unexpectedDoubleToIntConversion() {
609+
/**
610+
* This test documents an unexpected numeric behavior.
611+
* A double that ends with .0 is parsed, serialized, then
612+
* parsed again. On the second parse, it has become an int.
613+
*/
614+
String key30 = "key30";
615+
String key31 = "key31";
616+
JSONObject jsonObject = new JSONObject();
617+
jsonObject.put(key30, new Double(3.0));
618+
jsonObject.put(key31, new Double(3.1));
619+
620+
assertTrue("3.0 should remain a double",
621+
jsonObject.getDouble(key30) == 3);
622+
assertTrue("3.1 should remain a double",
623+
jsonObject.getDouble(key31) == 3.1);
624+
625+
// turns 3.0 into 3.
626+
String serializedString = jsonObject.toString();
627+
JSONObject deserialized = new JSONObject(serializedString);
628+
assertTrue("3.0 is now an int", deserialized.get(key30) instanceof Integer);
629+
assertTrue("3.0 can still be interpreted as a double",
630+
deserialized.getDouble(key30) == 3.0);
631+
assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1);
632+
}
633+
607634
/**
608635
* The purpose for the static method getNames() methods are not clear.
609636
* This method is not called from within JSON-Java. Most likely

0 commit comments

Comments
 (0)