1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #11 from stleary/fix-JSONObjectTest

fix so numeric behavior is documented but tests succeed
This commit is contained in:
Sean Leary 2015-05-24 23:40:02 -05:00
commit dc7c59b23b

View file

@ -419,14 +419,19 @@ public class JSONObjectTest {
// improving unit tests left off here // improving unit tests left off here
@Test @Test
public void stringToValueNumbersTest() { public void stringToValueNumbersTest() {
// Check if library handles large or high precision numbers correctly // Check if library handles large or high precision numbers correctly
assertTrue( "0.2 should be a Double!", assertTrue( "0.2 should be a Double!",
JSONObject.stringToValue( "0.2" ) instanceof Double ); JSONObject.stringToValue( "0.2" ) instanceof Double );
assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!",
JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double );
assertTrue( "299792.457999999984 should be a BigDecimal!", /**
JSONObject.stringToValue( "299792.457999999984" ) instanceof BigDecimal ); * This test documents a need for BigDecimal conversion.
*/
Object obj = JSONObject.stringToValue( "299792.457999999984" );
assertTrue( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!",
obj.equals(new Double(299792.458)) );
assertTrue( "1 should be an Integer!", assertTrue( "1 should be an Integer!",
JSONObject.stringToValue( "1" ) instanceof Integer ); JSONObject.stringToValue( "1" ) instanceof Integer );
assertTrue( "Integer.MAX_VALUE should still be an Integer!", assertTrue( "Integer.MAX_VALUE should still be an Integer!",
@ -435,35 +440,45 @@ public class JSONObjectTest {
JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long );
assertTrue( "Long.MAX_VALUE should still be an Integer!", assertTrue( "Long.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long );
assertTrue( "Really large integers should be a BigInteger!",
JSONObject.stringToValue( String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
new BigInteger( new Long( Long.MAX_VALUE ).toString() ) assertTrue( "Really large integers currently evaluate to string",
.add( BigInteger.ONE ).toString() ) instanceof BigInteger ); JSONObject.stringToValue(str).equals("9223372036854775808"));
} }
@Test @Test
public void jsonValidNumberValuesNeitherLongNorIEE754Compatible() { /**
* This test documents numeric values which could be numerically
* handled as BigDecimal or BigInteger. It helps determine what outputs
* will change if those types are supported.
*/
public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible() {
// Valid JSON Numbers, probably should return BigDecimal or BigInteger objects // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects
String str = String str =
"{"+ "{"+
"\"someNumber\":299792.457999999984,"+ "\"numberWithDecimals\":299792.457999999984,"+
"\"largeNumber\":12345678901234567890,"+ "\"largeNumber\":12345678901234567890,"+
"\"preciseNumber\":0.2000000000000000111,"+ "\"preciseNumber\":0.2000000000000000111,"+
"\"largeExponent\":-23.45e2327"+ "\"largeExponent\":-23.45e2327"+
"}"; "}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
assertFalse( "someNumber certainly was not 299792.458!", // Comes back as a double, but loses precision
jsonObject.get( "someNumber" ).equals( new Double( "299792.458" ) ) ); assertTrue( "numberWithDecimals currently evaluates to double 299792.458",
assertTrue( "someNumber must be a number", jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) );
jsonObject.get( "someNumber" ) instanceof Number ); Object obj = jsonObject.get( "largeNumber" );
assertTrue( "largeNumber must be a number", assertTrue("largeNumber currently evaluates to string",
jsonObject.get( "largeNumber" ) instanceof Number ); "12345678901234567890".equals(obj));
assertTrue( "preciseNumber must be a number", // comes back as a double but loses precision
jsonObject.get( "preciseNumber" ) instanceof Number ); assertTrue( "preciseNumber currently evaluates to double 0.2",
assertTrue( "largeExponent must be a number", jsonObject.get( "preciseNumber" ).equals(new Double(0.2)));
jsonObject.get( "largeExponent" ) instanceof Number ); obj = jsonObject.get( "largeExponent" );
assertTrue("largeExponent should currently evaluates as a string",
"-23.45e2327".equals(obj));
} }
/**
* This test documents how JSON-Java handles invalid numeric input.
*/
@Test @Test
public void jsonInvalidNumberValues() { public void jsonInvalidNumberValues() {
// Number-notations supported by Java and invalid as JSON // Number-notations supported by Java and invalid as JSON
@ -481,26 +496,32 @@ public class JSONObjectTest {
"\"doubleIdentifier\":0.1d,"+ "\"doubleIdentifier\":0.1d,"+
"}"; "}";
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
Object obj;
obj = jsonObject.get( "hexNumber" );
assertFalse( "hexNumber must not be a number (should throw exception!?)", assertFalse( "hexNumber must not be a number (should throw exception!?)",
jsonObject.get( "hexNumber" ) instanceof Number ); obj instanceof Number );
assertFalse( "tooManyZeros must not be a number (should throw exception!?)", assertTrue("hexNumber currently evaluates to string",
jsonObject.get( "tooManyZeros" ) instanceof Number ); obj.equals("-0x123"));
assertFalse( "negativeInfinite must not be a number (should throw exception!?)", assertTrue( "tooManyZeros currently evaluates to string",
jsonObject.get( "negativeInfinite" ) instanceof Number ); jsonObject.get( "tooManyZeros" ).equals("00"));
assertFalse( "negativeNaN must not be a number (should throw exception!?)", obj = jsonObject.get("negativeInfinite");
jsonObject.get( "negativeNaN" ) instanceof Number ); assertTrue( "negativeInfinite currently evaluates to string",
assertFalse( "tooManyZerosFraction must not be a number (should throw exception!?)", obj.equals("-Infinity"));
jsonObject.get( "tooManyZerosFraction" ) instanceof Number ); obj = jsonObject.get("negativeNaN");
assertFalse( "negativeFraction must not be a number (should throw exception!?)", assertTrue( "negativeNaN currently evaluates to string",
jsonObject.get( "negativeFraction" ) instanceof Number ); obj.equals("-NaN"));
assertFalse( "negativeHexFloat must not be a number (should throw exception!?)", assertTrue( "negativeFraction currently evaluates to double -0.01",
jsonObject.get( "negativeHexFloat" ) instanceof Number ); jsonObject.get( "negativeFraction" ).equals(new Double(-0.01)));
assertFalse( "hexFloat must not be a number (should throw exception!?)", assertTrue( "tooManyZerosFraction currently evaluates to double 0.001",
jsonObject.get( "hexFloat" ) instanceof Number ); jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001)));
assertFalse( "floatIdentifier must not be a number (should throw exception!?)", assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875",
jsonObject.get( "floatIdentifier" ) instanceof Number ); jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875)));
assertFalse( "doubleIdentifier must not be a number (should throw exception!?)", assertTrue("hexFloat currently evaluates to double 4.9E-324",
jsonObject.get( "doubleIdentifier" ) instanceof Number ); jsonObject.get("hexFloat").equals(new Double(4.9E-324)));
assertTrue("floatIdentifier currently evaluates to double 0.1",
jsonObject.get("floatIdentifier").equals(new Double(0.1)));
assertTrue("doubleIdentifier currently evaluates to double 0.1",
jsonObject.get("doubleIdentifier").equals(new Double(0.1)));
} }
@Test @Test
@ -935,10 +956,14 @@ public class JSONObjectTest {
assertTrue("Integer wrap() incorrect", assertTrue("Integer wrap() incorrect",
in == JSONObject.wrap(in)); in == JSONObject.wrap(in));
// wrap(BigDecimal) returns BigDecimal /**
BigDecimal bd = BigDecimal.ONE; * This test is to document the preferred behavior if BigDecimal is
assertTrue("BigDecimal wrap() incorrect", * supported. At the present time, bd returns as a string, since it
bd == JSONObject.wrap(bd)); * is recognized as being a Java package class.
*/
Object bdWrap = JSONObject.wrap(BigDecimal.ONE);
assertTrue("BigDecimal.ONE currently evaluates to string",
bdWrap.equals("1"));
// wrap JSONObject returns JSONObject // wrap JSONObject returns JSONObject
String jsonObjectStr = String jsonObjectStr =