Search code examples
javagenericsserializationxml-serializationxstream

Annotations in XStream without known class type


I have following, problem with XStream: when I try to read the annotations I need to use following sentence:

xstream.processAnnotations(DataClass .class);

which defines explicitly the class I'm going to serialize. But in my code:

public class Tester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        DataClass data = new DataClass();
        data.familyName = "Pil";
        data.firstName = "Paco";
        data.ID = 33;
        data.properties.put("one", "1");
        data.properties.put("two", "2");
        data.properties.put("three", "3");

        String xml = getXmlString(data);
        System.out.println(xml);
    }

    public static  String getXmlString(Object data) {
        String ret = "";
        final StringWriter stringWriter = new StringWriter();

        XStream xstream = new XStream(new StaxDriver());
        xstream.processAnnotations(Object.class);
        xstream.marshal(data, new PrettyPrintWriter(stringWriter));

        ret = stringWriter.toString();

        return ret;
    }
}

where the dataClass is:

@XStreamAlias("data")
public class DataClass {
    public Integer ID = 0;
    public String firstName = "";
    public String familyName = "";
    public Map<String, String> properties = null;

    public DataClass(){
        properties = new HashMap<String,String>();
    }
}

I would like to have something like that:

public static  <T> String getXmlString(T data) {
        String ret = "";
        final StringWriter stringWriter = new StringWriter();

        XStream xstream = new XStream(new StaxDriver());
        xstream.processAnnotations(T.class);
        xstream.marshal(data, new PrettyPrintWriter(stringWriter));

        ret = stringWriter.toString();

        return ret;
    }

but it doesn't work.

Do anyone know if it is possible what I'm trying to do?


Solution

  • Probably you should enabled "Auto-detect Annotations" mode:

    public static <T> String getXmlString(T data) {
        final StringWriter stringWriter = new StringWriter();
    
        XStream xstream = new XStream(new StaxDriver());
        xstream.autodetectAnnotations(true);
        xstream.marshal(data, new PrettyPrintWriter(stringWriter));
    
        return stringWriter.toString();
    }
    

    Please, read "Auto-detect Annotations" paragraph. You can find in it all information about problems which are linked with this solution. Such as: Chicken-and-egg problem, Concurrency, Exceptions and Performance.

    Result:

    <data>
      <ID>33</ID>
      <firstName>Paco</firstName>
      <familyName>Pil</familyName>
      <properties>
        <entry>
          <string>two</string>
          <string>2</string>
        </entry>
        <entry>
          <string>one</string>
          <string>1</string>
        </entry>
        <entry>
          <string>three</string>
          <string>3</string>
        </entry>
      </properties>
    </data>