@@ -419,14 +419,19 @@ public void jsonObjectValues() {
419419 // improving unit tests left off here
420420
421421 @ Test
422+
422423 public void stringToValueNumbersTest () {
423424 // Check if library handles large or high precision numbers correctly
424425 assertTrue ( "0.2 should be a Double!" ,
425426 JSONObject .stringToValue ( "0.2" ) instanceof Double );
426427 assertTrue ( "Doubles should be Doubles, even when incorrectly converting floats!" ,
427428 JSONObject .stringToValue ( new Double ( "0.2f" ).toString () ) instanceof Double );
428- assertTrue ( "299792.457999999984 should be a BigDecimal!" ,
429- JSONObject .stringToValue ( "299792.457999999984" ) instanceof BigDecimal );
429+ /**
430+ * This test documents a need for BigDecimal conversion.
431+ */
432+ Object obj = JSONObject .stringToValue ( "299792.457999999984" );
433+ assertTrue ( "evaluates to 299792.458 doubld instead of 299792.457999999984 BigDecimal!" ,
434+ obj .equals (new Double (299792.458 )) );
430435 assertTrue ( "1 should be an Integer!" ,
431436 JSONObject .stringToValue ( "1" ) instanceof Integer );
432437 assertTrue ( "Integer.MAX_VALUE should still be an Integer!" ,
@@ -435,35 +440,45 @@ public void stringToValueNumbersTest() {
435440 JSONObject .stringToValue ( new Long ( Long .sum ( Integer .MAX_VALUE , 1 ) ).toString () ) instanceof Long );
436441 assertTrue ( "Long.MAX_VALUE should still be an Integer!" ,
437442 JSONObject .stringToValue ( new Long ( Long .MAX_VALUE ).toString () ) instanceof Long );
438- assertTrue ( "Really large integers should be a BigInteger!" ,
439- JSONObject . stringToValue (
440- new BigInteger ( new Long ( Long . MAX_VALUE ). toString () )
441- . add ( BigInteger . ONE ). toString () ) instanceof BigInteger );
443+
444+ String str = new BigInteger ( new Long ( Long . MAX_VALUE ). toString () ). add ( BigInteger . ONE ). toString ();
445+ assertTrue ( "Really large integers currently evaluate to string" ,
446+ JSONObject . stringToValue ( str ). equals ( "9223372036854775808" ) );
442447 }
443448
444449 @ Test
445- public void jsonValidNumberValuesNeitherLongNorIEE754Compatible () {
450+ /**
451+ * This test documents numeric values which could be numerically
452+ * handled as BigDecimal or BigInteger. It helps determine what outputs
453+ * will change if those types are supported.
454+ */
455+ public void jsonValidNumberValuesNeitherLongNorIEEE754Compatible () {
446456 // Valid JSON Numbers, probably should return BigDecimal or BigInteger objects
447457 String str =
448458 "{" +
449- "\" someNumber \" :299792.457999999984," +
459+ "\" numberWithDecimals \" :299792.457999999984," +
450460 "\" largeNumber\" :12345678901234567890," +
451461 "\" preciseNumber\" :0.2000000000000000111," +
452462 "\" largeExponent\" :-23.45e2327" +
453463 "}" ;
454464 JSONObject jsonObject = new JSONObject (str );
455- assertFalse ( "someNumber certainly was not 299792.458!" ,
456- jsonObject .get ( "someNumber" ).equals ( new Double ( "299792.458" ) ) );
457- assertTrue ( "someNumber must be a number" ,
458- jsonObject .get ( "someNumber" ) instanceof Number );
459- assertTrue ( "largeNumber must be a number" ,
460- jsonObject .get ( "largeNumber" ) instanceof Number );
461- assertTrue ( "preciseNumber must be a number" ,
462- jsonObject .get ( "preciseNumber" ) instanceof Number );
463- assertTrue ( "largeExponent must be a number" ,
464- jsonObject .get ( "largeExponent" ) instanceof Number );
465+ // Comes back as a double, but loses precision
466+ assertTrue ( "numberWithDecimals currently evaluates to double 299792.458" ,
467+ jsonObject .get ( "numberWithDecimals" ).equals ( new Double ( "299792.458" ) ) );
468+ Object obj = jsonObject .get ( "largeNumber" );
469+ assertTrue ("largeNumber currently evaluates to string" ,
470+ "12345678901234567890" .equals (obj ));
471+ // comes back as a double but loses precision
472+ assertTrue ( "preciseNumber currently evaluates to double 0.2" ,
473+ jsonObject .get ( "preciseNumber" ).equals (new Double (0.2 )));
474+ obj = jsonObject .get ( "largeExponent" );
475+ assertTrue ("largeExponent should currently evaluates as a string" ,
476+ "-23.45e2327" .equals (obj ));
465477 }
466478
479+ /**
480+ * This test documents how JSON-Java handles invalid numeric input.
481+ */
467482 @ Test
468483 public void jsonInvalidNumberValues () {
469484 // Number-notations supported by Java and invalid as JSON
@@ -481,26 +496,32 @@ public void jsonInvalidNumberValues() {
481496 "\" doubleIdentifier\" :0.1d," +
482497 "}" ;
483498 JSONObject jsonObject = new JSONObject (str );
499+ Object obj ;
500+ obj = jsonObject .get ( "hexNumber" );
484501 assertFalse ( "hexNumber must not be a number (should throw exception!?)" ,
485- jsonObject .get ( "hexNumber" ) instanceof Number );
486- assertFalse ( "tooManyZeros must not be a number (should throw exception!?)" ,
487- jsonObject .get ( "tooManyZeros" ) instanceof Number );
488- assertFalse ( "negativeInfinite must not be a number (should throw exception!?)" ,
489- jsonObject .get ( "negativeInfinite" ) instanceof Number );
490- assertFalse ( "negativeNaN must not be a number (should throw exception!?)" ,
491- jsonObject .get ( "negativeNaN" ) instanceof Number );
492- assertFalse ( "tooManyZerosFraction must not be a number (should throw exception!?)" ,
493- jsonObject .get ( "tooManyZerosFraction" ) instanceof Number );
494- assertFalse ( "negativeFraction must not be a number (should throw exception!?)" ,
495- jsonObject .get ( "negativeFraction" ) instanceof Number );
496- assertFalse ( "negativeHexFloat must not be a number (should throw exception!?)" ,
497- jsonObject .get ( "negativeHexFloat" ) instanceof Number );
498- assertFalse ( "hexFloat must not be a number (should throw exception!?)" ,
499- jsonObject .get ( "hexFloat" ) instanceof Number );
500- assertFalse ( "floatIdentifier must not be a number (should throw exception!?)" ,
501- jsonObject .get ( "floatIdentifier" ) instanceof Number );
502- assertFalse ( "doubleIdentifier must not be a number (should throw exception!?)" ,
503- jsonObject .get ( "doubleIdentifier" ) instanceof Number );
502+ obj instanceof Number );
503+ assertTrue ("hexNumber currently evaluates to string" ,
504+ obj .equals ("-0x123" ));
505+ assertTrue ( "tooManyZeros currently evaluates to string" ,
506+ jsonObject .get ( "tooManyZeros" ).equals ("00" ));
507+ obj = jsonObject .get ("negativeInfinite" );
508+ assertTrue ( "negativeInfinite currently evaluates to string" ,
509+ obj .equals ("-Infinity" ));
510+ obj = jsonObject .get ("negativeNaN" );
511+ assertTrue ( "negativeNaN currently evaluates to string" ,
512+ obj .equals ("-NaN" ));
513+ assertTrue ( "negativeFraction currently evaluates to double -0.01" ,
514+ jsonObject .get ( "negativeFraction" ).equals (new Double (-0.01 )));
515+ assertTrue ( "tooManyZerosFraction currently evaluates to double 0.001" ,
516+ jsonObject .get ( "tooManyZerosFraction" ).equals (new Double (0.001 )));
517+ assertTrue ( "negativeHexFloat currently evaluates to double -3.99951171875" ,
518+ jsonObject .get ( "negativeHexFloat" ).equals (new Double (-3.99951171875 )));
519+ assertTrue ("hexFloat currently evaluates to double 4.9E-324" ,
520+ jsonObject .get ("hexFloat" ).equals (new Double (4.9E-324 )));
521+ assertTrue ("floatIdentifier currently evaluates to double 0.1" ,
522+ jsonObject .get ("floatIdentifier" ).equals (new Double (0.1 )));
523+ assertTrue ("doubleIdentifier currently evaluates to double 0.1" ,
524+ jsonObject .get ("doubleIdentifier" ).equals (new Double (0.1 )));
504525 }
505526
506527 @ Test
@@ -935,10 +956,14 @@ public void wrapObject() {
935956 assertTrue ("Integer wrap() incorrect" ,
936957 in == JSONObject .wrap (in ));
937958
938- // wrap(BigDecimal) returns BigDecimal
939- BigDecimal bd = BigDecimal .ONE ;
940- assertTrue ("BigDecimal wrap() incorrect" ,
941- bd == JSONObject .wrap (bd ));
959+ /**
960+ * This test is to document the preferred behavior if BigDecimal is
961+ * supported. At the present time, bd returns as a string, since it
962+ * is recognized as being a Java package class.
963+ */
964+ Object bdWrap = JSONObject .wrap (BigDecimal .ONE );
965+ assertTrue ("BigDecimal.ONE currently evaluates to string" ,
966+ bdWrap .equals ("1" ));
942967
943968 // wrap JSONObject returns JSONObject
944969 String jsonObjectStr =
0 commit comments