Skip to content

Commit

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

JsonToXml - added new logic when text type content is null
  • Loading branch information
skin27 authored Jan 24, 2025
2 parents 0cc5c0b + dad5b4e commit bb8a0ee
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Constants {
);

public static final String INTERNAL_NULL_OBJECT_NODE_VALUE = "@NULL@";
public static final String JSON_NULL_VALUE = "json_null";
public static final String NULL_VALUE = "null";

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public class TextType implements NodeTransaction {
@Override
public Element process(JsonToXmlConfiguration config) {
// extract child as a text

if(config.getJsonNode().isNull()) {
if(config.isTypeHints()) {
config.getElement().setAttribute(Constants.JSON_XML_ATTR_CLASS, Constants.JSON_XML_ATTR_TYPE_OBJECT);
config.getElement().setAttribute(Constants.NULL_VALUE, "true");
} else {
config.getElement().setAttribute(Constants.JSON_NULL_VALUE, "true");
}
return config.getElement();
}

Text textNode = config.getDocument().createTextNode(config.getJsonNode().asText());
config.getElement().appendChild(textNode);
if(config.getElement().hasAttribute(Constants.JSON_XML_ATTR_TYPE) || !config.isTypeHints()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,55 @@ public void testJsonXml_7_EATT() throws Exception {
);
}

/*****************************************
** Example 8
*****************************************/

@Test
public void testJsonXml_8_EARFF() throws Exception {
compareInputJsonFileWithOutputXmlFile(
"jsontoxmllegacy_Element_Array_Root",
"json-to-xml/example_8.json",
"json-to-xml/example_8_EARFF.xml"
);
}

@Test
public void testJsonXml_8_EARTF() throws Exception {
compareInputJsonFileWithOutputXmlFile(
"jsontoxmllegacy_Element_Array_Root_NamespaceLenient",
"json-to-xml/example_8.json",
"json-to-xml/example_8_EARTF.xml"
);
}

@Test
public void testJsonXml_8_EARFT() throws Exception {
compareInputJsonFileWithOutputXmlFile(
"jsontoxmllegacy_Element_Array_Root_TypeHints",
"json-to-xml/example_8.json",
"json-to-xml/example_8_EARFT.xml"
);
}

@Test
public void testJsonXml_8_EARTT() throws Exception {
compareInputJsonFileWithOutputXmlFile(
"jsontoxmllegacy_Element_Array_Root_NamespaceLenient_TypeHints",
"json-to-xml/example_8.json",
"json-to-xml/example_8_EARTT.xml"
);
}

@Test
public void testJsonXml_8_EATT() throws Exception {
compareInputJsonFileWithOutputXmlFile(
"jsontoxmllegacy_Element_Array_NamespaceLenient_TypeHints",
"json-to-xml/example_8.json",
"json-to-xml/example_8_EATT.xml"
);
}

private void compareInputJsonFileWithOutputXmlFile(String routeName, String inputJsonFile, String outputXmlFile)
throws IOException, InterruptedException {
String defaultJson = IOUtils.toString(classLoader.getResourceAsStream(inputJsonFile), StandardCharsets.UTF_8);
Expand Down
3 changes: 3 additions & 0 deletions jsontoxmllegacy/src/test/resources/json-to-xml/example_8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"test": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<root><test json_null="true"/></root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test class="object" null="true"/>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test json_null="true"/>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test class="object" null="true"/>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<o>
<test class="object" null="true"/>
</o>

0 comments on commit bb8a0ee

Please sign in to comment.