Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xstream List - Duplicate field

Tags:

java

json

xstream

I have a problem with XStream. I have a java class called simplePerson as follows:

public class SimplePerson {

@XStreamAlias("name")
private String name;


private List<String> cars;

I am using it to convert it to json using:

XStream xstream = new XStream(new JettisonMappedXmlDriver(){
        public HierarchicalStreamWriter createWriter(Writer writer) {
            return new JsonWriter(writer);
        }
    });

xstream.setMode(XStream.NO_REFERENCES);

xstream.toXML(person)

Using it I get the following json: {"simpleperson": { "name": "joe", "cars": ["Jag", "BMW"] }}

When I use the same json to convert it back to an object using xstream.fromXML(json), I get the following exception:

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$DuplicateFieldException: Duplicate field cars

I cant figure out what is the problem. I am using the same exact json that Xstream gave as output. Any help will be appreciated.

like image 538
BlackEagle Avatar asked Feb 03 '26 08:02

BlackEagle


1 Answers

I guess you need to add Implicit collection. like.

xstream.addImplicitCollection(SimplePerson.class, "cars");
like image 188
Mady Avatar answered Feb 05 '26 21:02

Mady