XStream
  1. XStream
  2. XSTR-193

Preserving references to objects across stream items

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.3
    • Component/s: None
    • Labels:
      None
    • JDK version and platform:
      Sun 1.5.0.01 for Windows

      Description

      Hi,

      I'm rather new to XStream. The library's great, but I still have a problem I cannot solve. I couldn't find an answer in the docs, nor in the mailing list.

      I have two objects p1 and p2. p2 contains a reference to p1. If I put them both in a list and serialize the list, everything's ok. If I try to serialize them one after the other, I get duplicates.

      Is this OK, and I'm missing something?

      Thank you very much.
      Francesco

      File Person.java:
      ------------------------------------------
      import java.io.*;

      public class Person implements Serializable
      {
      Person(String n, int a, Person s)

      { name = n; age = a; secretary = s; }

      public String name;
      public int age;
      public Person secretary;
      }
      ------------------------------------------

      File TestMultipleRefs.java
      ------------------------------------------
      import java.util.*;
      import java.io.*;

      import com.thoughtworks.xstream.*;
      import com.thoughtworks.xstream.io.xml.*;

      public class TestMultipleRefs
      {
      public static void main(String[] args)
      {
      Person p1;
      Person p2;

      p1 = new Person("Alice", 50, null);
      p2 = new Person("Joe", 35, p1);

      ArrayList list = new ArrayList();
      list.add(p1);
      list.add(p2);

      // Serialize
      XStream xstream = new XStream(new DomDriver());
      java.io.StringWriter writer = new java.io.StringWriter();
      try

      { ObjectOutputStream out = xstream.createObjectOutputStream(writer); // ObjectOutputStream out = // new ObjectOutputStream(new FileOutputStream("test.dat")); out.writeObject(p1); out.writeObject(p2); out.writeObject(list); out.close(); }

      catch (IOException e)

      {e.printStackTrace();}

      // Write down XML
      System.out.println(writer.toString());

      // Deserialize
      java.io.StringReader reader = new java.io.StringReader(writer.toString());
      try
      { ObjectInputStream in = xstream.createObjectInputStream(reader); // ObjectInputStream in = // new ObjectInputStream(new FileInputStream("prova.dat")); p1 = (Person)in.readObject(); p2 = (Person)in.readObject(); list = (ArrayList)in.readObject(); in.close(); }
      catch (IOException e) {e.printStackTrace();}

      catch (ClassNotFoundException e)

      {e.printStackTrace();}

      Person p1List = (Person) list.get(0);
      Person p2List = (Person) list.get(1);
      p1.age=25;
      System.out.println("Alice's age: "+p1.age);
      System.out.println("Age of Joe's secretary: "+p2.secretary.age);
      p1List.age = 35;
      System.out.println("Alice's age: "+p1List.age);
      System.out.println("Age of Joe's secretary: "+p2List.secretary.age);
      }
      }

        People

        • Assignee:
          Jörg Schaible
          Reporter:
          Francesco Casella
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: