1. XStream
  2. XSTR-624

Allow users to specify at the field level which set/map implementations should be used


    • Type: New Feature New Feature
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.3.1
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
    • JDK version and platform:
      Non JDK-specific


      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,
          private Set<String> myTreeSet = new TreeSet<String>();
          private Set<String> myHashSet = new HashSet<String>();
          public XStreamExample()
              myTreeMap.put("foo", "bar");
              myHashMap.put("foo", "bar");
          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:

        <myTreeMap class="tree-map">
        <myTreeSet class="tree-set">

      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",
          xstream.useSetImplementation(XStreamExample.class, "myTreeSet",

      ...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.


        • Assignee:
          Jörg Schaible
          Neil Salter
        • Votes:
          0 Vote for this issue
          0 Start watching this issue


          • Created: