@@ -1092,7 +1092,7 @@ public void testEmptyForceList() {
10921092 "<addresses></addresses>" ;
10931093
10941094 String expectedStr =
1095- "{\" addresses\" :[]}" ;
1095+ "{\" addresses\" :[\" \" ]}" ;
10961096
10971097 Set <String > forceList = new HashSet <String >();
10981098 forceList .add ("addresses" );
@@ -1130,7 +1130,7 @@ public void testEmptyTagForceList() {
11301130 "<addresses />" ;
11311131
11321132 String expectedStr =
1133- "{\" addresses\" :[]}" ;
1133+ "{\" addresses\" :[\" \" ]}" ;
11341134
11351135 Set <String > forceList = new HashSet <String >();
11361136 forceList .add ("addresses" );
@@ -1144,6 +1144,157 @@ 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+ HashSet <String > forceListCandidates = new HashSet <>();
1200+ forceListCandidates .add ("id" );
1201+ final JSONObject json = XML .toJSONObject (originalXml ,
1202+ new XMLParserConfiguration ()
1203+ .withKeepStrings (false )
1204+ .withForceList (forceListCandidates )
1205+ .withConvertNilAttributeToNull (true ));
1206+ assertEquals (expectedJsonString , json .toString ());
1207+ }
1208+
1209+ @ Test
1210+ public void testForceListWithFirstElementAsEmpty (){
1211+ final String originalXml = "<root><id></id><id>1</id></root>" ;
1212+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" ,1]}}" ;
1213+
1214+ HashSet <String > forceListCandidates = new HashSet <>();
1215+ forceListCandidates .add ("id" );
1216+ final JSONObject json = XML .toJSONObject (originalXml ,
1217+ new XMLParserConfiguration ()
1218+ .withKeepStrings (false )
1219+ .withForceList (forceListCandidates )
1220+ .withConvertNilAttributeToNull (true ));
1221+ assertEquals (expectedJsonString , json .toString ());
1222+ }
1223+
1224+ @ Test
1225+ public void testForceListWithMiddleElementAsEmpty (){
1226+ final String originalXml = "<root><id>1</id><id></id><id>2</id></root>" ;
1227+ final String expectedJsonString = "{\" root\" :{\" id\" :[1,\" \" ,2]}}" ;
1228+
1229+ HashSet <String > forceListCandidates = new HashSet <>();
1230+ forceListCandidates .add ("id" );
1231+ final JSONObject json = XML .toJSONObject (originalXml ,
1232+ new XMLParserConfiguration ()
1233+ .withKeepStrings (false )
1234+ .withForceList (forceListCandidates )
1235+ .withConvertNilAttributeToNull (true ));
1236+ assertEquals (expectedJsonString , json .toString ());
1237+ }
1238+
1239+ @ Test
1240+ public void testForceListEmptyAndEmptyTagsMixed (){
1241+ final String originalXml = "<root><id></id><id/><id>1</id><id/><id></id><id>2</id></root>" ;
1242+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" ,\" \" ,1,\" \" ,\" \" ,2]}}" ;
1243+
1244+ HashSet <String > forceListCandidates = new HashSet <>();
1245+ forceListCandidates .add ("id" );
1246+ final JSONObject json = XML .toJSONObject (originalXml ,
1247+ new XMLParserConfiguration ()
1248+ .withKeepStrings (false )
1249+ .withForceList (forceListCandidates )
1250+ .withConvertNilAttributeToNull (true ));
1251+ assertEquals (expectedJsonString , json .toString ());
1252+ }
1253+
1254+ @ Test
1255+ public void testForceListConsistencyWithDefault () {
1256+ final String originalXml = "<root><id>0</id><id>1</id><id/><id></id></root>" ;
1257+ final String expectedJsonString = "{\" root\" :{\" id\" :[0,1,\" \" ,\" \" ]}}" ;
1258+
1259+ // confirm expected result of default array-of-tags processing
1260+ JSONObject json = XML .toJSONObject (originalXml );
1261+ assertEquals (expectedJsonString , json .toString ());
1262+
1263+ // confirm forceList array-of-tags processing is consistent with default processing
1264+ HashSet <String > forceListCandidates = new HashSet <>();
1265+ forceListCandidates .add ("id" );
1266+ json = XML .toJSONObject (originalXml ,
1267+ new XMLParserConfiguration ()
1268+ .withForceList (forceListCandidates ));
1269+ assertEquals (expectedJsonString , json .toString ());
1270+ }
1271+
1272+ @ Test
1273+ public void testForceListInitializesAnArrayWithAnEmptyElement (){
1274+ final String originalXml = "<root><id></id></root>" ;
1275+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" ]}}" ;
1276+
1277+ HashSet <String > forceListCandidates = new HashSet <>();
1278+ forceListCandidates .add ("id" );
1279+ JSONObject json = XML .toJSONObject (originalXml ,
1280+ new XMLParserConfiguration ()
1281+ .withForceList (forceListCandidates ));
1282+ assertEquals (expectedJsonString , json .toString ());
1283+ }
1284+
1285+ @ Test
1286+ public void testForceListInitializesAnArrayWithAnEmptyTag (){
1287+ final String originalXml = "<root><id/></root>" ;
1288+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" ]}}" ;
1289+
1290+ HashSet <String > forceListCandidates = new HashSet <>();
1291+ forceListCandidates .add ("id" );
1292+ JSONObject json = XML .toJSONObject (originalXml ,
1293+ new XMLParserConfiguration ()
1294+ .withForceList (forceListCandidates ));
1295+ assertEquals (expectedJsonString , json .toString ());
1296+ }
1297+
11471298 @ Test
11481299 public void testMaxNestingDepthIsSet () {
11491300 XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration .ORIGINAL ;
0 commit comments