@@ -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