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