Skip to content

Commit e638955

Browse files
author
Zach
committed
Pass test case for empty tag with forceList
1 parent 3f9b53f commit e638955

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

src/main/java/org/json/XML.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
415415
if (parse(x, jsonObject, tagName, config)) {
416416
if (config.getForceList().contains(tagName)) {
417417
if (jsonObject.length() == 0) {
418-
context.append(tagName, "");
418+
context.put(tagName, new JSONArray());
419419
} else if (jsonObject.length() == 1
420420
&& jsonObject.opt(config.getcDataTagName()) != null) {
421421
context.append(tagName, jsonObject.opt(config.getcDataTagName()));

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ public void testSimpleForceList() {
919919
" </address>\n"+
920920
"</addresses>";
921921

922-
String expectedStr = "{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}";
922+
String expectedStr =
923+
"{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}";
923924

924925
Set<String> forceList = new HashSet<String>();
925926
forceList.add("addresses");
@@ -949,18 +950,18 @@ public void testLongForceList() {
949950
"</servers>";
950951

951952
String expectedStr =
952-
"{"+
953-
"\"servers\": ["+
954-
"{"+
955-
"\"server\": {"+
956-
"\"name\": \"host1\","+
957-
"\"os\": \"Linux\","+
958-
"\"interfaces\": ["+
959-
"{"+
960-
"\"interface\": {"+
961-
"\"name\": \"em0\","+
962-
"\"ip_address\": \"10.0.0.1\""+
963-
"}}]}}]}";
953+
"{"+
954+
"\"servers\": ["+
955+
"{"+
956+
"\"server\": {"+
957+
"\"name\": \"host1\","+
958+
"\"os\": \"Linux\","+
959+
"\"interfaces\": ["+
960+
"{"+
961+
"\"interface\": {"+
962+
"\"name\": \"em0\","+
963+
"\"ip_address\": \"10.0.0.1\""+
964+
"}}]}}]}";
964965

965966
Set<String> forceList = new HashSet<String>();
966967
forceList.add("servers");
@@ -974,7 +975,25 @@ public void testLongForceList() {
974975

975976
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
976977
}
977-
978+
@Test
979+
public void testEmptyForceList() {
980+
String xmlStr =
981+
"<addresses></addresses>";
982+
983+
String expectedStr =
984+
"{\"addresses\":[]}";
985+
986+
Set<String> forceList = new HashSet<String>();
987+
forceList.add("addresses");
988+
989+
XMLParserConfiguration config =
990+
new XMLParserConfiguration()
991+
.withForceList(forceList);
992+
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
993+
JSONObject expetedJsonObject = new JSONObject(expectedStr);
994+
995+
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
996+
}
978997

979998
/**
980999
* Convenience method, given an input string and expected result,

0 commit comments

Comments
 (0)