Skip to content

Commit b58513a

Browse files
committed
Properly convert to and from JSON and XML, (Fixed Arrays, kordamp#7)
1 parent e20624c commit b58513a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ build
1313
*.iws
1414
.idea
1515
out
16+
/.nb-gradle/

src/main/java/org/kordamp/json/xml/XMLSerializer.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ public JSON read(String xml) {
522522
try {
523523
Document doc = new Builder().build(new StringReader(xml));
524524
Element root = doc.getRootElement();
525+
log.info("[ " + root.getLocalName() + " " + isNullObject(root) + " " + isArray(root, true));
525526
if (isNullObject(root)) {
526527
return JSONNull.getInstance();
527528
}
@@ -830,7 +831,7 @@ private boolean checkChildElements(Element element, boolean isTopLevel) {
830831
}
831832
}
832833
}
833-
834+
834835
String childName = elements.get(0)
835836
.getQualifiedName();
836837
for (int i = 1; i < elementCount; i++) {
@@ -929,7 +930,7 @@ private boolean isArray(Element element, boolean isTopLevel) {
929930
&& (element.getAttribute(addJsonPrefix("class")) != null && element.getAttribute(addJsonPrefix("type")) != null)) {
930931
isArray = checkChildElements(element, isTopLevel);
931932
}
932-
933+
933934
if (isArray) {
934935
// check namespace
935936
for (int j = 0; j < element.getNamespaceDeclarationCount(); j++) {
@@ -941,6 +942,11 @@ private boolean isArray(Element element, boolean isTopLevel) {
941942
}
942943
}
943944

945+
if(!isArray && isTopLevel && !isForceTopLevelObject()
946+
&& element.getQualifiedName().equalsIgnoreCase(getArrayName())){
947+
isArray = true;
948+
}
949+
944950
return isArray;
945951
}
946952

@@ -974,6 +980,12 @@ private boolean isNullObject(Element element) {
974980
&& (element.getAttribute(addJsonPrefix("class")) != null && element.getAttribute(addJsonPrefix("type")) != null)) {
975981
return true;
976982
}
983+
// String clazz = getClass(element);
984+
// if(element.getAttribute(addJsonPrefix("class")) != null &&
985+
// (clazz.equalsIgnoreCase(JSONTypes.OBJECT) ||
986+
// clazz.equalsIgnoreCase(JSONTypes.ARRAY))){
987+
// return false;
988+
// }
977989
}
978990
if (skipWhitespace && element.getChildCount() == 1 && element.getChild(0) instanceof Text) {
979991
return true;

0 commit comments

Comments
 (0)