Last active
August 6, 2017 15:34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | |
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; | |
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; | |
import java.io.IOException; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Map; | |
public class JsonToXmlWithLists { | |
static class Row { | |
private final String caption; | |
private final String value; | |
@JsonCreator | |
Row(@JsonProperty("caption") String caption, @JsonProperty("value") String value) { | |
this.caption = caption; | |
this.value = value; | |
} | |
public String getCaption() { | |
return caption; | |
} | |
public String getValue() { | |
return value; | |
} | |
} | |
@JacksonXmlRootElement(localName = "Table") | |
static class XmlWrapper { | |
@JacksonXmlElementWrapper(useWrapping = false) | |
private final List<Row> row; | |
@JsonCreator | |
XmlWrapper(@JsonProperty("Row") List<Row> row) { | |
this.row = row; | |
} | |
@JsonProperty("Row") | |
public List<Row> getRow() { | |
return row; | |
} | |
} | |
private static Row rowFromMap(Map<String, String> currentEntry) { | |
return new Row(currentEntry.get("caption"), currentEntry.get("value")); | |
} | |
public static void main(String[] args) throws IOException { | |
final String testJson = | |
"{\n" + | |
" \"purpose\": {\n" + | |
" \"caption\": \"Purpose\",\n" + | |
" \"value\": \"Buy a goat\"\n" + | |
" },\n" + | |
" \"goatValue\": {\n" + | |
" \"caption\": \"Goat value\",\n" + | |
" \"value\": \"4500000\"\n" + | |
" },\n" + | |
" \"loanAmount\": {\n" + | |
" \"caption\": \"Loan amount\",\n" + | |
" \"value\": \"5000000\"\n" + | |
" },\n" + | |
" \"childrenInfo\": [{\n" + | |
" \"gender\": {\n" + | |
" \"caption\": \"Gender\",\n" + | |
" \"value\": \"Boy\"\n" + | |
" },\n" + | |
" \"provider\": {\n" + | |
" \"caption\": \"Provider\",\n" + | |
" \"value\": \"Yes\"\n" + | |
" },\n" + | |
" \"age\": {\n" + | |
" \"caption\": \"Age\",\n" + | |
" \"value\": \"5\"\n" + | |
" }\n" + | |
" },\n" + | |
" {\n" + | |
" \"gender\": {\n" + | |
" \"caption\": \"Gender\",\n" + | |
" \"value\": \"Girl\"\n" + | |
" },\n" + | |
" \"provider\": {\n" + | |
" \"caption\": \"Provider\",\n" + | |
" \"value\": \"No\"\n" + | |
" },\n" + | |
" \"age\": {\n" + | |
" \"caption\": \"Age\",\n" + | |
" \"value\": \"17\"\n" + | |
" }\n" + | |
" }]\n" + | |
"}"; | |
final ObjectMapper jsonMapper = new ObjectMapper(); | |
final Map<String, Object> input = jsonMapper.readValue(testJson, new TypeReference<Map<String, Object>>() {}); | |
final List<Row> output = new LinkedList<Row>(); | |
for (String key : input.keySet()) { | |
final Class<?> currentValueClass = input.get(key).getClass(); | |
if (Map.class.isAssignableFrom(currentValueClass)) { | |
final Map<String, String> currentEntry = (Map<String, String>) input.get(key); | |
output.add(rowFromMap(currentEntry)); | |
} else if (List.class.isAssignableFrom(currentValueClass)) { | |
final List<Map<String, Map<String, String>>> currentEntry = (List<Map<String, Map<String, String>>>) input.get(key); | |
for (Map<String, Map<String, String>> currentSubEntryWrapper : currentEntry) { | |
for (Map<String, String> currentSubEntry : currentSubEntryWrapper.values()) { | |
output.add(rowFromMap(currentSubEntry)); | |
} | |
} | |
} else { | |
System.err.println("Unhandled type for entry: " + key); | |
} | |
} | |
final XmlWrapper xmlWrapper = new XmlWrapper(output); | |
final XmlMapper xmlMapper = new XmlMapper(); | |
final String outputXml = xmlMapper.writeValueAsString(xmlWrapper); | |
System.out.println(outputXml); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment