Skip to content

Commit

Permalink
Merge pull request #136 from assimbly/334-xmltojsondifferent-output-a…
Browse files Browse the repository at this point in the history
…t-certain-message-body

XmlToJson component - improvements and corrections
  • Loading branch information
brunovg authored Jan 9, 2025
2 parents b1e0db4 + 2896073 commit ad4c700
Show file tree
Hide file tree
Showing 116 changed files with 1,656 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,22 @@ public static boolean isRootArray(XmlToJsonConfiguration config) {
private static boolean isRootArrayWithTypeHints(XmlToJsonConfiguration config) {
boolean isRootArray = false;

if (config.getLevel() == 0 && config.getNumberOfChildren() == 1 && !config.isElementDefiningNamespaces()) {
if (config.getLevel() == 0 && config.getNumberOfChildren() == 1 && !config.isElementDefiningNamespaces() && !config.isHasAttributes()) {
isRootArray = true;
}
if (config.getElementDeepestDepth() > 2 && !config.isElementDefiningNamespaces() && config.isAreChildrenNamesEqual()) {
isRootArray = true;
}
if (config.getElementDeepestDepth() == 2 && !config.isElementDefiningNamespaces() && config.isAreChildrenNamesEqual() &&
(config.isAreSiblingsNamesEqual() || !config.getClassAttr().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT))
(config.isAreSiblingsNamesEqual() || !config.getClassAttr().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT)) &&
!config.isHasAttributes()
) {
isRootArray = true;
}
if (config.getElementDeepestDepth() == 1 && config.getParentClass() != null && !config.isElementOnNamespace() &&
(config.getParentClass().equals("") || config.getParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_ARRAY) ||
config.getParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT))
config.getParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT)) &&
!config.isHasAttributes()
) {
isRootArray = true;
}
Expand All @@ -54,13 +56,13 @@ private static boolean isRootArrayWithTypeHints(XmlToJsonConfiguration config) {
)) {
isRootArray = true;
}
if (config.getElementDeepestDepth() == 0 && !config.isParentWithEmptyTextContent() && !config.isHasAttributes() && !config.isHasParentAttributes() &&
if (config.getElementDeepestDepth() == 0 && !config.isParentWithEmptyTextContent() && !config.isHasAttributes() &&
(config.getNumberOfSiblings() == 1 || config.getNumberOfSiblings() > 1 && config.isAreSiblingsNamesEqual()) &&
(!config.getParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT) ||
config.getParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT) &&
config.isParentSiblingsNamesEqual()) &&
(!config.getGrandParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT) ||
config.getGrandParentClass().equalsIgnoreCase(Constants.JSON_XML_ATTR_TYPE_OBJECT) &&
(!Constants.JSON_XML_ATTR_TYPE_OBJECT.equalsIgnoreCase(config.getGrandParentClass()) ||
Constants.JSON_XML_ATTR_TYPE_OBJECT.equalsIgnoreCase(config.getGrandParentClass()) &&
config.isGrandParentSiblingsNamesEqual())
) {
isRootArray = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public JsonNode process(XmlToJsonConfiguration config, Node childNode) {
if(config.isElementWithEmptyTextContent() ||
childrenHasEmptyTextContent ||
(config.getLevel() == 0 && config.getNumberOfChildren() > 1) ||
(config.getNumberOfChildren() == 1 && children >= 1 && !classAttrOnChildElementIsNUll) ||
(config.getNumberOfChildren() == 1 && (children == 0 || children >= 1 && (!classAttrOnChildElementIsNUll || childNode.hasAttributes()))) ||
!ElementUtils.areSiblingsNamesEqual((Element) childNode) ||
config.isElementOnNamespace()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public static void extractChildAsOtherInObjectNode(XmlToJsonConfiguration config
config.getRootObjectNode().put(propertyName, !config.isElementMustBeNull() ? node.get(0) : null);
}
} else {
config.getRootObjectNode().put(propertyName, !config.isElementMustBeNull() ? node : null);
boolean simpleValueToInclude = node.isArray() && node.size() == 1 && node.get(0).isValueNode() && config.getElementDeepestDepth() <= 1;
JsonNode nodeToInclude = simpleValueToInclude ? node.get(0) : node;
config.getRootObjectNode().put(propertyName, !config.isElementMustBeNull() ? nodeToInclude : null);
}
} else {
if(node.get(propertyName) != null) {
Expand Down
Loading

0 comments on commit ad4c700

Please sign in to comment.