diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 342b91e99..d8ecfd3b9 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -385,7 +385,7 @@ public > E getEnum(Class clazz, int index) throws JSONExcep /** * Get the BigDecimal value associated with an index. If the value is float - * or double, the the {@link BigDecimal#BigDecimal(double)} constructor + * or double, the the {@link BigDecimal#BigDecimal(String)} constructor * will be used. See notes on the constructor for conversion issues that * may arise. * @@ -792,7 +792,7 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) { * Get the optional BigDecimal value associated with an index. The * defaultValue is returned if there is no value for the index, or if the * value is not a number and cannot be converted to a number. If the value - * is float or double, the the {@link BigDecimal#BigDecimal(double)} + * is float or double, the the {@link BigDecimal#BigDecimal(String)} * constructor will be used. See notes on the constructor for conversion * issues that may arise. * diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index a8cfbb7ce..69efdf5bd 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -644,7 +644,7 @@ public BigInteger getBigInteger(String key) throws JSONException { /** * Get the BigDecimal value associated with a key. If the value is float or - * double, the the {@link BigDecimal#BigDecimal(double)} constructor will + * double, the the {@link BigDecimal#BigDecimal(String)} constructor will * be used. See notes on the constructor for conversion issues that may * arise. * @@ -1137,7 +1137,7 @@ public boolean optBoolean(String key, boolean defaultValue) { * Get an optional BigDecimal associated with a key, or the defaultValue if * there is no such key or if its value is not a number. If the value is a * string, an attempt will be made to evaluate it as a number. If the value - * is float or double, then the {@link BigDecimal#BigDecimal(double)} + * is float or double, then the {@link BigDecimal#BigDecimal(String)} * constructor will be used. See notes on the constructor for conversion * issues that may arise. * @@ -1172,7 +1172,10 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) { if (!numberIsFinite((Number)val)) { return defaultValue; } - return new BigDecimal(((Number) val).doubleValue()); + // use the string constructor so that we maintain "nice" values for doubles and floats + // the double constructor will translate doubles to "exact" values instead of the likely + // intended representation + return new BigDecimal(val.toString()); } if (val instanceof Long || val instanceof Integer || val instanceof Short || val instanceof Byte){ diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index cec27a9e5..eb0678de9 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -126,6 +126,7 @@ public void verifySimilar() { assertTrue("Should eval to true", obj1.similar(obj4)); + // verify that a double and big decimal are "similar" assertTrue("should eval to true",new JSONObject().put("a",1.1d).similar(new JSONObject("{\"a\":1.1}"))); } @@ -942,7 +943,7 @@ public void stringToValueNumbersTest() { assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double); assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String); - assertTrue( "0.2 should be a Double!", + assertTrue( "0.2 should be a BigDecimal!", JSONObject.stringToValue( "0.2" ) instanceof BigDecimal ); assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!", JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof BigDecimal ); @@ -2521,8 +2522,8 @@ public void jsonObjectOptBigDecimal() { assertEquals(new BigDecimal("123"),jo.optBigDecimal("int", null)); assertEquals(new BigDecimal("654"),jo.optBigDecimal("long", null)); - assertEquals(new BigDecimal(1.234f),jo.optBigDecimal("float", null)); - assertEquals(new BigDecimal(2.345d),jo.optBigDecimal("double", null)); + assertEquals(new BigDecimal("1.234"),jo.optBigDecimal("float", null)); + assertEquals(new BigDecimal("2.345"),jo.optBigDecimal("double", null)); assertEquals(new BigDecimal("1234"),jo.optBigDecimal("bigInteger", null)); assertEquals(new BigDecimal("1234.56789"),jo.optBigDecimal("bigDecimal", null)); assertNull(jo.optBigDecimal("nullVal", null));