Skip to content

Commit 5dd78bc

Browse files
author
Zach
committed
Test case passed with tags with multiple entries set forceList
1 parent e638955 commit 5dd78bc

2 files changed

Lines changed: 94 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,23 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
380380
if (x.nextToken() != GT) {
381381
throw x.syntaxError("Misshaped tag");
382382
}
383-
if (nilAttributeFound) {
384-
context.accumulate(tagName, JSONObject.NULL);
385-
} else if (jsonObject.length() > 0) {
386-
context.accumulate(tagName, jsonObject);
383+
if (config.getForceList().contains(tagName)) {
384+
// Force the value to be an array
385+
if (nilAttributeFound) {
386+
context.append(tagName, JSONObject.NULL);
387+
} else if (jsonObject.length() > 0) {
388+
context.append(tagName, jsonObject);
389+
} else {
390+
context.put(tagName, new JSONArray());
391+
}
387392
} else {
388-
context.accumulate(tagName, "");
393+
if (nilAttributeFound) {
394+
context.accumulate(tagName, JSONObject.NULL);
395+
} else if (jsonObject.length() > 0) {
396+
context.accumulate(tagName, jsonObject);
397+
} else {
398+
context.accumulate(tagName, "");
399+
}
389400
}
390401
return false;
391402

@@ -414,6 +425,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
414425
// Nested element
415426
if (parse(x, jsonObject, tagName, config)) {
416427
if (config.getForceList().contains(tagName)) {
428+
// Force the value to be an array
417429
if (jsonObject.length() == 0) {
418430
context.put(tagName, new JSONArray());
419431
} else if (jsonObject.length() == 1

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,45 @@ public void testLongForceList() {
976976
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
977977
}
978978
@Test
979+
public void testMultipleTagForceList() {
980+
String xmlStr =
981+
"<addresses>\n"+
982+
" <address>\n"+
983+
" <name>Sherlock Holmes</name>\n"+
984+
" <name>John H. Watson</name>\n"+
985+
" </address>\n"+
986+
"</addresses>";
987+
988+
String expectedStr =
989+
"{"+
990+
"\"addresses\":["+
991+
"{"+
992+
"\"address\":["+
993+
"{"+
994+
"\"name\":["+
995+
"\"Sherlock Holmes\","+
996+
"\"John H. Watson\""+
997+
"]"+
998+
"}"+
999+
"]"+
1000+
"}"+
1001+
"]"+
1002+
"}";
1003+
1004+
Set<String> forceList = new HashSet<String>();
1005+
forceList.add("addresses");
1006+
forceList.add("address");
1007+
forceList.add("name");
1008+
1009+
XMLParserConfiguration config =
1010+
new XMLParserConfiguration()
1011+
.withForceList(forceList);
1012+
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
1013+
JSONObject expetedJsonObject = new JSONObject(expectedStr);
1014+
1015+
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
1016+
}
1017+
@Test
9791018
public void testEmptyForceList() {
9801019
String xmlStr =
9811020
"<addresses></addresses>";
@@ -994,6 +1033,44 @@ public void testEmptyForceList() {
9941033

9951034
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
9961035
}
1036+
@Test
1037+
public void testContentForceList() {
1038+
String xmlStr =
1039+
"<addresses>Baker Street</addresses>";
1040+
1041+
String expectedStr =
1042+
"{\"addresses\":[\"Baker Street\"]}";
1043+
1044+
Set<String> forceList = new HashSet<String>();
1045+
forceList.add("addresses");
1046+
1047+
XMLParserConfiguration config =
1048+
new XMLParserConfiguration()
1049+
.withForceList(forceList);
1050+
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
1051+
JSONObject expetedJsonObject = new JSONObject(expectedStr);
1052+
1053+
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
1054+
}
1055+
@Test
1056+
public void testEmptyTagForceList() {
1057+
String xmlStr =
1058+
"<addresses />";
1059+
1060+
String expectedStr =
1061+
"{\"addresses\":[]}";
1062+
1063+
Set<String> forceList = new HashSet<String>();
1064+
forceList.add("addresses");
1065+
1066+
XMLParserConfiguration config =
1067+
new XMLParserConfiguration()
1068+
.withForceList(forceList);
1069+
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
1070+
JSONObject expetedJsonObject = new JSONObject(expectedStr);
1071+
1072+
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
1073+
}
9971074

9981075
/**
9991076
* Convenience method, given an input string and expected result,

0 commit comments

Comments
 (0)