@@ -637,6 +637,10 @@ public void unexpectedDoubleToIntConversion() {
637637 }
638638
639639 @ Test
640+ /**
641+ * Important behaviors of big numbers. Includes both JSONObject
642+ * and JSONArray tests
643+ */
640644 public void bigNumberOperations () {
641645 /**
642646 * JSONObject tries to parse BigInteger as a bean, but it only has
@@ -691,6 +695,9 @@ public void bigNumberOperations() {
691695 jsonObject .toString ().equals (
692696 "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
693697
698+ JSONArray jsonArray = new JSONArray ();
699+
700+
694701 /**
695702 * JSONObject.numberToString() works correctly, nothing to change.
696703 */
@@ -715,9 +722,7 @@ public void bigNumberOperations() {
715722 !obj .toString ().equals (bigDecimal .toString ()));
716723
717724 /**
718- * wrap() vs put() big number behavior changes serialization
719- * This is a bug.
720- * TODO: Updated expected wrap() test results after JSONObject is updated.
725+ * wrap() vs put() big number behavior is now the same.
721726 */
722727 // bigInt map ctor
723728 Map <String , Object > map = new HashMap <String , Object >();
@@ -726,7 +731,7 @@ public void bigNumberOperations() {
726731 String actualFromMapStr = jsonObject .toString ();
727732 assertTrue ("bigInt in map (or array or bean) is a string" ,
728733 actualFromMapStr .equals (
729- "{\" bigInt\" :\" 123456789012345678901234567890\" }" ));
734+ "{\" bigInt\" :123456789012345678901234567890}" ));
730735 // bigInt put
731736 jsonObject = new JSONObject ();
732737 jsonObject .put ("bigInt" , bigInteger );
@@ -739,9 +744,9 @@ public void bigNumberOperations() {
739744 map .put ("bigDec" , bigDecimal );
740745 jsonObject = new JSONObject (map );
741746 actualFromMapStr = jsonObject .toString ();
742- assertTrue ("bigDec in map (or array or bean) is a string " ,
747+ assertTrue ("bigDec in map (or array or bean) is a bigDec " ,
743748 actualFromMapStr .equals (
744- "{\" bigDec\" :\" 123456789012345678901234567890.12345678901234567890123456789\" }" ));
749+ "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
745750 // bigDec put
746751 jsonObject = new JSONObject ();
747752 jsonObject .put ("bigDec" , bigDecimal );
@@ -750,43 +755,59 @@ public void bigNumberOperations() {
750755 actualFromPutStr .equals (
751756 "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
752757 // bigInt,bigDec put
753- JSONArray jsonArray = new JSONArray ();
758+ jsonArray = new JSONArray ();
754759 jsonArray .put (bigInteger );
755760 jsonArray .put (bigDecimal );
756761 actualFromPutStr = jsonArray .toString ();
757762 assertTrue ("bigInt, bigDec from put is a number" ,
758763 actualFromPutStr .equals (
759764 "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]" ));
765+ assertTrue ("getBigInt is bigInt" , jsonArray .getBigInteger (0 ).equals (bigInteger ));
766+ assertTrue ("getBigDec is bigDec" , jsonArray .getBigDecimal (1 ).equals (bigDecimal ));
767+ assertTrue ("optBigInt is bigInt" , jsonArray .optBigInteger (0 , BigInteger .ONE ).equals (bigInteger ));
768+ assertTrue ("optBigDec is bigDec" , jsonArray .optBigDecimal (1 , BigDecimal .ONE ).equals (bigDecimal ));
769+ jsonArray .put (Boolean .TRUE );
770+ try {
771+ jsonArray .getBigInteger (2 );
772+ assertTrue ("should not be able to get big int" , false );
773+ } catch (Exception ignored ) {}
774+ try {
775+ jsonArray .getBigDecimal (2 );
776+ assertTrue ("should not be able to get big dec" , false );
777+ } catch (Exception ignored ) {}
778+ assertTrue ("optBigInt is default" , jsonArray .optBigInteger (2 , BigInteger .ONE ).equals (BigInteger .ONE ));
779+ assertTrue ("optBigDec is default" , jsonArray .optBigDecimal (2 , BigDecimal .ONE ).equals (BigDecimal .ONE ));
780+
760781 // bigInt,bigDec list ctor
761782 List <Object > list = new ArrayList <Object >();
762783 list .add (bigInteger );
763784 list .add (bigDecimal );
764785 jsonArray = new JSONArray (list );
765786 String actualFromListStr = jsonArray .toString ();
766- assertTrue ("bigInt, bigDec in list is a string " ,
787+ assertTrue ("bigInt, bigDec in list is a bigInt, bigDec " ,
767788 actualFromListStr .equals (
768- "[\" 123456789012345678901234567890\" , \" 123456789012345678901234567890.12345678901234567890123456789\" ]" ));
789+ "[123456789012345678901234567890, 123456789012345678901234567890.12345678901234567890123456789]" ));
769790 // bigInt bean ctor
770791 MyBigNumberBean myBigNumberBean = mock (MyBigNumberBean .class );
771792 when (myBigNumberBean .getBigInteger ()).thenReturn (new BigInteger ("123456789012345678901234567890" ));
772793 jsonObject = new JSONObject (myBigNumberBean );
773794 String actualFromBeanStr = jsonObject .toString ();
774795 // can't do a full string compare because mockery adds an extra key/value
775- assertTrue ("bigInt from bean ctor is a string " ,
776- actualFromBeanStr .contains ("\" 123456789012345678901234567890\" " ));
796+ assertTrue ("bigInt from bean ctor is a bigInt " ,
797+ actualFromBeanStr .contains ("123456789012345678901234567890" ));
777798 // bigDec bean ctor
778799 myBigNumberBean = mock (MyBigNumberBean .class );
779800 when (myBigNumberBean .getBigDecimal ()).thenReturn (new BigDecimal ("123456789012345678901234567890.12345678901234567890123456789" ));
780801 jsonObject = new JSONObject (myBigNumberBean );
781802 actualFromBeanStr = jsonObject .toString ();
782803 // can't do a full string compare because mockery adds an extra key/value
783- assertTrue ("bigDec from bean ctor is a string " ,
784- actualFromBeanStr .contains ("\" 123456789012345678901234567890.12345678901234567890123456789\" " ));
804+ assertTrue ("bigDec from bean ctor is a bigDec " ,
805+ actualFromBeanStr .contains ("123456789012345678901234567890.12345678901234567890123456789" ));
785806 // bigInt,bigDec wrap()
786807 obj = JSONObject .wrap (bigInteger );
787- assertTrue ("wrap() returns string " ,obj .equals (bigInteger . toString () ));
808+ assertTrue ("wrap() returns big num " ,obj .equals (bigInteger ));
788809 obj = JSONObject .wrap (bigDecimal );
789- assertTrue ("wrap() returns string" ,obj .equals (bigDecimal . toString () ));
810+ assertTrue ("wrap() returns string" ,obj .equals (bigDecimal ));
790811
791812 }
792813
@@ -1207,8 +1228,8 @@ public void wrapObject() {
12071228 * is recognized as being a Java package class.
12081229 */
12091230 Object bdWrap = JSONObject .wrap (BigDecimal .ONE );
1210- assertTrue ("BigDecimal.ONE evaluates to string " ,
1211- bdWrap .equals (BigDecimal .ONE . toString () ));
1231+ assertTrue ("BigDecimal.ONE evaluates to ONE " ,
1232+ bdWrap .equals (BigDecimal .ONE ));
12121233
12131234 // wrap JSONObject returns JSONObject
12141235 String jsonObjectStr =
0 commit comments