Hi Michael,

Michael Mogley wrote:

> Hi Jörg,
> 
> Ok, I wrote a small self-contained class to illustrate the issue:
> 
> import java.io.EOFException;
> import java.io.ObjectInputStream;
> import java.util.LinkedList;
> import java.util.List;
> 
> import org.apache.commons.io.IOUtils;
> import org.apache.commons.lang3.StringUtils;
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
> 
> import com.thoughtworks.xstream.XStream;
> 
> public class TestXStreamReferencing
> {
>   //~ Statics
> ------------------------------------------------------------------
> 
>   static private final Logger LOG =
> LoggerFactory.getLogger(TestXStreamReferencing.class);
> 
>   //~ Methods
> ------------------------------------------------------------------
> 
>   public static void main(String[] args)
>   {
>     try
>     {
>       String xml = StringUtils.join(new String[] {
>         "<list>",
>         "  <typeA/>",
>         "  <typeA/>",
>         "  <typeB>",
>         "    <items>",
>         "      <typeA reference=\"../../../typeA[2]\"/>",
>         "    </items>",
>         "  </typeB>",
>         "</list>"
>       }, "\n");
> 
>       // Read objects.
>       XStream xsUsersAndGroups = new XStream();
>       xsUsersAndGroups.alias("typeA", TypeA.class);
>       xsUsersAndGroups.alias("typeB", TypeB.class);
> 
>       ObjectInputStream isUsersAndGroups =
> xsUsersAndGroups.createObjectInputStream(IOUtils.toInputStream(xml));
> 
>       try
>       {
>         while (true)
>         {
>           Object nextObject = isUsersAndGroups.readObject();
> 
>           LOG.info("read: {}", nextObject);
>         }
>       }
>       catch (EOFException e)
>       {
>       }
>     }
>     catch (Throwable t)
>     {
>       LOG.error(t.getMessage(), t);
>     }
>   }

[snip]

OK, this explains it. Reading or writing individual objects in an 
ObjectStream does not preserve references between those objects. Each 
writeObject/readObject call is a separate marshalling process. Have alook at 
XStream's acceptance tests. In MultipleObjectsInOneStream is a workaround 
with a custom MarshallingStrategy that overcomes this limitation.

Hope this helps,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to