MOXy's @XmlVariableNode - Using a Map's Key as the Node Name
Join the DZone community and get the full member experience.
Join For FreeYou can try this out today using a nightly build of EclipseLink 2.6.0:
Input/Output
Below are the XML and JSON representations we will use in this example. Each has node names that correspond to keys and contents that correspond to values of a Map.
<?xml version="1.0" encoding="UTF-8"?> <root> <A>1</A> <B>2</B> </root>
{ "A" : 1, "B" : 2 }
package blog.variablenode.map; import java.util.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Root { @XmlPath(".") @XmlJavaTypeAdapter(MapAdapter.class) private Map<String, Integer> map = new HashMap<String, Integer>(); }
package blog.variablenode.map; import java.util.*; import java.util.Map.Entry; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlAdapter; import org.eclipse.persistence.oxm.annotations.XmlVariableNode; public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<String, Integer>> { public static class AdaptedMap { @XmlVariableNode("key") List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>(); } public static class AdaptedEntry { @XmlTransient public String key; @XmlValue public Integer value; } @Override public AdaptedMap marshal(Map<String, Integer> map) throws Exception { AdaptedMap adaptedMap = new AdaptedMap(); for(Entry<String, Integer> entry : map.entrySet()) { AdaptedEntry adaptedEntry = new AdaptedEntry(); adaptedEntry.key = entry.getKey(); adaptedEntry.value = entry.getValue(); adaptedMap.entries.add(adaptedEntry); } return adaptedMap; } @Override public Map<String, Integer> unmarshal(AdaptedMap adaptedMap) throws Exception { List<AdaptedEntry> adaptedEntries = adaptedMap.entries; Map<String, Integer> map = new HashMap<String, Integer>(adaptedEntries.size()); for(AdaptedEntry adaptedEntry : adaptedEntries) { map.put(adaptedEntry.key, adaptedEntry.value); } return map; } }
package blog.variablenode.map; import java.io.File; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.MarshallerProperties; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Root.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/blog/variablenode/map/input.xml"); Root root = (Root) unmarshaller.unmarshal(xml); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false); marshaller.marshal(root, System.out); } }
- MOXy's @XmlVariableNode - JSON Schema Example
- Specifying EclipseLink MOXy as your JAXB Provider
- JAXB and java.util.Map
- JSON Binding with EclipseLink MOXy - Twitter Example
Published at DZone with permission of Blaise Doughan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments