Skip to content

Instantly share code, notes, and snippets.

@pgbytes
Created September 18, 2018 13:00
Show Gist options
  • Save pgbytes/8bb48bb4a05e173d5d41ebb78b5f3e2a to your computer and use it in GitHub Desktop.
Save pgbytes/8bb48bb4a05e173d5d41ebb78b5f3e2a to your computer and use it in GitHub Desktop.
Parsing Map<K,V> to and from JSON
import _.model.MapEntry;
import _.model.MapType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.HashMap;
import java.util.Map;
public class JavaMapAdapter<K,V> extends XmlAdapter<MapType<K,V>, Map<K, V>> {
@Override
public Map<K, V> unmarshal(MapType<K, V> v) throws Exception {
Map<K,V> map = new HashMap<>();
for (MapEntry<K,V> mapEntry: v.getEntry()) {
map.put(mapEntry.getKey(), mapEntry.getValue());
}
return map;
}
@Override
public MapType<K, V> marshal(Map<K, V> v) throws Exception {
MapType<K,V> mapType = new MapType<>();
for (Map.Entry<K,V> entry: v.entrySet()) {
mapType.addEntry(new MapEntry<>(entry.getKey(), entry.getValue()));
}
return mapType;
}
}
import javax.xml.bind.annotation.XmlElement;
import java.util.Map;
/**
* Wrapper class
*/
public class MapEntry<K,V> {
@XmlElement
private K key;
@XmlElement
private V value;
public MapEntry() {
}
public MapEntry(Map.Entry<K,V> e) {
this.key = e.getKey();
this.value = e.getValue();
}
public MapEntry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
import javax.xml.bind.annotation.XmlElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Wrapper class for Hashmap
*/
public class MapType<K,V> {
@XmlElement
private List<MapEntry<K,V>> entries = new ArrayList<>();
public MapType() {
}
public MapType(Map<K,V> map) {
for (Map.Entry<K,V> e: map.entrySet()) {
entries.add(new MapEntry<>(e));
}
}
public List<MapEntry<K, V>> getEntry() {
return Collections.unmodifiableList(entries);
}
public void addEntry(MapEntry<K, V> entry) {
this.entries.add(entry);
}
}
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.HashMap;
import java.util.Map;
public class MarshallUtil {
private DocumentBuilder documentBuilder;
public MarshallUtil() throws Exception {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
public Element marshallElement(Map<String, String> map, String elementName) {
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement(elementName);
document.appendChild(rootElement);
for (Map.Entry<String, String> entry: map.entrySet()) {
Element childElement = document.createElement(entry.getKey());
childElement.setTextContent(entry.getValue());
rootElement.appendChild(childElement);
}
return rootElement;
}
public Map<String, String> unmarshalMap(Element rootElement) throws Exception {
NodeList nodeList = rootElement.getChildNodes();
Map<String, String> map = new HashMap<>(nodeList.getLength());
for(int x=0; x<nodeList.getLength(); x++) {
Node node = nodeList.item(x);
if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.ATTRIBUTE_NODE) {
map.put(node.getNodeName(), node.getTextContent());
}
}
return map;
}
}
/**
* Pojo class using the adapter
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TransactionResponse {
@XmlElement(name = "forward")
@XmlJavaTypeAdapter(JavaMapAdapter.class)
private Map<String, String> forward;
// Other non relevant fields are truncated
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment