Skip to content

Commit 995fb84

Browse files
Fixes the issue of losing the array if an empty forceList element or a tag is in the middle or the end
1 parent e635f40 commit 995fb84

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
391391
context.append(tagName, JSONObject.NULL);
392392
} else if (jsonObject.length() > 0) {
393393
context.append(tagName, jsonObject);
394-
} else {
394+
} else if(context.isEmpty() && (context.opt(tagName) == null || !(context.get(tagName) instanceof JSONArray))) { //avoids resetting the array in case of an empty tag in the middle or end
395395
context.put(tagName, new JSONArray());
396396
}
397397
} else {
@@ -451,7 +451,10 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
451451
if (config.getForceList().contains(tagName)) {
452452
// Force the value to be an array
453453
if (jsonObject.length() == 0) {
454-
context.put(tagName, new JSONArray());
454+
//avoids resetting the array in case of an empty element in the middle or end
455+
if(context.length()==0 && context.opt(tagName) == null || !(context.get(tagName) instanceof JSONArray)) {
456+
context.put(tagName, new JSONArray());
457+
}
455458
} else if (jsonObject.length() == 1
456459
&& jsonObject.opt(config.getcDataTagName()) != null) {
457460
context.append(tagName, jsonObject.opt(config.getcDataTagName()));

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,114 @@ public void testEmptyTagForceList() {
11441144
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
11451145
}
11461146

1147+
@Test
1148+
public void testForceListWithLastElementAsEmptyTag(){
1149+
final String originalXml = "<root><id>1</id><id/></root>";
1150+
final String expectedJsonString = "{\"root\":{\"id\":[1]}}";
1151+
1152+
HashSet<String> forceListCandidates = new HashSet<>();
1153+
forceListCandidates.add("id");
1154+
final JSONObject json = XML.toJSONObject(originalXml,
1155+
new XMLParserConfiguration()
1156+
.withKeepStrings(false)
1157+
.withcDataTagName("content")
1158+
.withForceList(forceListCandidates)
1159+
.withConvertNilAttributeToNull(true));
1160+
assertEquals(expectedJsonString, json.toString());
1161+
}
1162+
1163+
@Test
1164+
public void testForceListWithFirstElementAsEmptyTag(){
1165+
final String originalXml = "<root><id/><id>1</id></root>";
1166+
final String expectedJsonString = "{\"root\":{\"id\":[1]}}";
1167+
1168+
HashSet<String> forceListCandidates = new HashSet<>();
1169+
forceListCandidates.add("id");
1170+
final JSONObject json = XML.toJSONObject(originalXml,
1171+
new XMLParserConfiguration()
1172+
.withKeepStrings(false)
1173+
.withcDataTagName("content")
1174+
.withForceList(forceListCandidates)
1175+
.withConvertNilAttributeToNull(true));
1176+
assertEquals(expectedJsonString, json.toString());
1177+
}
1178+
1179+
@Test
1180+
public void testForceListWithMiddleElementAsEmptyTag(){
1181+
final String originalXml = "<root><id>1</id><id/><id>2</id></root>";
1182+
final String expectedJsonString = "{\"root\":{\"id\":[1,2]}}";
1183+
1184+
HashSet<String> forceListCandidates = new HashSet<>();
1185+
forceListCandidates.add("id");
1186+
final JSONObject json = XML.toJSONObject(originalXml,
1187+
new XMLParserConfiguration()
1188+
.withKeepStrings(false)
1189+
.withcDataTagName("content")
1190+
.withForceList(forceListCandidates)
1191+
.withConvertNilAttributeToNull(true));
1192+
assertEquals(expectedJsonString, json.toString());
1193+
}
1194+
1195+
@Test
1196+
public void testForceListWithLastElementAsEmpty(){
1197+
final String originalXml = "<root><id>1</id><id></id></root>";
1198+
final String expectedJsonString = "{\"root\":{\"id\":[1]}}";
1199+
1200+
HashSet<String> forceListCandidates = new HashSet<>();
1201+
forceListCandidates.add("id");
1202+
final JSONObject json = XML.toJSONObject(originalXml,
1203+
new XMLParserConfiguration()
1204+
.withKeepStrings(false)
1205+
.withForceList(forceListCandidates)
1206+
.withConvertNilAttributeToNull(true));
1207+
assertEquals(expectedJsonString, json.toString());
1208+
}
1209+
1210+
@Test
1211+
public void testForceListWithFirstElementAsEmpty(){
1212+
final String originalXml = "<root><id></id><id>1</id></root>";
1213+
final String expectedJsonString = "{\"root\":{\"id\":[1]}}";
1214+
1215+
HashSet<String> forceListCandidates = new HashSet<>();
1216+
forceListCandidates.add("id");
1217+
final JSONObject json = XML.toJSONObject(originalXml,
1218+
new XMLParserConfiguration()
1219+
.withKeepStrings(false)
1220+
.withForceList(forceListCandidates)
1221+
.withConvertNilAttributeToNull(true));
1222+
assertEquals(expectedJsonString, json.toString());
1223+
}
1224+
1225+
@Test
1226+
public void testForceListWithMiddleElementAsEmpty(){
1227+
final String originalXml = "<root><id>1</id><id></id><id>2</id></root>";
1228+
final String expectedJsonString = "{\"root\":{\"id\":[1,2]}}";
1229+
1230+
HashSet<String> forceListCandidates = new HashSet<>();
1231+
forceListCandidates.add("id");
1232+
final JSONObject json = XML.toJSONObject(originalXml,
1233+
new XMLParserConfiguration()
1234+
.withKeepStrings(false)
1235+
.withForceList(forceListCandidates)
1236+
.withConvertNilAttributeToNull(true));
1237+
assertEquals(expectedJsonString, json.toString());
1238+
}
1239+
1240+
@Test
1241+
public void testForceListEmptyAndEmptyTagsMixed(){
1242+
final String originalXml = "<root><id></id><id/><id>1</id><id/><id></id><id>2</id></root>";
1243+
final String expectedJsonString = "{\"root\":{\"id\":[1,2]}}";
1244+
1245+
HashSet<String> forceListCandidates = new HashSet<>();
1246+
forceListCandidates.add("id");
1247+
final JSONObject json = XML.toJSONObject(originalXml,
1248+
new XMLParserConfiguration()
1249+
.withKeepStrings(false)
1250+
.withForceList(forceListCandidates)
1251+
.withConvertNilAttributeToNull(true));
1252+
assertEquals(expectedJsonString, json.toString());
1253+
}
1254+
11471255
@Test
11481256
public void testMaxNestingDepthIsSet() {
11491257
XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration.ORIGINAL;

0 commit comments

Comments
 (0)