Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep proper data type for JSON arrays between conversions. & Fixed conversions for nested JSON arrays and single element Array #33

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ build
*.iws
.idea
out
/.nb-gradle/
14 changes: 10 additions & 4 deletions src/main/java/org/kordamp/json/xml/XMLSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ public JSON read(String xml) {
try {
Document doc = new Builder().build(new StringReader(xml));
Element root = doc.getRootElement();
log.info("[ " + root.getLocalName() + " " + isNullObject(root) + " " + isArray(root, true));
if (isNullObject(root)) {
return JSONNull.getInstance();
}
Expand Down Expand Up @@ -830,7 +831,7 @@ private boolean checkChildElements(Element element, boolean isTopLevel) {
}
}
}

String childName = elements.get(0)
.getQualifiedName();
for (int i = 1; i < elementCount; i++) {
Expand Down Expand Up @@ -929,7 +930,7 @@ private boolean isArray(Element element, boolean isTopLevel) {
&& (element.getAttribute(addJsonPrefix("class")) != null && element.getAttribute(addJsonPrefix("type")) != null)) {
isArray = checkChildElements(element, isTopLevel);
}

if (isArray) {
// check namespace
for (int j = 0; j < element.getNamespaceDeclarationCount(); j++) {
Expand All @@ -941,6 +942,11 @@ private boolean isArray(Element element, boolean isTopLevel) {
}
}

if(!isArray && isTopLevel && !isForceTopLevelObject()
&& element.getQualifiedName().equalsIgnoreCase(getArrayName())){
isArray = true;
}

return isArray;
}

Expand Down Expand Up @@ -1368,10 +1374,10 @@ private void setValue(JSONArray jsonArray, Element element, String defaultType)
params = StringUtils.split(paramsAttribute.getValue(), ",");
jsonArray.element(new JSONFunction(params, text));
return;
} else {
}/* else {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follows similar code setValue variant for JSONObject.

jsonArray.element(simplifyValue(null, processElement(element, type)));
return;
}
}*/
}

boolean classProcessed = false;
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/kordamp/json/xml/TestXMLSerializer_reads.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,28 @@ public void testReadNestedObject() {
JSONObject expected = JSONObject.fromObject("{name:\"json\",nested:{id:1}}");
Assertions.assertEquals(expected, actual);
}

public void testReadNestedArray(){
String xml = "<a> <e class=\"array\"> <e type=\"string\">values</e> <e type=\"number\">1</e> </e> </a>";
JSON actual = (JSONArray) xmlSerializer.read(xml);
JSON expected = JSONArray.fromObject("[[\"values\", 1]]");
Assertions.assertEquals(expected, actual);
}

public void testReadSingleElementArray(){
String xml = "<a> <e type=\"string\">val</e> </a>";
JSON actual = xmlSerializer.read(xml);
JSON expected = JSONArray.fromObject("[\"val\"]");
Assertions.assertEquals(expected, actual);
}

public void testReadNumberArray_withDefaultType() {
String xml = "<a type=\"number\"><e>1.1</e><e>2.2</e><e>3</e></a>";
JSON actual = xmlSerializer.read(xml);
JSON expected = JSONArray.fromObject("[1.1,2.2,3]");
Assertions.assertEquals(expected, actual);
}

public void testReadNumberArray_withoutDefaultType() {
String xml = "<a><e type=\"number\">1.1</e><e type=\"number\">2.2</e><e type=\"number\">3</e></a>";
JSON actual = xmlSerializer.read(xml);
Expand Down