Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/org/json/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,13 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
context.append(tagName, JSONObject.NULL);
} else if (jsonObject.length() > 0) {
context.append(tagName, jsonObject);
} else {
} else if(context.isEmpty()) { //avoids resetting the array in case of an empty tag in the middle or end
context.put(tagName, new JSONArray());
if (jsonObject.isEmpty()){
context.append(tagName, "");
}
} else {
context.append(tagName, "");
}
} else {
if (nilAttributeFound) {
Expand Down Expand Up @@ -451,7 +456,11 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
if (config.getForceList().contains(tagName)) {
// Force the value to be an array
if (jsonObject.length() == 0) {
context.put(tagName, new JSONArray());
//avoids resetting the array in case of an empty element in the middle or end
if(context.isEmpty()) {
context.put(tagName, new JSONArray());
}
context.append(tagName, "");
} else if (jsonObject.length() == 1
&& jsonObject.opt(config.getcDataTagName()) != null) {
context.append(tagName, jsonObject.opt(config.getcDataTagName()));
Expand Down
155 changes: 153 additions & 2 deletions src/test/java/org/json/junit/XMLConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ public void testEmptyForceList() {
"<addresses></addresses>";

String expectedStr =
"{\"addresses\":[]}";
"{\"addresses\":[\"\"]}";

Set<String> forceList = new HashSet<String>();
forceList.add("addresses");
Expand Down Expand Up @@ -1130,7 +1130,7 @@ public void testEmptyTagForceList() {
"<addresses />";

String expectedStr =
"{\"addresses\":[]}";
"{\"addresses\":[\"\"]}";

Set<String> forceList = new HashSet<String>();
forceList.add("addresses");
Expand All @@ -1144,6 +1144,157 @@ public void testEmptyTagForceList() {
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
}

@Test
public void testForceListWithLastElementAsEmptyTag(){
final String originalXml = "<root><id>1</id><id/></root>";
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\"]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withcDataTagName("content")
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListWithFirstElementAsEmptyTag(){
final String originalXml = "<root><id/><id>1</id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[\"\",1]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withcDataTagName("content")
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListWithMiddleElementAsEmptyTag(){
final String originalXml = "<root><id>1</id><id/><id>2</id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\",2]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withcDataTagName("content")
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListWithLastElementAsEmpty(){
final String originalXml = "<root><id>1</id><id></id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\"]}}";
HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListWithFirstElementAsEmpty(){
final String originalXml = "<root><id></id><id>1</id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[\"\",1]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListWithMiddleElementAsEmpty(){
final String originalXml = "<root><id>1</id><id></id><id>2</id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\",2]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListEmptyAndEmptyTagsMixed(){
final String originalXml = "<root><id></id><id/><id>1</id><id/><id></id><id>2</id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[\"\",\"\",1,\"\",\"\",2]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
final JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withKeepStrings(false)
.withForceList(forceListCandidates)
.withConvertNilAttributeToNull(true));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListConsistencyWithDefault() {
final String originalXml = "<root><id>0</id><id>1</id><id/><id></id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[0,1,\"\",\"\"]}}";

// confirm expected result of default array-of-tags processing
JSONObject json = XML.toJSONObject(originalXml);
assertEquals(expectedJsonString, json.toString());

// confirm forceList array-of-tags processing is consistent with default processing
HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withForceList(forceListCandidates));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListInitializesAnArrayWithAnEmptyElement(){
final String originalXml = "<root><id></id></root>";
final String expectedJsonString = "{\"root\":{\"id\":[\"\"]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withForceList(forceListCandidates));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testForceListInitializesAnArrayWithAnEmptyTag(){
final String originalXml = "<root><id/></root>";
final String expectedJsonString = "{\"root\":{\"id\":[\"\"]}}";

HashSet<String> forceListCandidates = new HashSet<>();
forceListCandidates.add("id");
JSONObject json = XML.toJSONObject(originalXml,
new XMLParserConfiguration()
.withForceList(forceListCandidates));
assertEquals(expectedJsonString, json.toString());
}

@Test
public void testMaxNestingDepthIsSet() {
XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration.ORIGINAL;
Expand Down
Loading