Just remove the annotation @XStreamImplicit(itemFieldName="PRODUCT")
from your products
field in Order
class. This instructs XStream to serialize your collection as an implicit collection, which means there will be no root object for the collection and all of its elements will be serialized separately and enclosed in an object called with the name of its collection (PRODUCT
). This results in a JSON object which has multiple properties with the same name (PRODUCT
), which is invalid.
By the way, XStream is originally meant to be serializing XMLs, you might try to use a dedicated JSON serializer library, GSON for instance.
UPDATE
As you are using the same POJOs to deserialize an XML, and removing the mentioned annotation breaks the XML deserialization, you have to configure XStream "manually" so that it can handle the implicit collection during deserialization of the XML:
xstream.addImplicitCollection(Order.class, "products", "PRODUCT", Product.class);
This has the same effect as the annotation except that it applies only to the deserialization.