Details
-
Type: Bug
-
Status: Closed
-
Priority: Minor
-
Resolution: Not A Bug
-
Affects Version/s: 1.4.2
-
Fix Version/s: None
-
Component/s: Annotations
-
Labels:None
-
JDK version and platform:1.6.0_33 for Mac OS 10.7
Description
When using implicit collections, when one collection references items in another collection, XStream is unable to properly reproduce the object graph, and one of the collections ends up being null.
Attached is a sample project with a JUnit test which reproduces the issue. It spits the XML to System.out before trying to deserialize, so you can see what it's generating. The resulting XML looks like this:
<bunny-family> <bunny name="mother"> <bunny-child name="15b2b5a2-2d52-4216-b714-102dd327611b"/> <bunny-child name="e3674e4a-8c0b-41bc-ad99-8153595c6d0b"/> </bunny> <bunny name="father"> <bunny-child reference="../../bunny/bunny-child"/> <bunny-child reference="../../bunny/bunny-child[2]"/> </bunny> </bunny-family>
Sorry, but this is how implicit collections work. Since the container itself has no representation anymore in XML, XStream cannot know, if the container was empty or null before serialization. Actually, since no element of the container is in the XML, XStream not even knows at deserialization time that there might be one. You may implement a readObject or readResolve method (see Java serialization) to initialize the container after deserialization, if it is null.