Deserialize generic type with XStream
I'm trying to use XStream to deserialize a xml response to a class that has a generic type field.
For example I have class Response
class Response<T extends BaseDTO>{
int id;
T field;
public int getId(){
return id;
}
public T getField(){
return field;
}
}
class B extends BaseDTO{}
class C extends BaseDTO{}
public abstract class XStreamFactory{
public static <T extends BaseDTO> XStream initXStream(Class<T> c){
XStream xstream = new XStream(new DomDriver());
xstream.alias("response-root", Response.class);
if(c.equals(B.class)){
xstream.alias("specific-response-b", B.class);
else
xstream.alias("specific-response-c", C.class);
xstream = addAliases(xstream, c);
return xstream;
}
// we want to process the annotations declared on type c
protected static XStream addAliases(XStream xstream, Class<?> c){
while (c != null) {
xstream.processAnnotations(c);
c = c.getSuperclass();
}
return xstream;
}
}
To deserialize the response I do the following:
XStream xstream = XStreamFactory.initializeXStream(B.class);
Response<B> resp = (Response<B>) xstream.fromXML(response);
Below are the xml responses that I get:
<response-root>
<开发者_JAVA技巧id></id>
<specific-response-b>
//... content specific for class B
</specific-response-b>
</response-root>
or
<response-root>
<id></id>
<specific-response-c>
//... content specific for class c
</specific-response-c>
</response-root>
The code above does not deserialize the response. It throws the following exception:
com.thoughtworks.xstream.converters.ConversionException: Element specific-response-b of type B is not defined as field in type Response
Can you guys provide any pointers on how to achieve what I want? Thank you.
XStream doesn't have a problem with this. It's probably some kind of custom marshalling that you're doing. Here's an example of marshalling and unmarshalling the classes you specified above:
public static void main(String[] args) {
Response<B> responseB = new Response<B>(5, new B());
String xml = new XStream().toXML(responseB);
System.out.println("marshalled:\n" + xml);
Response<B> resultB = (Response<B>) new XStream().fromXML(xml);
System.out.println("unmarshalled: " + resultB);
Response<C> responseC = new Response<C>(10, new C());
xml = new XStream().toXML(responseC);
System.out.println("marshalled:\n" + xml);
Response<C> resultC = (Response<C>) new XStream().fromXML(xml);
System.out.println("unmarshalled: " + resultC);
}
static class Response<T extends Foo> {
int id;
T field;
Response(int id, T field) {
this.id = id;
this.field = field;
}
public int getId() { return id; }
public T getField() { return field; }
}
static class Foo {}
static class B extends Foo {}
static class C extends Foo {}
The output--a little messy because of no aliasing and no toString():
marshalled:
<rds.testbed.XStreamReadingNamespaceAttributes_-Response>
<id>5</id>
<field class="rds.testbed.XStreamReadingNamespaceAttributes$B"/>
</rds.testbed.XStreamReadingNamespaceAttributes_-Response>
unmarshalled: rds.testbed.XStreamReadingNamespaceAttributes$Response@1cc7b00c
marshalled:
<rds.testbed.XStreamReadingNamespaceAttributes_-Response>
<id>10</id>
<field class="rds.testbed.XStreamReadingNamespaceAttributes$C"/>
</rds.testbed.XStreamReadingNamespaceAttributes_-Response>
unmarshalled: rds.testbed.XStreamReadingNamespaceAttributes$Response@3d66aa18
精彩评论