A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
How to implement and serialize nested Maps?
Siegfried Heintze
Ranch Hand
Joined: Aug 11, 2000
Posts: 359
posted
Aug 28, 2009 00:52:31
0
The code below serializes a hashmap in class Bean. There is a second map in there just to demonstrate sun's bug where maps by default won't serialize.
Let's say I want hash map that looks like this:
Map<
String
,Map><String,Map><String,String>>> map;
How would I modify my class bean to contain such a map of maps of maps?
I would want to use templates so I'm not contrained to using strings for keys or values. How would this work?
Thanks,
Siegfried
/* * Begin commands to execute this file using Java with bash * if [ `uname` == 'Linux' ] * then * export CLASSPATH=bin:. * else * export CLASSPATH=bin\;. * fi * pushd ../../ * if [ -e "bin" ] * then * echo "already have build (bin) directory" * else * echo "mkdir bin" * mkdir bin * fi * javac -g com/heintze/DemoMaps.java -d bin * java com.heintze.DemoMaps "/demoMaps/datum[1]/value/text()" '/demoMaps/datum[@name="abc"]' <<EOF * ><?xml version="1.0" encoding="UTF-8" standalone="yes"?> * <demoMaps><map/> * <map2> * <map> * <item value="mumble" key="fratz"/> * </map> * </map2> * <datum name="xyz"><value>Insert your sample data here</value></datum> * <datum name="abc"><value>def</value></datum> * </demoMaps> * EOF * rm bin/com/heintze/DemoMaps.class * popd * End commands to execute this file using Java with bash * */ package com.heintze; import java.io.FileReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeMap; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlType class Pair{ Pair(){} Pair(String name, String value){ this.name = name; this.value = value; } @XmlAttribute String name; @XmlElement String value; } // http://www.caucho.com/resin/doc/jaxb-annotations.xtp#@XmlJavaTypeAdapter @XmlRootElement class Bean { @XmlJavaTypeAdapter(MyMapAdapter.class) public HashMap<String,String> map = new HashMap<String,String>(); } class MyMapAdapter extends XmlAdapter<Item[],java.util.Map><String,String>> { // http://weblogs.java.net/blog/kohsuke/archive/2005/04/xmladapter_in_j.html @Override public Item[] marshal(Map<String, String> map) throws Exception { Item[] result=new Item[map.size()]; int ii=0; for(String key:map.keySet()){ result[ii]=new Item(); result[ii].key=key; result[ii].value=map.get(key); ii = ii + 1; } return result; } @Override public Map<String, String> unmarshal(Item[] value) throws Exception { Map<String,String> r = new HashMap<String,String>(); for( Item c : value ) r.put(c.key, c.value); return r; } } class Item { @XmlAttribute public String key; @XmlAttribute public String value; } class Temp { @XmlElement private List<Item> entry = new java.util.ArrayList<Item>(); } @XmlRootElement public class DemoMaps { static java.io.PrintStream out = System.out; public static void main(String[] args) { DemoMaps data = new DemoMaps(); try { JAXBContext context = JAXBContext.newInstance(DemoMaps.class); Marshaller m = context.createMarshaller(); Unmarshaller um = context.createUnmarshaller(); data = (DemoMaps)um.unmarshal(new FileReader(java.io.FileDescriptor.in) ); data.datum[0].name+="_modified"; data.datum[0].value+="_modified"; data.map.put("hello", "world"); data.map2.map.put("hello", "world"); m.marshal(data, System.out); } catch (JAXBException e) { e.printStackTrace(); } } @XmlElement Pair[] datum =new Pair[0]; @XmlElement(nillable=true, required=false) TreeMap<String,String> map=new TreeMap<String,String>(); @XmlElement() Bean map2 = new Bean(); }
Siegfried Heintze
Ranch Hand
Joined: Aug 11, 2000
Posts: 359
posted
Aug 29, 2009 14:08:13
0
I figured it out. Now please see my new post on converting it to generics.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: How to implement and serialize nested Maps?
Similar Threads
Wanted: Help serializing Generic Maps!
Problem regarding HashMap.
Hashtable
How to Serialize Maps?
JAXB - HashMap
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter