simplexml
simplexml copied to clipboard
Deserializing a generic list
I'm trying to deserialize this
<Collection>
<item>test uno</item>
<item>test dos</item>
</Collection>
Using this
@Root(name="Collection")
public class SimpleCollection<T> extends Vector<T> implements Serializable {
@ElementList(entry="item",inline=true)
SimpleCollection<T> list = this;
}
The problem is that T is always an Object
instead of being of the type that it was initialized (in this case a String
).
I tried using an Visitor
, and on the read method field is of type Object
. Is there a way I can change that?
I now the class of my collection, how can I pass it to the read method?
If I change it like this it works (for strings), but I need it to be generic.
@Root(name="Collection")
public class SimpleCollection<T> extends Vector<T> implements Serializable {
@ElementList(entry="item",inline=true)
SimpleCollection<String> list = ( SimpleCollection<String>)this;
}
As a workaround, maybe you could create an Interface SimpleXmlSerializable annotated with @Root and let T extends this interface. It's just an idea. I think the main problem is the serialization of classes. If you have no problem working with a ClassLoader, you could also serialize T itself by providing an abstract getClass method or a Lambda Class Supplier.
Disclaimer: I'm just a user of SimpleXML