mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Test case passed with tags with multiple entries set forceList
This commit is contained in:
parent
e638955034
commit
5dd78bc0b9
@ -380,12 +380,23 @@ public class XML {
|
|||||||
if (x.nextToken() != GT) {
|
if (x.nextToken() != GT) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
if (nilAttributeFound) {
|
if (config.getForceList().contains(tagName)) {
|
||||||
context.accumulate(tagName, JSONObject.NULL);
|
// Force the value to be an array
|
||||||
} else if (jsonObject.length() > 0) {
|
if (nilAttributeFound) {
|
||||||
context.accumulate(tagName, jsonObject);
|
context.append(tagName, JSONObject.NULL);
|
||||||
|
} else if (jsonObject.length() > 0) {
|
||||||
|
context.append(tagName, jsonObject);
|
||||||
|
} else {
|
||||||
|
context.put(tagName, new JSONArray());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
context.accumulate(tagName, "");
|
if (nilAttributeFound) {
|
||||||
|
context.accumulate(tagName, JSONObject.NULL);
|
||||||
|
} else if (jsonObject.length() > 0) {
|
||||||
|
context.accumulate(tagName, jsonObject);
|
||||||
|
} else {
|
||||||
|
context.accumulate(tagName, "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -414,6 +425,7 @@ public class XML {
|
|||||||
// Nested element
|
// Nested element
|
||||||
if (parse(x, jsonObject, tagName, config)) {
|
if (parse(x, jsonObject, tagName, config)) {
|
||||||
if (config.getForceList().contains(tagName)) {
|
if (config.getForceList().contains(tagName)) {
|
||||||
|
// Force the value to be an array
|
||||||
if (jsonObject.length() == 0) {
|
if (jsonObject.length() == 0) {
|
||||||
context.put(tagName, new JSONArray());
|
context.put(tagName, new JSONArray());
|
||||||
} else if (jsonObject.length() == 1
|
} else if (jsonObject.length() == 1
|
||||||
|
@ -976,6 +976,45 @@ public class XMLConfigurationTest {
|
|||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
|
public void testMultipleTagForceList() {
|
||||||
|
String xmlStr =
|
||||||
|
"<addresses>\n"+
|
||||||
|
" <address>\n"+
|
||||||
|
" <name>Sherlock Holmes</name>\n"+
|
||||||
|
" <name>John H. Watson</name>\n"+
|
||||||
|
" </address>\n"+
|
||||||
|
"</addresses>";
|
||||||
|
|
||||||
|
String expectedStr =
|
||||||
|
"{"+
|
||||||
|
"\"addresses\":["+
|
||||||
|
"{"+
|
||||||
|
"\"address\":["+
|
||||||
|
"{"+
|
||||||
|
"\"name\":["+
|
||||||
|
"\"Sherlock Holmes\","+
|
||||||
|
"\"John H. Watson\""+
|
||||||
|
"]"+
|
||||||
|
"}"+
|
||||||
|
"]"+
|
||||||
|
"}"+
|
||||||
|
"]"+
|
||||||
|
"}";
|
||||||
|
|
||||||
|
Set<String> forceList = new HashSet<String>();
|
||||||
|
forceList.add("addresses");
|
||||||
|
forceList.add("address");
|
||||||
|
forceList.add("name");
|
||||||
|
|
||||||
|
XMLParserConfiguration config =
|
||||||
|
new XMLParserConfiguration()
|
||||||
|
.withForceList(forceList);
|
||||||
|
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
|
||||||
|
JSONObject expetedJsonObject = new JSONObject(expectedStr);
|
||||||
|
|
||||||
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
public void testEmptyForceList() {
|
public void testEmptyForceList() {
|
||||||
String xmlStr =
|
String xmlStr =
|
||||||
"<addresses></addresses>";
|
"<addresses></addresses>";
|
||||||
@ -994,6 +1033,44 @@ public class XMLConfigurationTest {
|
|||||||
|
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
public void testContentForceList() {
|
||||||
|
String xmlStr =
|
||||||
|
"<addresses>Baker Street</addresses>";
|
||||||
|
|
||||||
|
String expectedStr =
|
||||||
|
"{\"addresses\":[\"Baker Street\"]}";
|
||||||
|
|
||||||
|
Set<String> forceList = new HashSet<String>();
|
||||||
|
forceList.add("addresses");
|
||||||
|
|
||||||
|
XMLParserConfiguration config =
|
||||||
|
new XMLParserConfiguration()
|
||||||
|
.withForceList(forceList);
|
||||||
|
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
|
||||||
|
JSONObject expetedJsonObject = new JSONObject(expectedStr);
|
||||||
|
|
||||||
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testEmptyTagForceList() {
|
||||||
|
String xmlStr =
|
||||||
|
"<addresses />";
|
||||||
|
|
||||||
|
String expectedStr =
|
||||||
|
"{\"addresses\":[]}";
|
||||||
|
|
||||||
|
Set<String> forceList = new HashSet<String>();
|
||||||
|
forceList.add("addresses");
|
||||||
|
|
||||||
|
XMLParserConfiguration config =
|
||||||
|
new XMLParserConfiguration()
|
||||||
|
.withForceList(forceList);
|
||||||
|
JSONObject jsonObject = XML.toJSONObject(xmlStr, config);
|
||||||
|
JSONObject expetedJsonObject = new JSONObject(expectedStr);
|
||||||
|
|
||||||
|
Util.compareActualVsExpectedJsonObjects(jsonObject, expetedJsonObject);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience method, given an input string and expected result,
|
* Convenience method, given an input string and expected result,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user