Skip to content

Instantly share code, notes, and snippets.

@santmatthew
Last active August 6, 2017 15:34

Revisions

  1. santmatthew revised this gist Aug 6, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion JsonToXmlWithLists.java
    Original file line number Diff line number Diff line change
    @@ -108,7 +108,6 @@ public static void main(String[] args) throws IOException {
    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()) {
    System.out.println("map: " + currentSubEntry);
    output.add(rowFromMap(currentSubEntry));
    }
    }
  2. santmatthew revised this gist Aug 6, 2017. 1 changed file with 48 additions and 45 deletions.
    93 changes: 48 additions & 45 deletions JsonToXmlWithLists.java
    Original file line number Diff line number Diff line change
    @@ -54,47 +54,47 @@ private static Row rowFromMap(Map<String, String> currentEntry) {
    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" +
    "}";
    " \"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>();
    @@ -105,9 +105,12 @@ public static void main(String[] args) throws IOException {
    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, String>> currentEntry = (List<Map<String, String>>) input.get(key);
    for (Map<String, String> currentSubEntry : currentEntry) {
    output.add(rowFromMap(currentSubEntry));
    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()) {
    System.out.println("map: " + currentSubEntry);
    output.add(rowFromMap(currentSubEntry));
    }
    }
    } else {
    System.err.println("Unhandled type for entry: " + key);
    @@ -119,4 +122,4 @@ public static void main(String[] args) throws IOException {
    final String outputXml = xmlMapper.writeValueAsString(xmlWrapper);
    System.out.println(outputXml);
    }
    }
    }
  3. santmatthew created this gist Aug 6, 2017.
    122 changes: 122 additions & 0 deletions JsonToXmlWithLists.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    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, String>> currentEntry = (List<Map<String, String>>) input.get(key);
    for (Map<String, String> currentSubEntry : currentEntry) {
    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);
    }
    }