Skip to content

Commit c5173e7

Browse files
committed
ip
1 parent 9a9973c commit c5173e7

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

JSONObjectTest.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,90 @@ public void unexpectedDoubleToIntConversion() {
631631
assertTrue("3.1 remains a double", deserialized.getDouble(key31) == 3.1);
632632
}
633633

634+
@Test
635+
public void bigNumberOperations() {
636+
/**
637+
* JSONObject tries to parse BigInteger as a bean, but it only has
638+
* one getter, getLowestBitSet(). The value is lost and an unhelpful
639+
* value is stored. This should be fixed.
640+
*/
641+
BigInteger bigInteger = new BigInteger("123456789012345678901234567890");
642+
JSONObject jsonObject = new JSONObject(bigInteger);
643+
Object obj = jsonObject.get("lowestSetBit");
644+
assertTrue("JSONObject only has 1 value", jsonObject.length() == 1);
645+
assertTrue("JSONObject parses BigInteger as the Integer lowestBitSet",
646+
obj instanceof Integer);
647+
assertTrue("this bigInteger lowestBitSet happens to be 1",
648+
obj.equals(1));
649+
650+
/**
651+
* JSONObject put(String, Object) method stores and serializesbigInt and bigDec
652+
* correctly. Nothing needs to change.
653+
*/
654+
BigDecimal bigDecimal = new BigDecimal(
655+
"123456789012345678901234567890.12345678901234567890123456789");
656+
jsonObject = new JSONObject(bigDecimal);
657+
assertTrue("large bigDecimal is not stored", jsonObject.length() == 0);
658+
659+
/**
660+
* JSONObject put(String, Object) method stores and serializes
661+
* bigInt and bigDec correctly. Nothing needs to change.
662+
*/
663+
jsonObject = new JSONObject();
664+
jsonObject.put("bigInt", bigInteger);
665+
assertTrue("jsonObject.put() handles bigInt correctly",
666+
jsonObject.get("bigInt").equals(bigInteger));
667+
assertTrue("jsonObject serializes bigInt correctly",
668+
jsonObject.toString().equals("{\"bigInt\":123456789012345678901234567890}"));
669+
jsonObject = new JSONObject();
670+
jsonObject.put("bigDec", bigDecimal);
671+
assertTrue("jsonObject.put() handles bigDec correctly",
672+
jsonObject.get("bigDec").equals(bigDecimal));
673+
assertTrue("jsonObject serializes bigDec correctly",
674+
jsonObject.toString().equals(
675+
"{\"bigDec\":123456789012345678901234567890.12345678901234567890123456789}"));
676+
677+
/**
678+
* There is no way to get bigInt or bigDec by type.
679+
* This should be fixed. E.G. jsonObject.getBigInteger(key);
680+
*/
681+
682+
/**
683+
* JSONObject.numberToString() works correctly, nothing to change.
684+
*/
685+
String str = JSONObject.numberToString(bigInteger);
686+
assertTrue("numberToString() handles bigInteger correctly",
687+
str.equals("123456789012345678901234567890"));
688+
str = JSONObject.numberToString(bigDecimal);
689+
assertTrue("numberToString() handles bigDecimal correctly",
690+
str.equals("123456789012345678901234567890.12345678901234567890123456789"));
691+
692+
/**
693+
* JSONObject.stringToValue() turns bigInt into an accurate string,
694+
* and rounds bigDec. This incorrect, but users may have come to
695+
* expect this behavior. Change would be marginally better, but
696+
* might inconvenience users.
697+
*/
698+
obj = JSONObject.stringToValue(bigInteger.toString());
699+
assertTrue("stringToValue() turns bigInteger string into string",
700+
obj instanceof String);
701+
obj = JSONObject.stringToValue(bigDecimal.toString());
702+
assertTrue("stringToValue() changes bigDecimal string",
703+
!obj.toString().equals(bigDecimal.toString()));
704+
705+
/**
706+
* JSONObject.wrap() performs the advertised behavior,
707+
* which is to turn Java classes into strings.
708+
* Probably not a bug
709+
*/
710+
obj = JSONObject.wrap(bigInteger);
711+
assertTrue("wrap() turns bigInt into a string",
712+
obj.equals(bigInteger.toString()));
713+
obj = JSONObject.wrap(bigDecimal);
714+
assertTrue("wrap() turns bigDec into a string",
715+
obj.equals(bigDecimal.toString()));
716+
}
717+
634718
/**
635719
* The purpose for the static method getNames() methods are not clear.
636720
* This method is not called from within JSON-Java. Most likely

0 commit comments

Comments
 (0)