@@ -20,7 +20,7 @@ class MyJsonString implements JSONString {
2020 public String toJSONString () {
2121 return "my string" ;
2222 }
23- };
23+ }
2424
2525interface MyBean {
2626 public Integer getIntKey ();
@@ -32,6 +32,11 @@ interface MyBean {
3232 public StringReader getStringReaderKey ();
3333};
3434
35+ interface MyBigNumberBean {
36+ public BigInteger getBigInteger ();
37+ public BigDecimal getBigDecimal ();
38+ }
39+
3540/**
3641 * JSONObject, along with JSONArray, are the central classes of the reference app.
3742 * All of the other classes interact with it and JSON functionality would be
@@ -659,26 +664,32 @@ public void bigNumberOperations() {
659664
660665 /**
661666 * JSONObject put(String, Object) method stores and serializes
662- * bigInt and bigDec correctly. Nothing needs to change.
667+ * bigInt and bigDec correctly. Nothing needs to change.
668+ * TODO: New methods
669+ * get|optBigInteger|BigDecimal() should work like other supported
670+ * objects. Uncomment the get/opt methods after JSONObject is updated.
663671 */
664672 jsonObject = new JSONObject ();
665673 jsonObject .put ("bigInt" , bigInteger );
666674 assertTrue ("jsonObject.put() handles bigInt correctly" ,
667675 jsonObject .get ("bigInt" ).equals (bigInteger ));
676+ // assertTrue("jsonObject.getBigInteger() handles bigInt correctly",
677+ // jsonObject.getBigInteger("bigInt").equals(bigInteger));
678+ // assertTrue("jsonObject.optBigInteger() handles bigInt correctly",
679+ // jsonObject.optBigInteger("bigInt", BigInteger.ONE).equals(bigInteger));
668680 assertTrue ("jsonObject serializes bigInt correctly" ,
669681 jsonObject .toString ().equals ("{\" bigInt\" :123456789012345678901234567890}" ));
670682 jsonObject = new JSONObject ();
671683 jsonObject .put ("bigDec" , bigDecimal );
672684 assertTrue ("jsonObject.put() handles bigDec correctly" ,
673685 jsonObject .get ("bigDec" ).equals (bigDecimal ));
686+ // assertTrue("jsonObject.getBigDecimal() handles bigDec correctly",
687+ // jsonObject.getBigDecimal("bigDec").equals(bigDecimal));
688+ // assertTrue("jsonObject.optBigDecimal() handles bigDec correctly",
689+ // jsonObject.optBigDecimal("bigDec", BigDecimal.ONE).equals(bigDecimal));
674690 assertTrue ("jsonObject serializes bigDec correctly" ,
675691 jsonObject .toString ().equals (
676- "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
677-
678- /**
679- * There is no way to get bigInt or bigDec by type.
680- * This should be fixed. E.G. jsonObject.getBigInteger(key);
681- */
692+ "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
682693
683694 /**
684695 * JSONObject.numberToString() works correctly, nothing to change.
@@ -704,16 +715,79 @@ public void bigNumberOperations() {
704715 !obj .toString ().equals (bigDecimal .toString ()));
705716
706717 /**
707- * JSONObject. wrap() performs the advertised behavior,
708- * which is to turn Java classes into strings .
709- * Probably not a bug
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.
710721 */
722+ // bigInt map ctor
723+ Map <String , Object > map = new HashMap <String , Object >();
724+ map .put ("bigInt" , bigInteger );
725+ jsonObject = new JSONObject (map );
726+ String actualFromMapStr = jsonObject .toString ();
727+ assertTrue ("bigInt in map (or array or bean) is a string" ,
728+ actualFromMapStr .equals (
729+ "{\" bigInt\" :\" 123456789012345678901234567890\" }" ));
730+ // bigInt put
731+ jsonObject = new JSONObject ();
732+ jsonObject .put ("bigInt" , bigInteger );
733+ String actualFromPutStr = jsonObject .toString ();
734+ assertTrue ("bigInt from put is a number" ,
735+ actualFromPutStr .equals (
736+ "{\" bigInt\" :123456789012345678901234567890}" ));
737+ // bigDec map ctor
738+ map = new HashMap <String , Object >();
739+ map .put ("bigDec" , bigDecimal );
740+ jsonObject = new JSONObject (map );
741+ actualFromMapStr = jsonObject .toString ();
742+ assertTrue ("bigDec in map (or array or bean) is a string" ,
743+ actualFromMapStr .equals (
744+ "{\" bigDec\" :\" 123456789012345678901234567890.12345678901234567890123456789\" }" ));
745+ // bigDec put
746+ jsonObject = new JSONObject ();
747+ jsonObject .put ("bigDec" , bigDecimal );
748+ actualFromPutStr = jsonObject .toString ();
749+ assertTrue ("bigDec from put is a number" ,
750+ actualFromPutStr .equals (
751+ "{\" bigDec\" :123456789012345678901234567890.12345678901234567890123456789}" ));
752+ // bigInt,bigDec put
753+ JSONArray jsonArray = new JSONArray ();
754+ jsonArray .put (bigInteger );
755+ jsonArray .put (bigDecimal );
756+ actualFromPutStr = jsonArray .toString ();
757+ assertTrue ("bigInt, bigDec from put is a number" ,
758+ actualFromPutStr .equals (
759+ "[123456789012345678901234567890,123456789012345678901234567890.12345678901234567890123456789]" ));
760+ // bigInt,bigDec list ctor
761+ List <Object > list = new ArrayList <Object >();
762+ list .add (bigInteger );
763+ list .add (bigDecimal );
764+ jsonArray = new JSONArray (list );
765+ String actualFromListStr = jsonArray .toString ();
766+ assertTrue ("bigInt, bigDec in list is a string" ,
767+ actualFromListStr .equals (
768+ "[\" 123456789012345678901234567890\" ,\" 123456789012345678901234567890.12345678901234567890123456789\" ]" ));
769+ // bigInt bean ctor
770+ MyBigNumberBean myBigNumberBean = mock (MyBigNumberBean .class );
771+ when (myBigNumberBean .getBigInteger ()).thenReturn (new BigInteger ("123456789012345678901234567890" ));
772+ jsonObject = new JSONObject (myBigNumberBean );
773+ String actualFromBeanStr = jsonObject .toString ();
774+ // 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\" " ));
777+ // bigDec bean ctor
778+ myBigNumberBean = mock (MyBigNumberBean .class );
779+ when (myBigNumberBean .getBigDecimal ()).thenReturn (new BigDecimal ("123456789012345678901234567890.12345678901234567890123456789" ));
780+ jsonObject = new JSONObject (myBigNumberBean );
781+ actualFromBeanStr = jsonObject .toString ();
782+ // 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\" " ));
785+ // bigInt,bigDec wrap()
711786 obj = JSONObject .wrap (bigInteger );
712- assertTrue ("wrap() turns bigInt into a string" ,
713- obj .equals (bigInteger .toString ()));
787+ assertTrue ("wrap() returns string" ,obj .equals (bigInteger .toString ()));
714788 obj = JSONObject .wrap (bigDecimal );
715- assertTrue ("wrap() turns bigDec into a string" ,
716- obj . equals ( bigDecimal . toString ()));
789+ assertTrue ("wrap() returns string" ,obj . equals ( bigDecimal . toString ()));
790+
717791 }
718792
719793 /**
@@ -1133,8 +1207,8 @@ public void wrapObject() {
11331207 * is recognized as being a Java package class.
11341208 */
11351209 Object bdWrap = JSONObject .wrap (BigDecimal .ONE );
1136- assertTrue ("BigDecimal.ONE currently evaluates to string" ,
1137- bdWrap .equals ("1" ));
1210+ assertTrue ("BigDecimal.ONE evaluates to string" ,
1211+ bdWrap .equals (BigDecimal . ONE . toString () ));
11381212
11391213 // wrap JSONObject returns JSONObject
11401214 String jsonObjectStr =
0 commit comments