simplexml icon indicating copy to clipboard operation
simplexml copied to clipboard

Deserializing a generic list

Open lsarni opened this issue 8 years ago • 1 comments

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;
}

lsarni avatar Feb 21 '17 17:02 lsarni

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

Ayutac avatar Feb 21 '17 18:02 Ayutac