@@ -1323,6 +1323,80 @@ public void testMaxNestingDepthWithValidFittingXML() {
13231323 "parameter of the XMLParserConfiguration used" );
13241324 }
13251325 }
1326+ @ Test
1327+ public void testWithWhitespaceTrimmingDisabled () {
1328+ String originalXml = "<testXml> Test Whitespace String \t </testXml>" ;
1329+
1330+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withShouldTrimWhitespace (false ));
1331+ String expectedJsonString = "{\" testXml\" :\" Test Whitespace String \t \" }" ;
1332+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1333+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1334+ }
1335+ @ Test
1336+ public void testNestedWithWhitespaceTrimmingDisabled () {
1337+ String originalXml =
1338+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
1339+ "<addresses>\n " +
1340+ " <address>\n " +
1341+ " <name> Sherlock Holmes </name>\n " +
1342+ " </address>\n " +
1343+ "</addresses>" ;
1344+
1345+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withShouldTrimWhitespace (false ));
1346+ String expectedJsonString = "{\" addresses\" :{\" address\" :{\" name\" :\" Sherlock Holmes \" }}}" ;
1347+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1348+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1349+ }
1350+ @ Test
1351+ public void shouldTrimWhitespaceDoesNotSupportTagsEqualingCDataTagName () {
1352+ // When using withShouldTrimWhitespace = true, input containing tags with same name as cDataTagName is unsupported and should not be used in conjunction
1353+ String originalXml =
1354+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
1355+ "<addresses>\n " +
1356+ " <address>\n " +
1357+ " <content> Sherlock Holmes </content>\n " +
1358+ " </address>\n " +
1359+ "</addresses>" ;
1360+
1361+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withShouldTrimWhitespace (false ).withcDataTagName ("content" ));
1362+ String expectedJsonString = "{\" addresses\" :{\" address\" :[[\" \\ n \" ,\" Sherlock Holmes \" ,\" \\ n \" ]]}}" ;
1363+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1364+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1365+ }
1366+ @ Test
1367+ public void shouldTrimWhitespaceEnabledDropsTagsEqualingCDataTagNameButValueRemains () {
1368+ String originalXml =
1369+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
1370+ "<addresses>\n " +
1371+ " <address>\n " +
1372+ " <content> Sherlock Holmes </content>\n " +
1373+ " </address>\n " +
1374+ "</addresses>" ;
1375+
1376+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withShouldTrimWhitespace (true ).withcDataTagName ("content" ));
1377+ String expectedJsonString = "{\" addresses\" :{\" address\" :\" Sherlock Holmes\" }}" ;
1378+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1379+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1380+ }
1381+ @ Test
1382+ public void testWithWhitespaceTrimmingEnabled () {
1383+ String originalXml = "<testXml> Test Whitespace String \t </testXml>" ;
1384+
1385+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withShouldTrimWhitespace (true ));
1386+ String expectedJsonString = "{\" testXml\" :\" Test Whitespace String\" }" ;
1387+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1388+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1389+ }
1390+ @ Test
1391+ public void testWithWhitespaceTrimmingEnabledByDefault () {
1392+ String originalXml = "<testXml> Test Whitespace String \t </testXml>" ;
1393+
1394+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ());
1395+ String expectedJsonString = "{\" testXml\" :\" Test Whitespace String\" }" ;
1396+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1397+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1398+ }
1399+
13261400}
13271401
13281402
0 commit comments