mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-08 03:24:27 +08:00
Add a config flag to disable whitespace trimming
This commit is contained in:
@@ -1181,4 +1181,4 @@ public class XMLConfigurationTest {
|
||||
assertTrue("Error: " +e.getMessage(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1319,6 +1319,80 @@ public class XMLTest {
|
||||
"parameter of the XMLParserConfiguration used");
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testWithWhitespaceTrimmingDisabled() {
|
||||
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
|
||||
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false));
|
||||
String expectedJsonString = "{\"testXml\":\" Test Whitespace String \t \"}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
@Test
|
||||
public void testNestedWithWhitespaceTrimmingDisabled() {
|
||||
String originalXml =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses>\n"+
|
||||
" <address>\n"+
|
||||
" <name> Sherlock Holmes </name>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false));
|
||||
String expectedJsonString = "{\"addresses\":{\"address\":{\"name\":\" Sherlock Holmes \"}}}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
@Test
|
||||
public void shouldTrimWhitespaceDoesNotSupportTagsEqualingCDataTagName() {
|
||||
// When using withShouldTrimWhitespace = true, input containing tags with same name as cDataTagName is unsupported and should not be used in conjunction
|
||||
String originalXml =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses>\n"+
|
||||
" <address>\n"+
|
||||
" <content> Sherlock Holmes </content>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(false).withcDataTagName("content"));
|
||||
String expectedJsonString = "{\"addresses\":{\"address\":[[\"\\n \",\" Sherlock Holmes \",\"\\n \"]]}}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
@Test
|
||||
public void shouldTrimWhitespaceEnabledDropsTagsEqualingCDataTagNameButValueRemains() {
|
||||
String originalXml =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
|
||||
"<addresses>\n"+
|
||||
" <address>\n"+
|
||||
" <content> Sherlock Holmes </content>\n"+
|
||||
" </address>\n"+
|
||||
"</addresses>";
|
||||
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(true).withcDataTagName("content"));
|
||||
String expectedJsonString = "{\"addresses\":{\"address\":\"Sherlock Holmes\"}}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
@Test
|
||||
public void testWithWhitespaceTrimmingEnabled() {
|
||||
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
|
||||
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withShouldTrimWhitespace(true));
|
||||
String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
@Test
|
||||
public void testWithWhitespaceTrimmingEnabledByDefault() {
|
||||
String originalXml = "<testXml> Test Whitespace String \t </testXml>";
|
||||
|
||||
JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration());
|
||||
String expectedJsonString = "{\"testXml\":\"Test Whitespace String\"}";
|
||||
JSONObject expectedJson = new JSONObject(expectedJsonString);
|
||||
Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user