changes number parsing to use BigDecimal as the backing type

* updated tests to support BigDecimal as the backing type for numbers
* updated some test resource handling to java7 try-with-resources format
* cleaned up some other minor compiler warnings
This commit is contained in:
John J. Aylward
2018-12-10 13:19:31 -05:00
parent 956bdfa5b7
commit 56d33b8061
13 changed files with 248 additions and 255 deletions

View File

@@ -75,7 +75,6 @@ import org.json.junit.data.Singleton;
import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList;
import org.junit.Test;
import org.junit.Ignore;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
@@ -268,7 +267,7 @@ public class JSONObjectTest {
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey")));
assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey")));
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey")));
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObjectByName.query("/doubleKey")));
assertTrue("expected \"doubleKey\":-23.45e67", new BigDecimal("-23.45e67").equals(jsonObjectByName.query("/doubleKey")));
}
/**
@@ -891,14 +890,14 @@ public class JSONObjectTest {
jsonObject.optNumber("intKey") instanceof Integer);
assertTrue("optNumber long should return Long",
jsonObject.optNumber("longKey") instanceof Long);
assertTrue("optNumber double should return Double",
jsonObject.optNumber("doubleKey") instanceof Double);
assertTrue("optNumber double should return BigDecimal",
jsonObject.optNumber("doubleKey") instanceof BigDecimal);
assertTrue("optNumber Str int should return Integer",
jsonObject.optNumber("intStrKey") instanceof Integer);
assertTrue("optNumber Str long should return Long",
jsonObject.optNumber("longStrKey") instanceof Long);
assertTrue("optNumber Str double should return Double",
jsonObject.optNumber("doubleStrKey") instanceof Double);
assertTrue("optNumber Str double should return BigDecimal",
jsonObject.optNumber("doubleStrKey") instanceof BigDecimal);
assertTrue("optNumber BigDecimalStrKey should return BigDecimal",
jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal);
assertTrue("xKey should not exist",
@@ -933,27 +932,27 @@ public class JSONObjectTest {
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!",
JSONObject.stringToValue( "0.2" ) instanceof Double );
assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!",
JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double );
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 );
/**
* This test documents a need for BigDecimal conversion.
*/
Object obj = JSONObject.stringToValue( "299792.457999999984" );
assertTrue( "evaluates to 299792.458 double instead of 299792.457999999984 BigDecimal!",
obj.equals(new Double(299792.458)) );
assertTrue( "does not evaluate to 299792.457999999984 BigDecimal!",
obj.equals(new BigDecimal("299792.457999999984")) );
assertTrue( "1 should be an Integer!",
JSONObject.stringToValue( "1" ) instanceof Integer );
assertTrue( "Integer.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer );
// assertTrue( "Large integers should be a Long!",
// JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long );
assertTrue( "Large integers should be a Long!",
JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long );
assertTrue( "Long.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long );
String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
assertTrue( "Really large integers currently evaluate to string",
JSONObject.stringToValue(str).equals("9223372036854775808"));
assertTrue( "Really large integers currently evaluate to BigInteger",
JSONObject.stringToValue(str).equals(new BigInteger("9223372036854775808")));
}
/**
@@ -974,16 +973,16 @@ public class JSONObjectTest {
JSONObject jsonObject = new JSONObject(str);
// Comes back as a double, but loses precision
assertTrue( "numberWithDecimals currently evaluates to double 299792.458",
jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) );
jsonObject.get( "numberWithDecimals" ).equals( new BigDecimal( "299792.457999999984" ) ) );
Object obj = jsonObject.get( "largeNumber" );
assertTrue("largeNumber currently evaluates to string",
"12345678901234567890".equals(obj));
assertTrue("largeNumber currently evaluates to BigInteger",
new BigInteger("12345678901234567890").equals(obj));
// comes back as a double but loses precision
assertTrue( "preciseNumber currently evaluates to double 0.2",
jsonObject.get( "preciseNumber" ).equals(new Double(0.2)));
assertEquals( "preciseNumber currently evaluates to double 0.2",
0.2, jsonObject.getDouble( "preciseNumber" ), 0.0);
obj = jsonObject.get( "largeExponent" );
assertTrue("largeExponent should currently evaluates as a string",
"-23.45e2327".equals(obj));
assertTrue("largeExponent should evaluate as a BigDecimal",
new BigDecimal("-23.45e2327").equals(obj));
}
/**
@@ -1021,17 +1020,17 @@ public class JSONObjectTest {
assertTrue( "negativeNaN currently evaluates to string",
obj.equals("-NaN"));
assertTrue( "negativeFraction currently evaluates to double -0.01",
jsonObject.get( "negativeFraction" ).equals(new Double(-0.01)));
jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-0.01)));
assertTrue( "tooManyZerosFraction currently evaluates to double 0.001",
jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001)));
jsonObject.get( "tooManyZerosFraction" ).equals(BigDecimal.valueOf(0.001)));
assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875",
jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875)));
jsonObject.get( "negativeHexFloat" ).equals(Double.valueOf(-3.99951171875)));
assertTrue("hexFloat currently evaluates to double 4.9E-324",
jsonObject.get("hexFloat").equals(new Double(4.9E-324)));
jsonObject.get("hexFloat").equals(Double.valueOf(4.9E-324)));
assertTrue("floatIdentifier currently evaluates to double 0.1",
jsonObject.get("floatIdentifier").equals(new Double(0.1)));
jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1)));
assertTrue("doubleIdentifier currently evaluates to double 0.1",
jsonObject.get("doubleIdentifier").equals(new Double(0.1)));
jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
}
/**
@@ -1311,11 +1310,11 @@ public class JSONObjectTest {
* might inconvenience users.
*/
obj = JSONObject.stringToValue(bigInteger.toString());
assertTrue("stringToValue() turns bigInteger string into string",
obj instanceof String);
assertTrue("stringToValue() turns bigInteger string into Number",
obj instanceof Number);
obj = JSONObject.stringToValue(bigDecimal.toString());
assertTrue("stringToValue() changes bigDecimal string",
!obj.toString().equals(bigDecimal.toString()));
assertTrue("stringToValue() changes bigDecimal Number",
obj instanceof Number);
/**
* wrap() vs put() big number behavior is now the same.
@@ -1580,7 +1579,7 @@ public class JSONObjectTest {
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt")));
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong")));
assertTrue("expected 3.1", Double.valueOf(3.1).equals(jsonObject.query("/keyDouble")));
assertTrue("expected 3.1", BigDecimal.valueOf(3.1).equals(jsonObject.query("/keyDouble")));
assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt")));
assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec")));
@@ -2083,7 +2082,7 @@ public class JSONObjectTest {
/**
* Explore how JSONObject handles parsing errors.
*/
@SuppressWarnings("boxing")
@SuppressWarnings({"boxing", "unused"})
@Test
public void jsonObjectParsingErrors() {
try {
@@ -2629,14 +2628,11 @@ public class JSONObjectTest {
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
String expectedStr = str;
JSONObject jsonObject = new JSONObject(str);
StringWriter stringWriter = new StringWriter();
try {
try (StringWriter stringWriter = new StringWriter()) {
String actualStr = jsonObject.write(stringWriter).toString();
assertTrue("write() expected " +expectedStr+
" but found " +actualStr,
expectedStr.equals(actualStr));
} finally {
stringWriter.close();
}
}
@@ -2739,21 +2735,15 @@ public class JSONObjectTest {
" }";
JSONObject jsonObject = new JSONObject(str0);
String expectedStr = str0;
StringWriter stringWriter = new StringWriter();
try {
try (StringWriter stringWriter = new StringWriter();) {
String actualStr = jsonObject.write(stringWriter,0,0).toString();
assertEquals(expectedStr, actualStr);
} finally {
stringWriter.close();
}
expectedStr = str2;
stringWriter = new StringWriter();
try {
try (StringWriter stringWriter = new StringWriter();) {
String actualStr = jsonObject.write(stringWriter,2,1).toString();
assertEquals(expectedStr, actualStr);
} finally {
stringWriter.close();
}
}
@@ -2965,7 +2955,7 @@ public class JSONObjectTest {
assertTrue("key3 list val 1 should not be null", key3Val1List != null);
assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2);
assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));
assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1")));
assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(new BigDecimal("2.1")));
List<?> key3Val2List = (List<?>)key3List.get(1);
assertTrue("key3 list val 2 should not be null", key3Val2List != null);
@@ -2984,8 +2974,7 @@ public class JSONObjectTest {
/**
* test that validates a singleton can be serialized as a bean.
*/
// @todo: investigate, re-enable this test
@Ignore
@SuppressWarnings("boxing")
@Test
public void testSingletonBean() {
final JSONObject jo = new JSONObject(Singleton.getInstance());
@@ -3009,8 +2998,7 @@ public class JSONObjectTest {
/**
* test that validates a singleton can be serialized as a bean.
*/
// @todo: investigate, re-enable this test
@Ignore
@SuppressWarnings("boxing")
@Test
public void testSingletonEnumBean() {
final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
@@ -3034,6 +3022,7 @@ public class JSONObjectTest {
/**
* Test to validate that a generic class can be serialized as a bean.
*/
@SuppressWarnings("boxing")
@Test
public void testGenericBean() {
GenericBean<Integer> bean = new GenericBean<>(42);
@@ -3048,6 +3037,7 @@ public class JSONObjectTest {
/**
* Test to validate that a generic class can be serialized as a bean.
*/
@SuppressWarnings("boxing")
@Test
public void testGenericIntBean() {
GenericBeanInt bean = new GenericBeanInt(42);
@@ -3064,6 +3054,7 @@ public class JSONObjectTest {
*/
@Test
public void testWierdListBean() {
@SuppressWarnings("boxing")
WeirdList bean = new WeirdList(42, 43, 44);
final JSONObject jo = new JSONObject(bean);
// get() should have a key of 0 length