Skip to content

Commit

Permalink
BAEL-6509 - JSON to XML conversion in Java (eugenp#14050)
Browse files Browse the repository at this point in the history
* BAEL-6509 - JSON to XML conversion

* BAEL-6509 - CRLF to LF
  • Loading branch information
abh1navv authored May 27, 2023
1 parent 668b8f0 commit d9027f6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
23 changes: 23 additions & 0 deletions xml-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<version>${underscore.version}</version>
</dependency>
</dependencies>

<build>
Expand All @@ -51,6 +71,9 @@

<properties>
<dom4j.version>2.1.3</dom4j.version>
<jackson.version>2.14.1</jackson.version>
<json.version>20230227</json.version>
<underscore.version>1.89</underscore.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.baeldung.xml.json2xml;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.github.underscore.U;
import org.json.JSONObject;
import org.json.XML;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class JsonToXmlUnitTest {

@Test
public void givenJsonString_whenConvertToXMLUsingJsonJava_thenConverted() {
String jsonString = "{\"name\":\"John\", \"age\":20, \"address\":{\"street\":\"Wall Street\", \"city\":\"New York\"}}";
JSONObject jsonObject = new JSONObject(jsonString);
String xmlString = XML.toString(jsonObject);
Assertions.assertEquals("<address><city>New York</city><street>Wall Street</street></address><name>John</name><age>20</age>", xmlString);
}

@Test
public void givenJsonString_whenConvertToXMLUsingJackson_thenConverted() throws JsonProcessingException {
String jsonString = "{\"name\":\"John\", \"age\":20, \"address\":{\"street\":\"Wall Street\", \"city\":\"New York\"}}";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);
String xmlString = new XmlMapper().writeValueAsString(jsonNode);
Assertions.assertEquals("<ObjectNode><name>John</name><age>20</age><address><street>Wall Street</street><city>New York</city></address></ObjectNode>", xmlString);
}

@Test
public void givenJsonString_whenConvertToXMLUsingJacksonWithXMLDeclarationAndRoot_thenConverted() throws JsonProcessingException {
String jsonString = "{\"name\":\"John\", \"age\":20, \"address\":{\"street\":\"Wall Street\", \"city\":\"New York\"}}";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
String xmlString = xmlMapper.writer().withRootName("root").withDefaultPrettyPrinter().writeValueAsString(jsonNode);
Assertions.assertEquals("<?xml version='1.1' encoding='UTF-8'?>\n" +
"<root>\n" +
" <name>John</name>\n" +
" <age>20</age>\n" +
" <address>\n" +
" <street>Wall Street</street>\n" +
" <city>New York</city>\n" +
" </address>\n" +
"</root>\n", xmlString);
}

@Test
public void givenJsonString_whenConvertToXMLUsingUnderscoreJava_thenConverted() {
String jsonString = "{\"name\":\"John\", \"age\":20}";
String xmlString = U.jsonToXml(jsonString);
Assertions.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root>\n" +
" <name>John</name>\n" +
" <age number=\"true\">20</age>\n" +
"</root>", xmlString);
}

@Test
public void givenJsonString_whenConvertToXMLUsingUnderscoreJavaWithoutAttributes_thenConverted() {
String jsonString = "{\"name\":\"John\", \"age\":20}";
String xmlString = U.jsonToXml(jsonString, U.JsonToXmlMode.REMOVE_ATTRIBUTES);
Assertions.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root>\n" +
" <name>John</name>\n" +
" <age>20</age>\n" +
"</root>", xmlString);
}
}

0 comments on commit d9027f6

Please sign in to comment.