From 5dd78bc0b96eceb77f9ba113e2e5f7867053ee7e Mon Sep 17 00:00:00 2001 From: Zach Date: Fri, 19 Nov 2021 21:22:38 -0600 Subject: [PATCH] Test case passed with tags with multiple entries set forceList --- src/main/java/org/json/XML.java | 22 ++++-- .../org/json/junit/XMLConfigurationTest.java | 77 +++++++++++++++++++ 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index d78ae1f..9b2ba89 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -380,12 +380,23 @@ public class XML { if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } - if (nilAttributeFound) { - context.accumulate(tagName, JSONObject.NULL); - } else if (jsonObject.length() > 0) { - context.accumulate(tagName, jsonObject); + if (config.getForceList().contains(tagName)) { + // Force the value to be an array + if (nilAttributeFound) { + context.append(tagName, JSONObject.NULL); + } else if (jsonObject.length() > 0) { + context.append(tagName, jsonObject); + } else { + context.put(tagName, new JSONArray()); + } } else { - context.accumulate(tagName, ""); + if (nilAttributeFound) { + context.accumulate(tagName, JSONObject.NULL); + } else if (jsonObject.length() > 0) { + context.accumulate(tagName, jsonObject); + } else { + context.accumulate(tagName, ""); + } } return false; @@ -414,6 +425,7 @@ public class XML { // Nested element if (parse(x, jsonObject, tagName, config)) { if (config.getForceList().contains(tagName)) { + // Force the value to be an array if (jsonObject.length() == 0) { context.put(tagName, new JSONArray()); } else if (jsonObject.length() == 1 diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 3008024..2ab06be 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -976,6 +976,45 @@ public class XMLConfigurationTest { Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject); } @Test + public void testMultipleTagForceList() { + String xmlStr = + "\n"+ + "
\n"+ + " Sherlock Holmes\n"+ + " John H. Watson\n"+ + "
\n"+ + "
"; + + String expectedStr = + "{"+ + "\"addresses\":["+ + "{"+ + "\"address\":["+ + "{"+ + "\"name\":["+ + "\"Sherlock Holmes\","+ + "\"John H. Watson\""+ + "]"+ + "}"+ + "]"+ + "}"+ + "]"+ + "}"; + + Set forceList = new HashSet(); + forceList.add("addresses"); + forceList.add("address"); + forceList.add("name"); + + XMLParserConfiguration config = + new XMLParserConfiguration() + .withForceList(forceList); + JSONObject jsonObject = XML.toJSONObject(xmlStr, config); + JSONObject expetedJsonObject = new JSONObject(expectedStr); + + Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject); + } + @Test public void testEmptyForceList() { String xmlStr = ""; @@ -994,6 +1033,44 @@ public class XMLConfigurationTest { Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject); } + @Test + public void testContentForceList() { + String xmlStr = + "Baker Street"; + + String expectedStr = + "{\"addresses\":[\"Baker Street\"]}"; + + Set forceList = new HashSet(); + forceList.add("addresses"); + + XMLParserConfiguration config = + new XMLParserConfiguration() + .withForceList(forceList); + JSONObject jsonObject = XML.toJSONObject(xmlStr, config); + JSONObject expetedJsonObject = new JSONObject(expectedStr); + + Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject); + } + @Test + public void testEmptyTagForceList() { + String xmlStr = + ""; + + String expectedStr = + "{\"addresses\":[]}"; + + Set forceList = new HashSet(); + 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,