@@ -3738,6 +3738,43 @@ public void testDifferentKeySameInstanceNotACircleReference() {
37383738 new JSONObject (map1 );
37393739 }
37403740
3741+ @ Test
3742+ public void clarifyCurrentBehavior () {
3743+ // Behavior documented in #653 optLong vs getLong inconsistencies
3744+ // This problem still exists.
3745+ // Internally, both number_1 and number_2 are stored as strings. This is reasonable since they are parsed as strings.
3746+ // However, getLong and optLong should return similar results
3747+ JSONObject json = new JSONObject ("{\" number_1\" :\" 01234\" , \" number_2\" : \" 332211\" }" );
3748+ assertEquals (json .getLong ("number_1" ), 1234L );
3749+ assertEquals (json .optLong ("number_1" ), 0 ); //THIS VALUE IS NOT RETURNED AS A NUMBER
3750+ assertEquals (json .getLong ("number_2" ), 332211L );
3751+ assertEquals (json .optLong ("number_2" ), 332211L );
3752+
3753+ // Behavior documented in #826 JSONObject parsing 0-led numeric strings as ints
3754+ // After reverting the code, personId is stored as a string, and the behavior is as expected
3755+ String personId = "0123" ;
3756+ JSONObject j1 = new JSONObject ("{personId: " + personId + "}" );
3757+ assertEquals (j1 .getString ("personId" ), "0123" );
3758+
3759+ // Also #826. Here is input with missing quotes. Because of the leading zero, it should not be parsed as a number.
3760+ // This example was mentioned in the same ticket
3761+ // After reverting the code, personId is stored as a string, and the behavior is as expected
3762+ JSONObject j2 = new JSONObject ("{\" personId\" :0123}" );
3763+ assertEquals (j2 .getString ("personId" ), "0123" );
3764+
3765+ // Behavior uncovered while working on the code
3766+ // All of the values are stored as strings except for hex4, which is stored as a number. This is probably incorrect
3767+ JSONObject j3 = new JSONObject ("{ " +
3768+ "\" hex1\" : \" 010e4\" , \" hex2\" : \" 00f0\" , \" hex3\" : \" 0011\" , " +
3769+ "\" hex4\" : 00e0, \" hex5\" : 00f0, \" hex6\" : 0011 }" );
3770+ assertEquals (j3 .getString ("hex1" ), "010e4" );
3771+ assertEquals (j3 .getString ("hex2" ), "00f0" );
3772+ assertEquals (j3 .getString ("hex3" ), "0011" );
3773+ assertEquals (j3 .getLong ("hex4" ), 0 , .1 );
3774+ assertEquals (j3 .getString ("hex5" ), "00f0" );
3775+ assertEquals (j3 .getString ("hex6" ), "0011" );
3776+ }
3777+
37413778 /**
37423779 * Method to build nested map of max maxDepth
37433780 *
0 commit comments