@@ -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" );
@@ -1147,7 +1147,7 @@ public void testEmptyTagForceList() {
11471147 @ Test
11481148 public void testForceListWithLastElementAsEmptyTag (){
11491149 final String originalXml = "<root><id>1</id><id/></root>" ;
1150- final String expectedJsonString = "{\" root\" :{\" id\" :[1]}}" ;
1150+ final String expectedJsonString = "{\" root\" :{\" id\" :[1, \" \" ]}}" ;
11511151
11521152 HashSet <String > forceListCandidates = new HashSet <>();
11531153 forceListCandidates .add ("id" );
@@ -1163,7 +1163,7 @@ public void testForceListWithLastElementAsEmptyTag(){
11631163 @ Test
11641164 public void testForceListWithFirstElementAsEmptyTag (){
11651165 final String originalXml = "<root><id/><id>1</id></root>" ;
1166- final String expectedJsonString = "{\" root\" :{\" id\" :[1]}}" ;
1166+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" , 1]}}" ;
11671167
11681168 HashSet <String > forceListCandidates = new HashSet <>();
11691169 forceListCandidates .add ("id" );
@@ -1179,7 +1179,7 @@ public void testForceListWithFirstElementAsEmptyTag(){
11791179 @ Test
11801180 public void testForceListWithMiddleElementAsEmptyTag (){
11811181 final String originalXml = "<root><id>1</id><id/><id>2</id></root>" ;
1182- final String expectedJsonString = "{\" root\" :{\" id\" :[1,2]}}" ;
1182+ final String expectedJsonString = "{\" root\" :{\" id\" :[1,\" \" , 2]}}" ;
11831183
11841184 HashSet <String > forceListCandidates = new HashSet <>();
11851185 forceListCandidates .add ("id" );
@@ -1195,8 +1195,7 @@ public void testForceListWithMiddleElementAsEmptyTag(){
11951195 @ Test
11961196 public void testForceListWithLastElementAsEmpty (){
11971197 final String originalXml = "<root><id>1</id><id></id></root>" ;
1198- final String expectedJsonString = "{\" root\" :{\" id\" :[1]}}" ;
1199-
1198+ final String expectedJsonString = "{\" root\" :{\" id\" :[1,\" \" ]}}" ;
12001199 HashSet <String > forceListCandidates = new HashSet <>();
12011200 forceListCandidates .add ("id" );
12021201 final JSONObject json = XML .toJSONObject (originalXml ,
@@ -1210,7 +1209,7 @@ public void testForceListWithLastElementAsEmpty(){
12101209 @ Test
12111210 public void testForceListWithFirstElementAsEmpty (){
12121211 final String originalXml = "<root><id></id><id>1</id></root>" ;
1213- final String expectedJsonString = "{\" root\" :{\" id\" :[1]}}" ;
1212+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" , 1]}}" ;
12141213
12151214 HashSet <String > forceListCandidates = new HashSet <>();
12161215 forceListCandidates .add ("id" );
@@ -1225,7 +1224,7 @@ public void testForceListWithFirstElementAsEmpty(){
12251224 @ Test
12261225 public void testForceListWithMiddleElementAsEmpty (){
12271226 final String originalXml = "<root><id>1</id><id></id><id>2</id></root>" ;
1228- final String expectedJsonString = "{\" root\" :{\" id\" :[1,2]}}" ;
1227+ final String expectedJsonString = "{\" root\" :{\" id\" :[1,\" \" , 2]}}" ;
12291228
12301229 HashSet <String > forceListCandidates = new HashSet <>();
12311230 forceListCandidates .add ("id" );
@@ -1240,7 +1239,7 @@ public void testForceListWithMiddleElementAsEmpty(){
12401239 @ Test
12411240 public void testForceListEmptyAndEmptyTagsMixed (){
12421241 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]}}" ;
1242+ final String expectedJsonString = "{\" root\" :{\" id\" :[\" \" , \" \" ,1, \" \" , \" \" ,2]}}" ;
12441243
12451244 HashSet <String > forceListCandidates = new HashSet <>();
12461245 forceListCandidates .add ("id" );
@@ -1252,6 +1251,50 @@ public void testForceListEmptyAndEmptyTagsMixed(){
12521251 assertEquals (expectedJsonString , json .toString ());
12531252 }
12541253
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+
12551298 @ Test
12561299 public void testMaxNestingDepthIsSet () {
12571300 XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration .ORIGINAL ;
0 commit comments