Skip to content

Commit 8982888

Browse files
Sean LearySean Leary
authored andcommitted
add unit tests to clarify current behavior for JSONObject and XML
1 parent 771c82c commit 8982888

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

src/test/java/org/json/junit/XMLTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,21 @@ public void testWithWhitespaceTrimmingEnabledByDefault() {
13971397
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
13981398
}
13991399

1400+
@Test
1401+
public void clarifyCurrentBehavior() {
1402+
1403+
// Behavior documented in #852
1404+
// After reverting the code, value is still stored as a number. This is due to how XML.isDecimalNotation() works
1405+
// and is probably a bug. JSONObject has a similar problem.
1406+
String str = "<color> <color_type>primary</color_type> <value>008E97</value> </color>";
1407+
JSONObject jsonObject = XML.toJSONObject(str);
1408+
assertEquals(jsonObject.getJSONObject("color").getLong("value"), 0e897, .1);
1409+
1410+
// Workaround for now is to use keepStrings
1411+
JSONObject jsonObject1 = XML.toJSONObject(str, new XMLParserConfiguration().withKeepStrings(true));
1412+
assertEquals(jsonObject1.getJSONObject("color").getString("value"), "008E97");
1413+
}
1414+
14001415
}
14011416

14021417

0 commit comments

Comments
 (0)