Skip to content

Instantly share code, notes, and snippets.

@psamsotha
Created January 6, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psamsotha/624d0dc1cc8a00c417e57f8fce8d905d to your computer and use it in GitHub Desktop.
Save psamsotha/624d0dc1cc8a00c417e57f8fce8d905d to your computer and use it in GitHub Desktop.
Map XmlAdapter
public class MapAdapter extends XmlAdapter<MapAdapter.MapLike, Map<String, String>> {
public static class Entry {
public String value;
public String key;
public Entry() {}
public Entry(String value, String key) {}
}
public static class MapLike {
public List<MapAdapter.Entry> entry;
}
@Override
public Map<String, String> unmarshal(MapLike v) throws Exception {
Map<String, String> map = new HashMap<>();
for (Entry entry: v.entry) {
map.put(entry.key, entry.value);
}
return map;
}
@Override
public MapLike marshal(Map<String, String> v) throws Exception {
MapLike mapLike = new MapLike();
mapLike.entry = new ArrayList<>();
for (Map.Entry<String, String> entry: v.entrySet()) {
mapLike.entry.add(new Entry(entry.getKey(), entry.getValue()));
}
return mapLike;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment