Pass test case for empty tag with forceList

This commit is contained in:
Zach 2021-11-19 21:05:05 -06:00
parent 3f9b53fee4
commit e638955034
2 changed files with 34 additions and 15 deletions

View File

@ -415,7 +415,7 @@ public class XML {
if (parse(x, jsonObject, tagName, config)) {
if (config.getForceList().contains(tagName)) {
if (jsonObject.length() == 0) {
context.append(tagName, "");
context.put(tagName, new JSONArray());
} else if (jsonObject.length() == 1
&& jsonObject.opt(config.getcDataTagName()) != null) {
context.append(tagName, jsonObject.opt(config.getcDataTagName()));

View File

@ -919,7 +919,8 @@ public class XMLConfigurationTest {
" </address>\n"+
"</addresses>";
String expectedStr = "{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}";
String expectedStr =
"{\"addresses\":[{\"address\":{\"name\":\"Sherlock Holmes\"}}]}";
Set<String> forceList = new HashSet<String>();
forceList.add("addresses");
@ -949,18 +950,18 @@ public class XMLConfigurationTest {
"</servers>";
String expectedStr =
"{"+
"\"servers\": ["+
"{"+
"\"server\": {"+
"\"name\": \"host1\","+
"\"os\": \"Linux\","+
"\"interfaces\": ["+
"{"+
"\"interface\": {"+
"\"name\": \"em0\","+
"\"ip_address\": \"10.0.0.1\""+
"}}]}}]}";
"{"+
"\"servers\": ["+
"{"+
"\"server\": {"+
"\"name\": \"host1\","+
"\"os\": \"Linux\","+
"\"interfaces\": ["+
"{"+
"\"interface\": {"+
"\"name\": \"em0\","+
"\"ip_address\": \"10.0.0.1\""+
"}}]}}]}";
Set<String> forceList = new HashSet<String>();
forceList.add("servers");
@ -974,7 +975,25 @@ public class XMLConfigurationTest {
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
}
@Test
public void testEmptyForceList() {
String xmlStr =
"<addresses></addresses>";
String expectedStr =
"{\"addresses\":[]}";
Set<String> forceList = new HashSet<String>();
forceList.add("addresses");
XMLParserConfiguration config =
new XMLParserConfiguration()
.withForceList(forceList);
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
JSONObject expetedJsonObject = new JSONObject(expectedStr);
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
}
/**
* Convenience method, given an input string and expected result,