mirror of
https://github.com/stleary/JSON-java.git
synced 2026-04-04 00:00:53 -04:00
Merge pull request #1039 from pratiktiwari13/bugfix/empty-force-list
Fixes the issue of losing the array if an empty forceList element or a tag is in the middle or the end
This commit is contained in:
@@ -1092,7 +1092,7 @@ public class XMLConfigurationTest {
|
||||
"<addresses></addresses>";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addresses\":[]}";
|
||||
"{\"addresses\":[\"\"]}";
|
||||
|
||||
Set<String> forceList = new HashSet<String>();
|
||||
forceList.add("addresses");
|
||||
@@ -1130,7 +1130,7 @@ public class XMLConfigurationTest {
|
||||
"<addresses />";
|
||||
|
||||
String expectedStr =
|
||||
"{\"addresses\":[]}";
|
||||
"{\"addresses\":[\"\"]}";
|
||||
|
||||
Set<String> forceList = new HashSet<String>();
|
||||
forceList.add("addresses");
|
||||
@@ -1144,6 +1144,157 @@ public class XMLConfigurationTest {
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListWithLastElementAsEmptyTag(){
|
||||
final String originalXml = "<root><id>1</id><id/></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\"]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withcDataTagName("content")
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListWithFirstElementAsEmptyTag(){
|
||||
final String originalXml = "<root><id/><id>1</id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[\"\",1]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withcDataTagName("content")
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListWithMiddleElementAsEmptyTag(){
|
||||
final String originalXml = "<root><id>1</id><id/><id>2</id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\",2]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withcDataTagName("content")
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListWithLastElementAsEmpty(){
|
||||
final String originalXml = "<root><id>1</id><id></id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\"]}}";
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListWithFirstElementAsEmpty(){
|
||||
final String originalXml = "<root><id></id><id>1</id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[\"\",1]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListWithMiddleElementAsEmpty(){
|
||||
final String originalXml = "<root><id>1</id><id></id><id>2</id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[1,\"\",2]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListEmptyAndEmptyTagsMixed(){
|
||||
final String originalXml = "<root><id></id><id/><id>1</id><id/><id></id><id>2</id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[\"\",\"\",1,\"\",\"\",2]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
final JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withKeepStrings(false)
|
||||
.withForceList(forceListCandidates)
|
||||
.withConvertNilAttributeToNull(true));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListConsistencyWithDefault() {
|
||||
final String originalXml = "<root><id>0</id><id>1</id><id/><id></id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[0,1,\"\",\"\"]}}";
|
||||
|
||||
// confirm expected result of default array-of-tags processing
|
||||
JSONObject json = XML.toJSONObject(originalXml);
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
|
||||
// confirm forceList array-of-tags processing is consistent with default processing
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withForceList(forceListCandidates));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListInitializesAnArrayWithAnEmptyElement(){
|
||||
final String originalXml = "<root><id></id></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[\"\"]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withForceList(forceListCandidates));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceListInitializesAnArrayWithAnEmptyTag(){
|
||||
final String originalXml = "<root><id/></root>";
|
||||
final String expectedJsonString = "{\"root\":{\"id\":[\"\"]}}";
|
||||
|
||||
HashSet<String> forceListCandidates = new HashSet<>();
|
||||
forceListCandidates.add("id");
|
||||
JSONObject json = XML.toJSONObject(originalXml,
|
||||
new XMLParserConfiguration()
|
||||
.withForceList(forceListCandidates));
|
||||
assertEquals(expectedJsonString, json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxNestingDepthIsSet() {
|
||||
XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration.ORIGINAL;
|
||||
|
||||
Reference in New Issue
Block a user