Details
-
Type: New Feature
-
Status: Open
-
Priority: Major
-
Resolution: Unresolved
-
Affects Version/s: 1.3.1
-
Fix Version/s: None
-
Component/s: Core
-
Labels:None
-
JDK version and platform:Non JDK-specific
Description
See this email thread for the background on this.
It would be nice to be able to tell XStream about which map/set implementations are in use on specific fields of specific classes. This would obviate the need for XStream to write out "class=..." attributes when marshalling, and lets xstream know what to instantiate when unmarshalling. It also benefits Hibernate users and allows them to neatly sidestep the problem of hibernate classnames being written out to the XML file.
More detail follows.
Say I have the following class...
public class XStreamExample { private Map<String,String> myTreeMap = new TreeMap<String,String>(); private Map<String, String> myHashMap = new HashMap<String, String>(); private Set<String> myTreeSet = new TreeSet<String>(); private Set<String> myHashSet = new HashSet<String>(); public XStreamExample() { myTreeMap.put("foo", "bar"); myHashMap.put("foo", "bar"); myTreeSet.add("a"); myHashSet.add("a"); } public static void main(String[] args) { XStream xStream = new XStream(); xStream.alias("xstream-example", XStreamExample.class); System.out.println(xStream.toXML(new XStreamExample())); } }
I'll get output like this:
<xstream-example> <myTreeMap class="tree-map"> <no-comparator/> <entry> <string>foo</string> <string>bar</string> </entry> </myTreeMap> <myHashMap> <entry> <string>foo</string> <string>bar</string> </entry> </myHashMap> <myTreeSet class="tree-set"> <no-comparator/> <string>a</string> </myTreeSet> <myHashSet> <string>a</string> </myHashSet> </xstream-example>
Ideally I'd like to be able to omit the class="tree-map" and similar attributes. I realise I can add a default implementation for certain types, but say I have a complex graph of objects that I'm trying to turn into XML, with a mixture of different types of maps and sets. Furthermore, the actual map and set implementations in use may be hibernate lazy-load implementations.
I'd like to be able to say which map/set implementations should be used for specific fields, something like...
xstream.useMapImplementation(XStreamExample.class, "myTreeMap", TreeMap.class); xstream.useSetImplementation(XStreamExample.class, "myTreeSet", TreeSet.class);
...or similar. XStream would omit the class attributes on export regardless of what implementation is in use, and whether it's a regular java or hibernate implementation. And it would know which class to instantiate when encountering the fields on import. This keeps the XML a little but cleaner, and prevents java-specific, and even hibernate-specific things creeping in.